summaryrefslogtreecommitdiff
path: root/scene/gui/text_edit.cpp
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2015-11-19 00:30:39 -0300
committerJuan Linietsky <reduzio@gmail.com>2015-11-19 00:30:39 -0300
commitb6084a8a623e7af1d323b5284ac8f670a4c2e5d0 (patch)
treebd25e07f92bceff2f6824cfc8b3ac82e481fd799 /scene/gui/text_edit.cpp
parent9a7769f194cdc5f585b090f29a950dbb466b2940 (diff)
parentb70e2b754d44eb360c6485029afc589c4476bfe8 (diff)
Merge pull request #2364 from neikeq/textedit_select
Some TextEdit selection improvements
Diffstat (limited to 'scene/gui/text_edit.cpp')
-rw-r--r--scene/gui/text_edit.cpp119
1 files changed, 60 insertions, 59 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index d081e84df4..5415484009 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -1094,16 +1094,16 @@ void TextEdit::backspace_at_cursor() {
}
-bool TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
+void 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;
-
- 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()) {
@@ -1119,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) {
@@ -1177,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;
@@ -1210,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();
}
@@ -1255,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()) {
@@ -1279,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();
@@ -1289,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);
}
@@ -1302,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);
@@ -1585,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;
@@ -2008,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: {
@@ -2201,12 +2195,6 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
}
-
- if (!selection.selecting_test) {
-
- selection.selecting_mode=Selection::MODE_NONE;
- }
-
return;
} break;
@@ -2218,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() {
@@ -2237,7 +2226,7 @@ void TextEdit::_post_shift_selection() {
}
- selection.selecting_test=true;
+ selection.selecting_text=true;
}
/**** TEXT EDIT CORE API ****/
@@ -2533,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;
@@ -2544,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())
@@ -2555,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;
@@ -2571,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;
@@ -2937,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();
}
@@ -2978,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;
}
@@ -3607,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);
@@ -3789,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;