summaryrefslogtreecommitdiff
path: root/scene/gui/text_edit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/text_edit.cpp')
-rw-r--r--scene/gui/text_edit.cpp667
1 files changed, 570 insertions, 97 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index 0fc8e39fef..fe03b34105 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -44,7 +44,20 @@ static bool _is_text_char(CharType c) {
static bool _is_symbol(CharType c) {
- return c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t');
+ return c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t' || c==' ');
+}
+
+static bool _is_char(CharType c) {
+
+ return (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_';
+}
+
+static bool _is_number(CharType c) {
+ return (c >= '0' && c <= '9');
+}
+
+static bool _is_hex_symbol(CharType c) {
+ return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}
static bool _is_pair_right_symbol(CharType c) {
@@ -301,6 +314,10 @@ void TextEdit::_update_scrollbars() {
if (line_numbers)
total_width += cache.line_number_w;
+ if (draw_breakpoint_gutter) {
+ total_width += cache.breakpoint_gutter_width;
+ }
+
bool use_hscroll=true;
bool use_vscroll=true;
@@ -399,9 +416,15 @@ void TextEdit::_notification(int p_what) {
case NOTIFICATION_THEME_CHANGED: {
_update_caches();
- };
+ } break;
case NOTIFICATION_DRAW: {
+ if (draw_breakpoint_gutter) {
+ cache.breakpoint_gutter_width = breakpoint_gutter_width;
+ } else {
+ cache.breakpoint_gutter_width = 0;
+ }
+
int line_number_char_count=0;
{
@@ -426,7 +449,7 @@ void TextEdit::_notification(int p_what) {
RID ci = get_canvas_item();
- int xmargin_beg=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w;
+ int xmargin_beg=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width;
int xmargin_end=cache.size.width-cache.style_normal->get_margin(MARGIN_RIGHT);
//let's do it easy for now:
cache.style_normal->draw(ci,Rect2(Point2(),cache.size));
@@ -659,25 +682,23 @@ void TextEdit::_notification(int p_what) {
int char_ofs=0;
int ofs_y=i*get_row_height()+cache.line_spacing/2;
bool prev_is_char=false;
+ bool prev_is_number = false;
bool in_keyword=false;
+ bool in_word = false;
+ bool in_function_name = false;
+ bool in_member_variable = false;
+ bool is_hex_notation = false;
Color keyword_color;
// check if line contains highlighted word
int highlighted_text_col = -1;
- if (highlighted_text.length() != 0) {
- highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, 0);
- }
+ int search_text_col = -1;
- if (cache.line_number_w) {
- Color fcol = cache.font_color;
- fcol.a*=0.4;
- String fc = String::num(line+1);
- while (fc.length() < line_number_char_count) {
- fc="0"+fc;
- }
+ if (!search_text.empty())
+ search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
- cache.font->draw(ci,Point2(cache.style_normal->get_margin(MARGIN_LEFT),ofs_y+cache.font->get_ascent()),fc,fcol);
- }
+ if (highlighted_text.length() != 0 && highlighted_text != search_text)
+ highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE|SEARCH_WHOLE_WORDS, 0);
const Map<int,Text::ColorRegionInfo>& cri_map=text.get_color_region_info(line);
@@ -692,11 +713,28 @@ void TextEdit::_notification(int p_what) {
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.breakpoint_color);
}
-
if (line==cursor.line) {
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(0, ofs_y,xmargin_end,get_row_height()),cache.current_line_color);
+ }
+
+ // draw breakpoint marker
+ if (text.is_breakpoint(line)) {
+ if (draw_breakpoint_gutter) {
+ int vertical_gap = cache.breakpoint_gutter_width / 2;
+ int marker_size = cache.breakpoint_gutter_width - vertical_gap;
+ // no transparency on marker
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + 1, ofs_y + vertical_gap ,marker_size, marker_size),Color(cache.breakpoint_color.r, cache.breakpoint_color.g, cache.breakpoint_color.b));
+ }
+ }
+
- VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.current_line_color);
+ if (cache.line_number_w) {
+ String fc = String::num(line+1);
+ while (fc.length() < line_number_char_count) {
+ fc="0"+fc;
+ }
+ cache.font->draw(ci,Point2(cache.style_normal->get_margin(MARGIN_LEFT)+cache.breakpoint_gutter_width,ofs_y+cache.font->get_ascent()),fc,cache.line_number_color);
}
for (int j=0;j<str.length();j++) {
@@ -712,13 +750,43 @@ void TextEdit::_notification(int p_what) {
color = cache.font_color; //reset
//find keyword
- bool is_char = _is_text_char(str[j]);
- bool is_symbol=_is_symbol(str[j]);
+ bool is_char = _is_text_char(str[j]);
+ bool is_symbol = _is_symbol(str[j]);
+ bool is_number = _is_number(str[j]);
if (j==0 && in_region>=0 && color_regions[in_region].line_only) {
in_region=-1; //reset regions that end at end of line
}
+ // allow ABCDEF in hex notation
+ if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
+ is_number = true;
+ } else {
+ is_hex_notation = false;
+ }
+
+ // check for dot or 'x' for hex notation in floating point number
+ if ((str[j] == '.' || str[j] == 'x') && !in_word && prev_is_number && !is_number) {
+ is_number = true;
+ is_symbol = false;
+
+ if (str[j] == 'x' && str[j-1] == '0') {
+ is_hex_notation = true;
+ }
+ }
+
+ if (!in_word && _is_char(str[j])) {
+ in_word = true;
+ }
+
+ if ((in_keyword || in_word) && !is_hex_notation) {
+ is_number = false;
+ }
+
+ if (is_symbol && str[j] != '.' && in_word) {
+ in_word = false;
+ }
+
if (is_symbol && cri_map.has(j)) {
@@ -760,15 +828,49 @@ void TextEdit::_notification(int p_what) {
}
}
+ if (!in_function_name && in_word && !in_keyword) {
+
+ int k = j;
+ while(k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
+ k++;
+ }
+
+ if (str[k] == '(') {
+ in_function_name = true;
+ }
+ }
+
+ if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
+ int k = j;
+ while(k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
+ k--;
+ }
+
+ if (str[k] == '.') {
+ in_member_variable = true;
+ }
+ }
+
+ if (is_symbol) {
+ in_function_name = false;
+ in_member_variable = false;
+ }
if (in_region>=0)
color=color_regions[in_region].color;
else if (in_keyword)
color=keyword_color;
+ else if (in_member_variable)
+ color=cache.member_variable_color;
+ else if (in_function_name)
+ color=cache.function_color;
else if (is_symbol)
color=symbol_color;
+ else if (is_number)
+ color=cache.number_color;
prev_is_char=is_char;
+ prev_is_number=is_number;
}
int char_w;
@@ -799,20 +901,45 @@ void TextEdit::_notification(int p_what) {
break;
}
- bool in_selection = (selection.active && line>=selection.from_line && line<=selection.to_line && (line>selection.from_line || j>=selection.from_column) && (line<selection.to_line || j<selection.to_column));
+ bool in_search_result=false;
+ if (search_text_col != -1) {
+ // if we are at the end check for new search result on same line
+ if (j >= search_text_col+search_text.length())
+ search_text_col = _get_column_pos_of_word(search_text, str, search_flags, j);
+
+ in_search_result = j >= search_text_col && j < search_text_col+search_text.length();
+
+ if (in_search_result) {
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w, get_row_height())),cache.search_result_color);
+ }
+ }
+
+ bool in_selection = (selection.active && line>=selection.from_line && line<=selection.to_line && (line>selection.from_line || j>=selection.from_column) && (line<selection.to_line || j<selection.to_column));
if (in_selection) {
//inside selection!
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w,get_row_height())),cache.selection_color);
}
+ if (in_search_result) {
+ Color border_color=(line==search_result_line && j>=search_result_col && j<search_result_col+search_text.length())?cache.font_color:cache.search_result_border_color;
+
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w,1)),border_color);
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y+get_row_height()-1 ), Size2i(char_w,1)),border_color);
+
+ if (j==search_text_col)
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(1,get_row_height())),border_color);
+ if (j==search_text_col+search_text.length()-1)
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin+char_w-1, ofs_y ), Size2i(1,get_row_height())),border_color);
+ }
+
if (highlight_all_occurrences) {
if (highlighted_text_col != -1) {
// if we are at the end check for new word on same line
if (j > highlighted_text_col+highlighted_text.length()) {
- highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, j);
+ highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE|SEARCH_WHOLE_WORDS, j);
}
bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col+highlighted_text.length());
@@ -857,9 +984,18 @@ void TextEdit::_notification(int p_what) {
if (cursor.column==j && cursor.line==line) {
cursor_pos = Point2i( char_ofs+char_margin, ofs_y );
- VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.font_color);
+ if (insert_mode) {
+ cursor_pos.y += get_row_height();
+ }
+ if (draw_caret) {
+ if (insert_mode) {
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color);
+ } else {
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
+ }
+ }
}
char_ofs+=char_w;
@@ -868,8 +1004,19 @@ void TextEdit::_notification(int p_what) {
if (cursor.column==str.length() && cursor.line==line && (char_ofs+char_margin)>=xmargin_beg) {
cursor_pos=Point2i( char_ofs+char_margin, ofs_y );
- VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.font_color);
+ if (insert_mode) {
+ cursor_pos.y += get_row_height();
+ }
+
+ if (draw_caret) {
+ if (insert_mode) {
+ int char_w = cache.font->get_char_size(' ').width;
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color);
+ } else {
+ VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
+ }
+ }
}
}
@@ -1071,7 +1218,7 @@ void TextEdit::_consume_pair_symbol(CharType ch) {
int new_column,new_line;
- _begin_compex_operation();
+ begin_complex_operation();
_insert_text(get_selection_from_line(), get_selection_from_column(),
ch_single,
&new_line, &new_column);
@@ -1084,7 +1231,7 @@ void TextEdit::_consume_pair_symbol(CharType ch) {
get_selection_to_column() + to_col_offset,
ch_single_pair,
&new_line,&new_column);
- _end_compex_operation();
+ end_complex_operation();
cursor_set_line(get_selection_to_line());
cursor_set_column(get_selection_to_column() + to_col_offset);
@@ -1168,6 +1315,66 @@ void TextEdit::backspace_at_cursor() {
}
+void TextEdit::indent_selection_right() {
+
+ if (!is_selection_active()) {
+ return;
+ }
+ begin_complex_operation();
+ int start_line = get_selection_from_line();
+ int end_line = get_selection_to_line();
+
+ // ignore if the cursor is not past the first column
+ if (get_selection_to_column() == 0) {
+ end_line--;
+ }
+
+ for (int i = start_line; i <= end_line; i++) {
+ String line_text = get_line(i);
+ line_text = '\t' + line_text;
+ set_line(i, line_text);
+ }
+
+ // fix selection being off by one on the last line
+ selection.to_column++;
+ end_complex_operation();
+ update();
+}
+
+void TextEdit::indent_selection_left() {
+
+ if (!is_selection_active()) {
+ return;
+ }
+ begin_complex_operation();
+ int start_line = get_selection_from_line();
+ int end_line = get_selection_to_line();
+
+ // ignore if the cursor is not past the first column
+ if (get_selection_to_column() == 0) {
+ end_line--;
+ }
+ String last_line_text = get_line(end_line);
+
+ for (int i = start_line; i <= end_line; i++) {
+ String line_text = get_line(i);
+
+ if (line_text.begins_with("\t")) {
+ line_text = line_text.substr(1, line_text.length());
+ set_line(i, line_text);
+ } else if (line_text.begins_with(" ")) {
+ line_text = line_text.substr(4, line_text.length());
+ set_line(i, line_text);
+ }
+ }
+
+ // fix selection being off by one on the last line
+ if (last_line_text != get_line(end_line) && selection.to_column > 0) {
+ selection.to_column--;
+ }
+ end_complex_operation();
+ update();
+}
void TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
@@ -1187,7 +1394,7 @@ void TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) co
col=text[row].size();
} else {
- col=p_mouse.x-(cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w);
+ col=p_mouse.x-(cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width);
col+=cursor.x_ofs;
col=get_char_pos_for( col, get_line(row) );
}
@@ -1256,9 +1463,20 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
if (mb.button_index==BUTTON_LEFT) {
+ _reset_caret_blink_timer();
+
int row,col;
_get_mouse_pos(Point2i(mb.x,mb.y), row,col);
+ // toggle breakpoint on gutter click
+ if (draw_breakpoint_gutter) {
+ int gutter=cache.style_normal->get_margin(MARGIN_LEFT);
+ if (mb.x > gutter && mb.x <= gutter + cache.breakpoint_gutter_width + 3) {
+ set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row));
+ return;
+ }
+ }
+
int prev_col=cursor.column;
int prev_line=cursor.line;
@@ -1372,6 +1590,15 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
update();
}
+
+ if (mb.button_index==BUTTON_RIGHT) {
+
+ menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
+ menu->set_size(Vector2(1,1));
+ menu->popup();
+ grab_focus();
+
+ }
} else {
if (mb.button_index==BUTTON_LEFT)
@@ -1390,6 +1617,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (selection.selecting_mode!=Selection::MODE_NONE) {
+ _reset_caret_blink_timer();
+
int row,col;
_get_mouse_pos(Point2i(mm.x,mm.y), row,col);
@@ -1510,6 +1739,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.scancode==KEY_BACKSPACE) {
+ _reset_caret_blink_timer();
+
backspace_at_cursor();
_update_completion_candidates();
accept_event();
@@ -1525,20 +1756,29 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.unicode>32) {
- if (cursor.column<text[cursor.line].length() && text[cursor.line][cursor.column]==k.unicode) {
- //same char, move ahead
- cursor_set_column(cursor.column+1);
+ _reset_caret_blink_timer();
+ const CharType chr[2] = {(CharType)k.unicode, 0};
+ if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
+ _consume_pair_symbol(chr[0]);
} else {
- //different char, go back
- const CharType chr[2] = {(CharType)k.unicode, 0};
- if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
- _consume_pair_symbol(chr[0]);
- } else {
- _insert_text_at_cursor(chr);
+
+ // remove the old character if in insert mode
+ if (insert_mode) {
+ begin_complex_operation();
+
+ // make sure we don't try and remove empty space
+ if (cursor.column < get_line(cursor.line).length()) {
+ _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
+ }
}
- }
+ _insert_text_at_cursor(chr);
+
+ if (insert_mode) {
+ end_complex_operation();
+ }
+ }
_update_completion_candidates();
accept_event();
@@ -1564,8 +1804,13 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
k.mod.shift=false;
}
- // stuff to do when selection is active..
+ if (!k.mod.command) {
+ _reset_caret_blink_timer();
+ }
+ // save here for insert mode, just in case it is cleared in the following section
+ bool had_selection = selection.active;
+ // stuff to do when selection is active..
if (selection.active) {
if (readonly)
@@ -1578,51 +1823,13 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
switch(k.scancode) {
case KEY_TAB: {
-
- String txt = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
- String prev_txt=txt;
-
if (k.mod.shift) {
-
- for(int i=0;i<txt.length();i++) {
- if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/)) && (txt[i]=='\t' || txt[i]==' ')) {
- txt.remove(i);
- //i--;
- }
- }
+ indent_selection_left();
} else {
-
- for(int i=0;i<txt.length();i++) {
-
- if (((i>0 && txt[i-1]=='\n') || (i==0 /*&& selection.from_column==0*/))) {
- txt=txt.insert(i,"\t");
- //i--;
- }
- }
+ indent_selection_right();
}
-
- if (txt!=prev_txt) {
-
- int sel_line=selection.from_line;
- int sel_column=selection.from_column;
-
- cursor_set_line(selection.from_line);
- cursor_set_column(selection.from_column);
- _begin_compex_operation();
- _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
- _insert_text_at_cursor(txt);
- _end_compex_operation();
- selection.active=true;
- selection.from_column=sel_column;
- selection.from_line=sel_line;
- selection.to_column=cursor.column;
- selection.to_line=cursor.line;
- update();
- }
-
dobreak=true;
accept_event();
-
} break;
case KEY_X:
case KEY_C:
@@ -1630,6 +1837,13 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
clear=(!k.mod.command || k.mod.shift || k.mod.alt );
break;
case KEY_DELETE:
+ if (!k.mod.shift) {
+ accept_event();
+ clear=true; dobreak=true;
+ } else if (k.mod.command || k.mod.alt) {
+ dobreak=true;
+ }
+ break;
case KEY_BACKSPACE:
accept_event();
clear=true; dobreak=true;
@@ -1662,6 +1876,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
if (clear) {
+ if (!dobreak) {
+ begin_complex_operation();
+ }
selection.active=false;
update();
_remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column);
@@ -1733,7 +1950,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (completion_hint!="") {
completion_hint="";
update();
-
+ } else {
+ scancode_handled=false;
}
} break;
case KEY_TAB: {
@@ -1936,7 +2154,17 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
scancode_handled=false;
break;
}
-#ifdef APPLE_STYLE_KEYS
+#ifndef APPLE_STYLE_KEYS
+ if (k.mod.command) {
+ _scroll_lines_up();
+ break;
+ }
+#else
+ if (k.mod.command && k.mod.alt) {
+ _scroll_lines_up();
+ break;
+ }
+
if (k.mod.command)
cursor_set_line(0);
else
@@ -1963,7 +2191,17 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
scancode_handled=false;
break;
}
-#ifdef APPLE_STYLE_KEYS
+#ifndef APPLE_STYLE_KEYS
+ if (k.mod.command) {
+ _scroll_lines_down();
+ break;
+ }
+#else
+ if (k.mod.command && k.mod.alt) {
+ _scroll_lines_down();
+ break;
+ }
+
if (k.mod.command)
cursor_set_line(text.size()-1);
else
@@ -2295,6 +2533,12 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
}
*/
+ if (k.scancode==KEY_INSERT) {
+ set_insert_mode(!insert_mode);
+ accept_event();
+ return;
+ }
+
if (!scancode_handled && !k.mod.command) { //for german kbds
if (k.unicode>=32) {
@@ -2302,6 +2546,16 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (readonly)
break;
+ // remove the old character if in insert mode and no selection
+ if (insert_mode && !had_selection) {
+ begin_complex_operation();
+
+ // make sure we don't try and remove empty space
+ if (cursor.column < get_line(cursor.line).length()) {
+ _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
+ }
+ }
+
const CharType chr[2] = {(CharType)k.unicode, 0};
if (completion_hint!="" && k.unicode==')') {
@@ -2313,6 +2567,13 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
_insert_text_at_cursor(chr);
}
+ if (insert_mode && !had_selection) {
+ end_complex_operation();
+ }
+
+ if (selection.active != had_selection) {
+ end_complex_operation();
+ }
accept_event();
} else {
@@ -2354,6 +2615,36 @@ void TextEdit::_post_shift_selection() {
selection.selecting_text=true;
}
+void TextEdit::_scroll_lines_up() {
+ // adjust the vertical scroll
+ if (get_v_scroll() > 0) {
+ set_v_scroll(get_v_scroll() - 1);
+ }
+
+ // adjust the cursor
+ if (cursor_get_line() >= (get_visible_rows() + get_v_scroll()) && !selection.active) {
+ cursor_set_line((get_visible_rows() + get_v_scroll()) - 1, false);
+ }
+}
+
+void TextEdit::_scroll_lines_down() {
+ // calculate the maximum vertical scroll position
+ int max_v_scroll = get_line_count() - 1;
+ if (!scroll_past_end_of_file_enabled) {
+ max_v_scroll -= get_visible_rows() - 1;
+ }
+
+ // adjust the vertical scroll
+ if (get_v_scroll() < max_v_scroll) {
+ set_v_scroll(get_v_scroll() + 1);
+ }
+
+ // adjust the cursor
+ if ((cursor_get_line()) <= get_v_scroll() - 1 && !selection.active) {
+ cursor_set_line(get_v_scroll(), false);
+ }
+}
+
/**** TEXT EDIT CORE API ****/
void TextEdit::_base_insert_text(int p_line, int p_char,const String& p_text,int &r_end_line,int &r_end_column) {
@@ -2614,7 +2905,7 @@ void TextEdit::adjust_viewport_to_cursor() {
if (cursor.line_ofs>cursor.line)
cursor.line_ofs=cursor.line;
- int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w;
+ int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w-cache.breakpoint_gutter_width;
if (v_scroll->is_visible())
visible_width-=v_scroll->get_combined_minimum_size().width;
visible_width-=20; // give it a little more space
@@ -2716,6 +3007,30 @@ int TextEdit::cursor_get_line() const {
return cursor.line;
}
+bool TextEdit::cursor_get_blink_enabled() const {
+ return caret_blink_enabled;
+}
+
+void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
+ caret_blink_enabled = p_enabled;
+
+ if (p_enabled) {
+ caret_blink_timer->start();
+ } else {
+ caret_blink_timer->stop();
+ }
+ draw_caret = true;
+}
+
+
+float TextEdit::cursor_get_blink_speed() const {
+ return caret_blink_timer->get_wait_time();
+}
+
+void TextEdit::cursor_set_blink_speed(const float p_speed) {
+ ERR_FAIL_COND(p_speed <= 0);
+ caret_blink_timer->set_wait_time(p_speed);
+}
void TextEdit::_scroll_moved(double p_to_val) {
@@ -2819,7 +3134,8 @@ void TextEdit::insert_text_at_cursor(const String& p_text) {
}
Control::CursorShape TextEdit::get_cursor_shape(const Point2& p_pos) const {
- if(completion_active && completion_rect.has_point(p_pos)) {
+ int gutter=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width;
+ if((completion_active && completion_rect.has_point(p_pos)) || p_pos.x < gutter) {
return CURSOR_ARROW;
}
return CURSOR_IBEAM;
@@ -2931,20 +3247,41 @@ void TextEdit::set_max_chars(int p_max_chars) {
max_chars=p_max_chars;
}
+void TextEdit::_reset_caret_blink_timer() {
+ if (caret_blink_enabled) {
+ caret_blink_timer->stop();
+ caret_blink_timer->start();
+ draw_caret = true;
+ update();
+ }
+}
+
+void TextEdit::_toggle_draw_caret() {
+ draw_caret = !draw_caret;
+ update();
+}
+
void TextEdit::_update_caches() {
cache.style_normal=get_stylebox("normal");
cache.style_focus=get_stylebox("focus");
cache.font=get_font("font");
+ cache.caret_color=get_color("caret_color");
+ cache.line_number_color=get_color("line_number_color");
cache.font_color=get_color("font_color");
cache.font_selected_color=get_color("font_selected_color");
cache.keyword_color=get_color("keyword_color");
+ cache.function_color=get_color("function_color");
+ cache.member_variable_color=get_color("member_variable_color");
+ cache.number_color=get_color("number_color");
cache.selection_color=get_color("selection_color");
cache.mark_color=get_color("mark_color");
cache.current_line_color=get_color("current_line_color");
cache.breakpoint_color=get_color("breakpoint_color");
cache.brace_mismatch_color=get_color("brace_mismatch_color");
cache.word_highlighted_color=get_color("word_highlighted_color");
+ cache.search_result_color=get_color("search_result_color");
+ cache.search_result_border_color=get_color("search_result_border_color");
cache.line_spacing=get_constant("line_spacing");
cache.row_height = cache.font->get_height() + cache.line_spacing;
cache.tab_icon=get_icon("tab");
@@ -3206,12 +3543,26 @@ String TextEdit::get_word_under_cursor() const {
return text[cursor.line].substr(prev_cc, next_cc-prev_cc);
}
+void TextEdit::set_search_text(const String &p_search_text) {
+ search_text = p_search_text;
+}
+
+void TextEdit::set_search_flags(uint32_t p_flags) {
+ search_flags = p_flags;
+}
+
+void TextEdit::set_current_search_result(int line, int col) {
+ search_result_line = line;
+ search_result_col = col;
+ update();
+}
+
void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
highlight_all_occurrences = p_enabled;
update();
}
-int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, int p_from_column) {
+int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) {
int col = -1;
if (p_key.length() > 0 && p_search.length() > 0) {
@@ -3219,12 +3570,15 @@ int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_searc
p_from_column = 0;
}
- while (col == -1 && p_from_column <= p_search.length()) {
- // match case
- col = p_search.findn(p_key, p_from_column);
+ while (col == -1 && p_from_column <= p_search.length()) {
+ if (p_search_flags&SEARCH_MATCH_CASE) {
+ col = p_search.find(p_key,p_from_column);
+ } else {
+ col = p_search.findn(p_key,p_from_column);
+ }
// whole words only
- if (col != -1) {
+ if (col != -1 && p_search_flags&SEARCH_WHOLE_WORDS) {
p_from_column=col;
if (col > 0 && _is_text_char(p_search[col-1])) {
@@ -3290,10 +3644,8 @@ bool TextEdit::search(const String &p_key,uint32_t p_search_flags, int p_from_li
//wrapped
if (p_search_flags&SEARCH_BACKWARDS) {
- text_line=text_line.substr(from_column,text_line.length());
from_column=text_line.length();
} else {
- text_line=text_line.substr(0,from_column);
from_column=0;
}
@@ -3304,7 +3656,6 @@ bool TextEdit::search(const String &p_key,uint32_t p_search_flags, int p_from_li
} else {
- //text_line=text_line.substr(0,p_from_column); //wrap around for missing begining.
if (p_search_flags&SEARCH_BACKWARDS)
from_column=text_line.length()-1;
else
@@ -3313,12 +3664,25 @@ bool TextEdit::search(const String &p_key,uint32_t p_search_flags, int p_from_li
pos=-1;
- if (!(p_search_flags&SEARCH_BACKWARDS)) {
+ int pos_from=0;
+ int last_pos=-1;
+ while ((last_pos=(p_search_flags&SEARCH_MATCH_CASE)?text_line.find(p_key,pos_from):text_line.findn(p_key,pos_from))!=-1) {
- pos = (p_search_flags&SEARCH_MATCH_CASE)?text_line.find(p_key,from_column):text_line.findn(p_key,from_column);
- } else {
+ if (p_search_flags&SEARCH_BACKWARDS) {
+
+ if (last_pos>from_column)
+ break;
+ pos=last_pos;
+
+ } else {
+
+ if (last_pos>=from_column) {
+ pos=last_pos;
+ break;
+ }
+ }
- pos = (p_search_flags&SEARCH_MATCH_CASE)?text_line.rfind(p_key,from_column):text_line.rfindn(p_key,from_column);
+ pos_from=last_pos+p_key.length();
}
if (pos!=-1 && (p_search_flags&SEARCH_WHOLE_WORDS)) {
@@ -3502,12 +3866,12 @@ void TextEdit::clear_undo_history() {
}
-void TextEdit::_begin_compex_operation() {
+void TextEdit::begin_complex_operation() {
_push_current_op();
next_operation_is_complex=true;
}
-void TextEdit::_end_compex_operation() {
+void TextEdit::end_complex_operation() {
_push_current_op();
ERR_FAIL_COND(undo_stack.size() == 0);
@@ -3554,6 +3918,15 @@ bool TextEdit::is_drawing_tabs() const{
return draw_tabs;
}
+void TextEdit::set_insert_mode(bool p_enabled) {
+ insert_mode = p_enabled;
+ update();
+}
+
+bool TextEdit::is_insert_mode() const {
+ return insert_mode;
+}
+
uint32_t TextEdit::get_version() const {
return current_op.version;
}
@@ -3703,6 +4076,9 @@ void TextEdit::_update_completion_candidates() {
}
}
+ if (l[cursor.column - 1] == '(' && !pre_keyword && !completion_strings[0].begins_with("\"")) {
+ cancel = true;
+ }
update();
@@ -3718,6 +4094,10 @@ void TextEdit::_update_completion_candidates() {
int ci_match=0;
for(int i=0;i<completion_strings.size();i++) {
if (completion_strings[i].begins_with(s)) {
+ // don't remove duplicates if no input is provided
+ if (completion_options.find(completion_strings[i]) != -1 && s != "") {
+ continue;
+ }
completion_options.push_back(completion_strings[i]);
int m=0;
int max=MIN(completion_current.length(),completion_strings[i].length());
@@ -3876,10 +4256,60 @@ void TextEdit::set_show_line_numbers(bool p_show) {
update();
}
+void TextEdit::set_draw_breakpoint_gutter(bool p_draw) {
+ draw_breakpoint_gutter = p_draw;
+ update();
+}
+
+bool TextEdit::is_drawing_breakpoint_gutter() const {
+ return draw_breakpoint_gutter;
+}
+
+void TextEdit::set_breakpoint_gutter_width(int p_gutter_width) {
+ breakpoint_gutter_width = p_gutter_width;
+ update();
+}
+
+int TextEdit::get_breakpoint_gutter_width() const {
+ return cache.breakpoint_gutter_width;
+}
+
bool TextEdit::is_text_field() const {
return true;
}
+
+void TextEdit::menu_option(int p_option) {
+
+ switch( p_option ) {
+ case MENU_CUT: {
+
+ cut();
+ } break;
+ case MENU_COPY: {
+ copy();
+ } break;
+ case MENU_PASTE: {
+
+ paste();
+ } break;
+ case MENU_CLEAR: {
+ clear();
+ } break;
+ case MENU_SELECT_ALL: {
+ select_all();
+ } break;
+ case MENU_UNDO: {
+ undo();
+ } break;
+
+ };
+}
+
+PopupMenu *TextEdit::get_menu() const {
+ return menu;
+}
+
void TextEdit::_bind_methods() {
@@ -3889,6 +4319,7 @@ void TextEdit::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_text_changed_emit"),&TextEdit::_text_changed_emit);
ObjectTypeDB::bind_method(_MD("_push_current_op"),&TextEdit::_push_current_op);
ObjectTypeDB::bind_method(_MD("_click_selection_held"),&TextEdit::_click_selection_held);
+ ObjectTypeDB::bind_method(_MD("_toggle_draw_caret"),&TextEdit::_toggle_draw_caret);
BIND_CONSTANT( SEARCH_MATCH_CASE );
BIND_CONSTANT( SEARCH_WHOLE_WORDS );
@@ -3911,7 +4342,10 @@ void TextEdit::_bind_methods() {
ObjectTypeDB::bind_method(_MD("cursor_get_column"),&TextEdit::cursor_get_column);
ObjectTypeDB::bind_method(_MD("cursor_get_line"),&TextEdit::cursor_get_line);
-
+ ObjectTypeDB::bind_method(_MD("cursor_set_blink_enabled", "enable"),&TextEdit::cursor_set_blink_enabled);
+ ObjectTypeDB::bind_method(_MD("cursor_get_blink_enabled"),&TextEdit::cursor_get_blink_enabled);
+ ObjectTypeDB::bind_method(_MD("cursor_set_blink_speed", "blink_speed"),&TextEdit::cursor_set_blink_speed);
+ ObjectTypeDB::bind_method(_MD("cursor_get_blink_speed"),&TextEdit::cursor_get_blink_speed);
ObjectTypeDB::bind_method(_MD("set_readonly","enable"),&TextEdit::set_readonly);
ObjectTypeDB::bind_method(_MD("set_wrap","enable"),&TextEdit::set_wrap);
@@ -3945,12 +4379,25 @@ void TextEdit::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_symbol_color","color"),&TextEdit::set_symbol_color);
ObjectTypeDB::bind_method(_MD("set_custom_bg_color","color"),&TextEdit::set_custom_bg_color);
ObjectTypeDB::bind_method(_MD("clear_colors"),&TextEdit::clear_colors);
+ ObjectTypeDB::bind_method(_MD("menu_option"),&TextEdit::menu_option);
+ ObjectTypeDB::bind_method(_MD("get_menu:PopupMenu"),&TextEdit::get_menu);
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/caret_blink"), _SCS("cursor_set_blink_enabled"), _SCS("cursor_get_blink_enabled"));;
+ ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret/caret_blink_speed",PROPERTY_HINT_RANGE,"0.1,10,0.1"), _SCS("cursor_set_blink_speed"),_SCS("cursor_get_blink_speed") );
ADD_SIGNAL(MethodInfo("cursor_changed"));
ADD_SIGNAL(MethodInfo("text_changed"));
ADD_SIGNAL(MethodInfo("request_completion"));
+ BIND_CONSTANT( MENU_CUT );
+ BIND_CONSTANT( MENU_COPY );
+ BIND_CONSTANT( MENU_PASTE );
+ BIND_CONSTANT( MENU_CLEAR );
+ BIND_CONSTANT( MENU_SELECT_ALL );
+ BIND_CONSTANT( MENU_UNDO );
+ BIND_CONSTANT( MENU_MAX );
+
+
}
TextEdit::TextEdit() {
@@ -3958,6 +4405,7 @@ TextEdit::TextEdit() {
readonly=false;
setting_row=false;
draw_tabs=false;
+ draw_caret=true;
max_chars=0;
clear();
wrap=false;
@@ -3967,6 +4415,8 @@ TextEdit::TextEdit() {
cache.row_height=1;
cache.line_spacing=1;
cache.line_number_w=1;
+ cache.breakpoint_gutter_width=0;
+ breakpoint_gutter_width = 0;
tab_size=4;
text.set_tab_size(tab_size);
@@ -3997,6 +4447,13 @@ TextEdit::TextEdit() {
selection.active=false;
syntax_coloring=false;
+ caret_blink_enabled=false;
+ caret_blink_timer = memnew(Timer);
+ add_child(caret_blink_timer);
+ caret_blink_timer->set_wait_time(0.65);
+ caret_blink_timer->connect("timeout", this,"_toggle_draw_caret");
+ cursor_set_blink_enabled(false);
+
custom_bg_color=Color(0,0,0,0);
idle_detect = memnew( Timer );
add_child(idle_detect);
@@ -4041,11 +4498,27 @@ TextEdit::TextEdit() {
completion_line_ofs=0;
tooltip_obj=NULL;
line_numbers=false;
+ draw_breakpoint_gutter=false;
next_operation_is_complex=false;
scroll_past_end_of_file_enabled=false;
auto_brace_completion_enabled=false;
brace_matching_enabled=false;
auto_indent=false;
+ insert_mode = false;
+
+ menu = memnew( PopupMenu );
+ add_child(menu);
+ menu->add_item(TTR("Cut"),MENU_CUT,KEY_MASK_CMD|KEY_X);
+ menu->add_item(TTR("Copy"),MENU_COPY,KEY_MASK_CMD|KEY_C);
+ menu->add_item(TTR("Paste"),MENU_PASTE,KEY_MASK_CMD|KEY_V);
+ menu->add_separator();
+ menu->add_item(TTR("Select All"),MENU_SELECT_ALL,KEY_MASK_CMD|KEY_A);
+ menu->add_item(TTR("Clear"),MENU_CLEAR);
+ menu->add_separator();
+ menu->add_item(TTR("Undo"),MENU_UNDO,KEY_MASK_CMD|KEY_Z);
+ menu->connect("item_pressed",this,"menu_option");
+
+
}
TextEdit::~TextEdit()