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.cpp171
1 files changed, 133 insertions, 38 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index 3b7ad910d6..3c8545bd75 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -6,6 +6,7 @@
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -100,15 +101,15 @@ void TextEdit::Text::set_font(const Ref<Font> &p_font) {
font = p_font;
}
-void TextEdit::Text::set_tab_size(int p_tab_size) {
+void TextEdit::Text::set_indent_size(int p_indent_size) {
- tab_size = p_tab_size;
+ indent_size = p_indent_size;
}
void TextEdit::Text::_update_line_cache(int p_line) const {
int w = 0;
- int tab_w = font->get_char_size(' ').width * tab_size;
+ int tab_w = font->get_char_size(' ').width * indent_size;
int len = text[p_line].data.length();
const CharType *str = text[p_line].data.c_str();
@@ -359,7 +360,7 @@ void TextEdit::_click_selection_held() {
if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && selection.selecting_mode != Selection::MODE_NONE) {
- Point2 mp = Input::get_singleton()->get_mouse_pos() - get_global_pos();
+ Point2 mp = Input::get_singleton()->get_mouse_position() - get_global_position();
int row, col;
_get_mouse_pos(Point2i(mp.x, mp.y), row, col);
@@ -455,7 +456,7 @@ void TextEdit::_notification(int p_what) {
int visible_rows = get_visible_rows();
- int tab_w = cache.font->get_char_size(' ').width * tab_size;
+ int tab_w = cache.font->get_char_size(' ').width * indent_size;
Color color = cache.font_color;
int in_region = -1;
@@ -812,8 +813,8 @@ void TextEdit::_notification(int p_what) {
keyword_color = *col;
}
- if (select_identifiers_enabled && hilighted_word != String()) {
- if (hilighted_word == range) {
+ if (select_identifiers_enabled && highlighted_word != String()) {
+ if (highlighted_word == range) {
underlined = true;
}
}
@@ -1304,7 +1305,38 @@ void TextEdit::backspace_at_cursor() {
_is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
_consume_backspace_for_pair_symbol(prev_line, prev_column);
} else {
- _remove_text(prev_line, prev_column, cursor.line, cursor.column);
+ // handle space indentation
+ if (cursor.column - indent_size >= 0 && indent_using_spaces) {
+
+ // if there is enough spaces to count as a tab
+ bool unindent = true;
+ for (int i = 1; i <= indent_size; i++) {
+ if (text[cursor.line][cursor.column - i] != ' ') {
+ unindent = false;
+ break;
+ }
+ }
+
+ // and it is before the first character
+ int i = 0;
+ while (i < cursor.column && i < text[cursor.line].length()) {
+ if (text[cursor.line][i] != ' ' && text[cursor.line][i] != '\t') {
+ unindent = false;
+ break;
+ }
+ i++;
+ }
+
+ // then we can remove it as a single character.
+ if (unindent) {
+ _remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column);
+ prev_column = cursor.column - indent_size;
+ } else {
+ _remove_text(prev_line, prev_column, cursor.line, cursor.column);
+ }
+ } else {
+ _remove_text(prev_line, prev_column, cursor.line, cursor.column);
+ }
}
cursor_set_line(prev_line);
@@ -1327,7 +1359,11 @@ void TextEdit::indent_selection_right() {
for (int i = start_line; i <= end_line; i++) {
String line_text = get_line(i);
- line_text = '\t' + line_text;
+ if (indent_using_spaces) {
+ line_text = space_indent + line_text;
+ } else {
+ line_text = '\t' + line_text;
+ }
set_line(i, line_text);
}
@@ -1358,8 +1394,8 @@ void TextEdit::indent_selection_left() {
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());
+ } else if (line_text.begins_with(space_indent)) {
+ line_text = line_text.substr(indent_size, line_text.length());
set_line(i, line_text);
}
}
@@ -1463,9 +1499,9 @@ void TextEdit::_gui_input(const InputEvent &p_gui_input) {
int row, col;
_get_mouse_pos(Point2i(mb.x, mb.y), row, col);
- if (mb.mod.command && hilighted_word != String()) {
+ if (mb.mod.command && highlighted_word != String()) {
- emit_signal("symbol_lookup", hilighted_word, row, col);
+ emit_signal("symbol_lookup", highlighted_word, row, col);
return;
}
@@ -1585,7 +1621,7 @@ void TextEdit::_gui_input(const InputEvent &p_gui_input) {
if (mb.button_index == BUTTON_RIGHT && context_menu_enabled) {
- menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
+ menu->set_position(get_global_transform().xform(get_local_mouse_pos()));
menu->set_size(Vector2(1, 1));
menu->popup();
grab_focus();
@@ -1608,13 +1644,13 @@ void TextEdit::_gui_input(const InputEvent &p_gui_input) {
if (mm.mod.command && mm.button_mask == 0) {
String new_word = get_word_at_pos(Vector2(mm.x, mm.y));
- if (new_word != hilighted_word) {
- hilighted_word = new_word;
+ if (new_word != highlighted_word) {
+ highlighted_word = new_word;
update();
}
} else {
- if (hilighted_word != String()) {
- hilighted_word = String();
+ if (highlighted_word != String()) {
+ highlighted_word = String();
update();
}
}
@@ -1655,11 +1691,11 @@ void TextEdit::_gui_input(const InputEvent &p_gui_input) {
if (k.pressed) {
- hilighted_word = get_word_at_pos(get_local_mouse_pos());
+ highlighted_word = get_word_at_pos(get_local_mouse_pos());
update();
} else {
- hilighted_word = String();
+ highlighted_word = String();
update();
}
}
@@ -1930,17 +1966,39 @@ void TextEdit::_gui_input(const InputEvent &p_gui_input) {
String ins = "\n";
//keep indentation
+ int space_count = 0;
for (int i = 0; i < text[cursor.line].length(); i++) {
- if (text[cursor.line][i] == '\t')
- ins += "\t";
- else
+ if (text[cursor.line][i] == '\t') {
+ if (indent_using_spaces) {
+ ins += space_indent;
+ } else {
+ ins += "\t";
+ }
+ space_count = 0;
+ } else if (text[cursor.line][i] == ' ') {
+ space_count++;
+
+ if (space_count == indent_size) {
+ if (indent_using_spaces) {
+ ins += space_indent;
+ } else {
+ ins += "\t";
+ }
+ space_count = 0;
+ }
+ } else {
break;
+ }
}
if (auto_indent) {
// indent once again if previous line will end with ':'
// (i.e. colon precedes current cursor position)
if (cursor.column > 0 && text[cursor.line][cursor.column - 1] == ':') {
- ins += "\t";
+ if (indent_using_spaces) {
+ ins += space_indent;
+ } else {
+ ins += "\t";
+ }
}
}
@@ -1986,15 +2044,36 @@ void TextEdit::_gui_input(const InputEvent &p_gui_input) {
} else {
if (k.mod.shift) {
+ //simple unindent
int cc = cursor.column;
- if (cc > 0 && cc <= text[cursor.line].length() && text[cursor.line][cursor.column - 1] == '\t') {
- //simple unindent
+ if (cc > 0 && cc <= text[cursor.line].length()) {
+ if (text[cursor.line][cursor.column - 1] == '\t') {
+ backspace_at_cursor();
+ } else {
+ if (cursor.column - indent_size >= 0) {
+
+ bool unindent = true;
+ for (int i = 1; i <= indent_size; i++) {
+ if (text[cursor.line][cursor.column - i] != ' ') {
+ unindent = false;
+ break;
+ }
+ }
- backspace_at_cursor();
+ if (unindent) {
+ _remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column);
+ cursor_set_column(cursor.column - indent_size);
+ }
+ }
+ }
}
} else {
//simple indent
- _insert_text_at_cursor("\t");
+ if (indent_using_spaces) {
+ _insert_text_at_cursor(space_indent);
+ } else {
+ _insert_text_at_cursor("\t");
+ }
}
}
@@ -3102,7 +3181,7 @@ int TextEdit::get_char_pos_for(int p_px, String p_str) const {
int px = 0;
int c = 0;
- int tab_w = cache.font->get_char_size(' ').width * tab_size;
+ int tab_w = cache.font->get_char_size(' ').width * indent_size;
while (c < p_str.length()) {
@@ -3134,7 +3213,7 @@ int TextEdit::get_column_x_offset(int p_char, String p_str) {
int px = 0;
- int tab_w = cache.font->get_char_size(' ').width * tab_size;
+ int tab_w = cache.font->get_char_size(' ').width * indent_size;
for (int i = 0; i < p_char; i++) {
@@ -3174,7 +3253,7 @@ void TextEdit::insert_text_at_cursor(const String &p_text) {
}
Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
- if (hilighted_word != String())
+ if (highlighted_word != String())
return CURSOR_POINTING_HAND;
int gutter = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width;
@@ -3951,10 +4030,24 @@ void TextEdit::_push_current_op() {
current_op.chain_forward = false;
}
-void TextEdit::set_tab_size(const int p_size) {
+void TextEdit::set_indent_using_spaces(const bool p_use_spaces) {
+ indent_using_spaces = p_use_spaces;
+}
+
+bool TextEdit::is_indent_using_spaces() const {
+ return indent_using_spaces;
+}
+
+void TextEdit::set_indent_size(const int p_size) {
ERR_FAIL_COND(p_size <= 0);
- tab_size = p_size;
- text.set_tab_size(p_size);
+ indent_size = p_size;
+ text.set_indent_size(p_size);
+
+ space_indent = "";
+ for (int i = 0; i < p_size; i++) {
+ space_indent += " ";
+ }
+
update();
}
@@ -4447,8 +4540,8 @@ void TextEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
- ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(false));
- ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport"), &TextEdit::cursor_set_line, DEFVAL(false));
+ ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(true));
+ ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport"), &TextEdit::cursor_set_line, DEFVAL(true));
ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column);
ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line);
@@ -4541,8 +4634,8 @@ TextEdit::TextEdit() {
cache.breakpoint_gutter_width = 0;
breakpoint_gutter_width = 0;
- tab_size = 4;
- text.set_tab_size(tab_size);
+ indent_size = 4;
+ text.set_indent_size(indent_size);
text.clear();
//text.insert(1,"Mongolia..");
//text.insert(2,"PAIS GENEROSO!!");
@@ -4630,6 +4723,8 @@ TextEdit::TextEdit() {
auto_brace_completion_enabled = false;
brace_matching_enabled = false;
highlight_all_occurrences = false;
+ indent_using_spaces = false;
+ space_indent = " ";
auto_indent = false;
insert_mode = false;
window_has_focus = true;