mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 08:32:43 +00:00 
			
		
		
		
	TextEditor: Show the current cursor position in the statusbar.
This commit is contained in:
		
							parent
							
								
									9158de6c41
								
							
						
					
					
						commit
						ca65ca2f2d
					
				
					 3 changed files with 31 additions and 20 deletions
				
			
		|  | @ -25,6 +25,10 @@ int main(int argc, char** argv) | |||
|     auto* text_editor = new GTextEditor(widget); | ||||
|     auto* statusbar = new GStatusBar(widget); | ||||
| 
 | ||||
|     text_editor->on_cursor_change = [statusbar] (GTextEditor& editor) { | ||||
|         statusbar->set_text(String::format("Line: %d, Column: %d", editor.cursor().line(), editor.cursor().column())); | ||||
|     }; | ||||
| 
 | ||||
|     { | ||||
|         StringBuilder builder; | ||||
|         int fd = open("/home/anon/ReadMe.md", O_RDONLY); | ||||
|  |  | |||
|  | @ -7,7 +7,7 @@ | |||
| GTextEditor::GTextEditor(GWidget* parent) | ||||
|     : GWidget(parent) | ||||
| { | ||||
|     set_font(GFontDatabase::the().get_by_name("Liza Thin")); | ||||
|     set_font(GFontDatabase::the().get_by_name("Csilla Thin")); | ||||
| 
 | ||||
|     set_fill_with_background_color(false); | ||||
| 
 | ||||
|  | @ -48,7 +48,7 @@ void GTextEditor::set_text(const String& text) | |||
|             add_line(i); | ||||
|     } | ||||
|     add_line(i); | ||||
|     m_cursor = GTextPosition(0, 0); | ||||
|     set_cursor(0, 0); | ||||
|     update(); | ||||
| } | ||||
| 
 | ||||
|  | @ -116,35 +116,31 @@ void GTextEditor::keydown_event(GKeyEvent& event) | |||
| { | ||||
|     if (event.key() == KeyCode::Key_Up) { | ||||
|         if (m_cursor.line() > 0) { | ||||
|             update_cursor(); | ||||
|             m_cursor.set_line(m_cursor.line() - 1); | ||||
|             m_cursor.set_column(min(m_cursor.column(), m_lines[m_cursor.line()].length())); | ||||
|             update_cursor(); | ||||
|             int new_line = m_cursor.line() - 1; | ||||
|             int new_column = min(m_cursor.column(), m_lines[new_line].length()); | ||||
|             set_cursor(new_line, new_column); | ||||
|         } | ||||
|         return; | ||||
|     } | ||||
|     if (event.key() == KeyCode::Key_Down) { | ||||
|         if (m_cursor.line() < (m_lines.size() - 1)) { | ||||
|             update_cursor(); | ||||
|             m_cursor.set_line(m_cursor.line() + 1); | ||||
|             m_cursor.set_column(min(m_cursor.column(), m_lines[m_cursor.line()].length())); | ||||
|             update_cursor(); | ||||
|             int new_line = m_cursor.line() + 1; | ||||
|             int new_column = min(m_cursor.column(), m_lines[new_line].length()); | ||||
|             set_cursor(new_line, new_column); | ||||
|         } | ||||
|         return; | ||||
|     } | ||||
|     if (event.key() == KeyCode::Key_Left) { | ||||
|         if (m_cursor.column() > 0) { | ||||
|             update_cursor(); | ||||
|             m_cursor.set_column(m_cursor.column() - 1); | ||||
|             update_cursor(); | ||||
|             int new_column = m_cursor.column() - 1; | ||||
|             set_cursor(m_cursor.line(), new_column); | ||||
|         } | ||||
|         return; | ||||
|     } | ||||
|     if (event.key() == KeyCode::Key_Right) { | ||||
|         if (m_cursor.column() < (m_lines[m_cursor.line()].length())) { | ||||
|             update_cursor(); | ||||
|             m_cursor.set_column(m_cursor.column() + 1); | ||||
|             update_cursor(); | ||||
|             int new_column = m_cursor.column() + 1; | ||||
|             set_cursor(m_cursor.line(), new_column); | ||||
|         } | ||||
|         return; | ||||
|     } | ||||
|  | @ -210,6 +206,17 @@ void GTextEditor::update_cursor() | |||
|     update(); | ||||
| } | ||||
| 
 | ||||
| void GTextEditor::set_cursor(int line, int column) | ||||
| { | ||||
|     if (m_cursor.line() == line && m_cursor.column() == column) | ||||
|         return; | ||||
|     update_cursor(); | ||||
|     m_cursor = GTextPosition(line, column); | ||||
|     update_cursor(); | ||||
|     if (on_cursor_change) | ||||
|         on_cursor_change(*this); | ||||
| } | ||||
| 
 | ||||
| void GTextEditor::Line::set_text(const String& text) | ||||
| { | ||||
|     if (text == m_text) | ||||
|  |  | |||
|  | @ -33,16 +33,15 @@ public: | |||
|     explicit GTextEditor(GWidget* parent); | ||||
|     virtual ~GTextEditor() override; | ||||
| 
 | ||||
|     Function<void(GTextEditor&)> on_cursor_change; | ||||
| 
 | ||||
|     void set_text(const String&); | ||||
| 
 | ||||
|     int content_width() const; | ||||
| 
 | ||||
|     Rect visible_content_rect() const; | ||||
|     void scroll_into_view(const GTextPosition&, Orientation); | ||||
| 
 | ||||
|     int line_count() const { return m_lines.size(); } | ||||
| 
 | ||||
|     int padding() const { return 2; } | ||||
|     GTextPosition cursor() const { return m_cursor; } | ||||
| 
 | ||||
| private: | ||||
|     virtual void paint_event(GPaintEvent&) override; | ||||
|  | @ -55,6 +54,7 @@ private: | |||
|     Rect line_content_rect(int item_index) const; | ||||
|     Rect cursor_content_rect() const; | ||||
|     void update_cursor(); | ||||
|     void set_cursor(int line, int column); | ||||
| 
 | ||||
|     GScrollBar* m_vertical_scrollbar { nullptr }; | ||||
|     GScrollBar* m_horizontal_scrollbar { nullptr }; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling