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.cpp250
1 files changed, 181 insertions, 69 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index 1759f3eb04..5415484009 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -94,9 +94,9 @@ void TextEdit::Text::set_tab_size(int p_tab_size) {
void TextEdit::Text::_update_line_cache(int p_line) const {
- int w =0;
- int tab_w=font->get_char_size(' ').width;
-
+ int w = 0;
+ int tab_w=font->get_char_size(' ').width*tab_size;
+
int len = text[p_line].data.length();
const CharType *str = text[p_line].data.c_str();
@@ -292,7 +292,10 @@ void TextEdit::_update_scrollbars() {
int vscroll_pixels = v_scroll->get_combined_minimum_size().width;
int visible_width = size.width - cache.style_normal->get_minimum_size().width;
- int total_width = text.get_max_width();
+ int total_width = text.get_max_width() + vmin.x;
+
+ if (line_numbers)
+ total_width += cache.line_number_w;
bool use_hscroll=true;
bool use_vscroll=true;
@@ -322,7 +325,6 @@ void TextEdit::_update_scrollbars() {
v_scroll->show();
v_scroll->set_max(total_rows);
v_scroll->set_page(visible_rows);
-
v_scroll->set_val(cursor.line_ofs);
} else {
@@ -336,6 +338,7 @@ void TextEdit::_update_scrollbars() {
h_scroll->set_max(total_width);
h_scroll->set_page(visible_width);
h_scroll->set_val(cursor.x_ofs);
+
} else {
h_scroll->hide();
@@ -706,7 +709,7 @@ void TextEdit::_notification(int p_what) {
if (in_region==-1 && !in_keyword && is_char && !prev_is_char) {
int to=j;
- while(_is_text_char(str[to]) && to<str.length())
+ while(to<str.length() && _is_text_char(str[to]))
to++;
uint32_t hash = String::hash(&str[j],to-j);
@@ -1091,16 +1094,16 @@ void TextEdit::backspace_at_cursor() {
}
-bool TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
-
- int row=p_mouse.y;
- row-=cache.style_normal->get_margin(MARGIN_TOP);
- row/=get_row_height();
-
- if (row<0 || row>=get_visible_rows())
- return false;
+void TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
- row+=cursor.line_ofs;
+ float rows=p_mouse.y;
+ rows-=cache.style_normal->get_margin(MARGIN_TOP);
+ rows/=get_row_height();
+ int row=cursor.line_ofs+rows;
+
+ if (row<0)
+ row=0;
+
int col=0;
if (row>=text.size()) {
@@ -1116,7 +1119,6 @@ bool TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) co
r_row=row;
r_col=col;
- return true;
}
void TextEdit::_input_event(const InputEvent& p_input_event) {
@@ -1174,8 +1176,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (mb.button_index==BUTTON_LEFT) {
int row,col;
- if (!_get_mouse_pos(Point2i(mb.x,mb.y), row,col))
- return;
+ _get_mouse_pos(Point2i(mb.x,mb.y), row,col);
int prev_col=cursor.column;
int prev_line=cursor.line;
@@ -1207,27 +1208,30 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
update();
} else {
- if (cursor.line<selection.from_line || (cursor.line==selection.from_line && cursor.column<=selection.from_column)) {
- selection.from_column=cursor.column;
- selection.from_line=cursor.line;
- } else if (cursor.line>selection.to_line || (cursor.line==selection.to_line && cursor.column>=selection.to_column)) {
- selection.to_column=cursor.column;
- selection.to_line=cursor.line;
-
- } else if (!selection.shiftclick_left) {
+ if (cursor.line<selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column<selection.selecting_column)) {
+ if (selection.shiftclick_left) {
+ SWAP(selection.from_column,selection.to_column);
+ SWAP(selection.from_line,selection.to_line);
+ selection.shiftclick_left = !selection.shiftclick_left;
+ }
selection.from_column=cursor.column;
selection.from_line=cursor.line;
- } else {
+ } else if (cursor.line>selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column>selection.selecting_column)) {
+
+ if (!selection.shiftclick_left) {
+ SWAP(selection.from_column,selection.to_column);
+ SWAP(selection.from_line,selection.to_line);
+ selection.shiftclick_left = !selection.shiftclick_left;
+ }
selection.to_column=cursor.column;
selection.to_line=cursor.line;
- }
- if (selection.from_line>selection.to_line || (selection.from_line==selection.to_line && selection.from_column>selection.to_column)) {
- SWAP(selection.from_column,selection.to_column);
- SWAP(selection.from_line,selection.to_line);
+ } else {
+ selection.active=false;
}
+
update();
}
@@ -1252,6 +1256,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec()-last_dblclk)<600 && cursor.line==prev_line) {
//tripleclick select line
select(cursor.line,0,cursor.line,text[cursor.line].length());
+ selection.selecting_column=0;
last_dblclk=0;
} else if (mb.doubleclick && text[cursor.line].length()) {
@@ -1276,6 +1281,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
end+=1;
select(cursor.line,beg,cursor.line,end);
+
+ selection.selecting_column=beg;
}
last_dblclk = OS::get_singleton()->get_ticks_msec();
@@ -1286,7 +1293,6 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
} else {
- selection.selecting_mode=Selection::MODE_NONE;
// notify to show soft keyboard
notification(NOTIFICATION_FOCUS_ENTER);
}
@@ -1299,10 +1305,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (mm.button_mask&BUTTON_MASK_LEFT) {
int row,col;
- if (!_get_mouse_pos(Point2i(mm.x,mm.y), row,col))
- return;
+ _get_mouse_pos(Point2i(mm.x,mm.y), row,col);
- if (selection.selecting_mode==Selection::MODE_POINTER) {
+ if (selection.selecting_mode!=Selection::MODE_NONE) {
select(selection.selecting_line,selection.selecting_column,row,col);
@@ -1582,7 +1587,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
break;
}
- selection.selecting_test=false;
+ selection.selecting_text=false;
bool scancode_handled=true;
@@ -1644,8 +1649,60 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
case KEY_BACKSPACE: {
if (readonly)
break;
- backspace_at_cursor();
-
+
+#ifdef APPLE_STYLE_KEYS
+ if (k.mod.alt) {
+#else
+ if (k.mod.alt) {
+ scancode_handled=false;
+ break;
+ } else if (k.mod.command) {
+#endif
+ int line=cursor.line;
+ int column=cursor.column;
+
+ bool prev_char=false;
+ bool only_whitespace=true;
+
+ while (only_whitespace && line > 0) {
+
+ while (column>0) {
+ CharType c=text[line][column-1];
+
+ if (c != '\t' && c != ' ') {
+ only_whitespace=false;
+ break;
+ }
+
+ column--;
+ }
+
+ if (only_whitespace) {
+ line--;
+ column=text[line].length();
+ }
+ }
+
+ while (column>0) {
+ bool ischar=_is_text_char(text[line][column-1]);
+
+ if (prev_char && !ischar)
+ break;
+
+ prev_char=ischar;
+ column--;
+
+ }
+
+ _remove_text(line, column, cursor.line, cursor.column);
+
+ cursor_set_line(line);
+ cursor_set_column(column);
+
+ } else {
+ backspace_at_cursor();
+ }
+
} break;
case KEY_LEFT: {
@@ -1786,10 +1843,63 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (cursor.line==text.size()-1 && cursor.column==curline_len)
break; //nothing to do
- int next_line = cursor.column<curline_len?cursor.line:cursor.line+1;
- int next_column = cursor.column<curline_len?(cursor.column+1):0;
+ int next_line=cursor.column<curline_len?cursor.line:cursor.line+1;
+ int next_column;
+
+#ifdef APPLE_STYLE_KEYS
+ if (k.mod.alt) {
+#else
+ if (k.mod.alt) {
+ scancode_handled=false;
+ break;
+ } else if (k.mod.command) {
+#endif
+ int last_line=text.size()-1;
+
+ int line=cursor.line;
+ int column=cursor.column;
+
+ bool prev_char=false;
+ bool only_whitespace=true;
+
+ while (only_whitespace && line < last_line) {
+
+ while (column<text[line].length()) {
+ CharType c=text[line][column];
+
+ if (c != '\t' && c != ' ') {
+ only_whitespace=false;
+ break;
+ }
+
+ column++;
+ }
+
+ if (only_whitespace) {
+ line++;
+ column=0;
+ }
+ }
+
+ while (column<text[line].length()) {
+
+ bool ischar=_is_text_char(text[line][column]);
+
+ if (prev_char && !ischar)
+ break;
+ prev_char=ischar;
+ column++;
+ }
+
+ next_line=line;
+ next_column=column;
+ } else {
+ next_column=cursor.column<curline_len?(cursor.column+1):0;
+ }
+
_remove_text(cursor.line,cursor.column,next_line,next_column);
update();
+
} break;
#ifdef APPLE_STYLE_KEYS
case KEY_HOME: {
@@ -1900,15 +2010,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
break;
}
- if (text.size()==1 && text[0].length()==0)
- break;
- selection.active=true;
- selection.from_line=0;
- selection.from_column=0;
- selection.to_line=text.size()-1;
- selection.to_column=text[selection.to_line].length();
- selection.selecting_mode=Selection::MODE_NONE;
- update();
+ select_all();
} break;
case KEY_X: {
@@ -2093,12 +2195,6 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
}
-
- if (!selection.selecting_test) {
-
- selection.selecting_mode=Selection::MODE_NONE;
- }
-
return;
} break;
@@ -2110,13 +2206,14 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
void TextEdit::_pre_shift_selection() {
- if (!selection.active || selection.selecting_mode!=Selection::MODE_SHIFT) {
+ if (!selection.active || selection.selecting_mode==Selection::MODE_NONE) {
selection.selecting_line=cursor.line;
selection.selecting_column=cursor.column;
selection.active=true;
- selection.selecting_mode=Selection::MODE_SHIFT;
}
+
+ selection.selecting_mode=Selection::MODE_SHIFT;
}
void TextEdit::_post_shift_selection() {
@@ -2129,7 +2226,7 @@ void TextEdit::_post_shift_selection() {
}
- selection.selecting_test=true;
+ selection.selecting_text=true;
}
/**** TEXT EDIT CORE API ****/
@@ -2425,7 +2522,7 @@ void TextEdit::adjust_viewport_to_cursor() {
}
-void TextEdit::cursor_set_column(int p_col) {
+void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
if (p_col<0)
p_col=0;
@@ -2436,7 +2533,8 @@ void TextEdit::cursor_set_column(int p_col) {
cursor.last_fit_x=get_column_x_offset(cursor.column,get_line(cursor.line));
- adjust_viewport_to_cursor();
+ if (p_adjust_viewport)
+ adjust_viewport_to_cursor();
if (!cursor_changed_dirty) {
if (is_inside_tree())
@@ -2447,7 +2545,7 @@ void TextEdit::cursor_set_column(int p_col) {
}
-void TextEdit::cursor_set_line(int p_row) {
+void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport) {
if (setting_row)
return;
@@ -2463,8 +2561,8 @@ void TextEdit::cursor_set_line(int p_row) {
cursor.line=p_row;
cursor.column=get_char_pos_for( cursor.last_fit_x, get_line( cursor.line) );
-
- adjust_viewport_to_cursor();
+ if (p_adjust_viewport)
+ adjust_viewport_to_cursor();
setting_row=false;
@@ -2829,9 +2927,14 @@ void TextEdit::select_all() {
selection.active=true;
selection.from_line=0;
selection.from_column=0;
+ selection.selecting_line=0;
+ selection.selecting_column=0;
selection.to_line=text.size()-1;
selection.to_column=text[selection.to_line].length();
- selection.selecting_mode=Selection::MODE_NONE;
+ selection.selecting_mode=Selection::MODE_SHIFT;
+ selection.shiftclick_left=true;
+ cursor_set_line( selection.to_line, false );
+ cursor_set_column( selection.to_column, false );
update();
}
@@ -2870,12 +2973,20 @@ void TextEdit::select(int p_from_line,int p_from_column,int p_to_line,int p_to_c
} else if (selection.from_column>selection.to_column) {
+ selection.shiftclick_left = false;
SWAP( selection.from_column, selection.to_column );
+ } else {
+
+ selection.shiftclick_left = true;
}
} else if (selection.from_line>selection.to_line) {
+ selection.shiftclick_left = false;
SWAP( selection.from_line, selection.to_line );
SWAP( selection.from_column, selection.to_column );
+ } else {
+
+ selection.shiftclick_left = true;
}
@@ -3499,10 +3610,8 @@ String TextEdit::get_tooltip(const Point2& p_pos) const {
if (!tooltip_obj)
return Control::get_tooltip(p_pos);
int row,col;
- if (!_get_mouse_pos(p_pos, row,col)) {
- return Control::get_tooltip(p_pos);
- }
-
+ _get_mouse_pos(p_pos, row, col);
+
String s = text[row];
if (s.length()==0)
return Control::get_tooltip(p_pos);
@@ -3565,7 +3674,10 @@ void TextEdit::set_show_line_numbers(bool p_show) {
update();
}
+bool TextEdit::is_text_field() const {
+ return true;
+}
void TextEdit::_bind_methods() {
@@ -3678,7 +3790,7 @@ TextEdit::TextEdit() {
selection.selecting_mode=Selection::MODE_NONE;
selection.selecting_line=0;
selection.selecting_column=0;
- selection.selecting_test=false;
+ selection.selecting_text=false;
selection.active=false;
syntax_coloring=false;