2020-01-18 09:38:21 +01:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
*
* 1. Redistributions of source code must retain the above copyright notice , this
* list of conditions and the following disclaimer .
*
* 2. Redistributions in binary form must reproduce the above copyright notice ,
* this list of conditions and the following disclaimer in the documentation
* and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
* AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL
* DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
* OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2019-07-11 13:52:33 -05:00
# include "TextEditorWidget.h"
2020-09-14 12:51:12 +02:00
# include <AK/JsonObject.h>
# include <AK/JsonValue.h>
2019-07-13 19:58:04 -05:00
# include <AK/Optional.h>
2019-07-11 13:52:33 -05:00
# include <AK/StringBuilder.h>
2019-12-19 20:20:20 +01:00
# include <AK/URL.h>
2020-12-23 01:12:27 +01:00
# include <Applications/TextEditor/TextEditorWindowGML.h>
2020-02-06 15:04:03 +01:00
# include <LibCore/File.h>
2020-02-14 13:18:34 +01:00
# include <LibCore/MimeData.h>
2020-08-11 15:13:07 +02:00
# include <LibDesktop/Launcher.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/Action.h>
2020-03-11 03:02:37 +02:00
# include <LibGUI/ActionGroup.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/BoxLayout.h>
# include <LibGUI/Button.h>
2020-02-07 20:12:25 +01:00
# include <LibGUI/CppSyntaxHighlighter.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/FilePicker.h>
2020-12-30 18:04:55 +01:00
# include <LibGUI/FontPicker.h>
2020-12-21 13:59:21 +01:00
# include <LibGUI/GMLSyntaxHighlighter.h>
2020-05-01 01:57:06 +03:00
# include <LibGUI/INISyntaxHighlighter.h>
2020-03-13 00:53:22 +02:00
# include <LibGUI/JSSyntaxHighlighter.h>
2020-02-15 01:56:30 +01:00
# include <LibGUI/Menu.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/MenuBar.h>
# include <LibGUI/MessageBox.h>
2021-01-02 11:59:55 +01:00
# include <LibGUI/RegularEditingEngine.h>
2020-09-28 14:28:44 +03:30
# include <LibGUI/ShellSyntaxHighlighter.h>
2020-04-28 21:42:45 +02:00
# include <LibGUI/Splitter.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/StatusBar.h>
# include <LibGUI/TextBox.h>
# include <LibGUI/TextEditor.h>
# include <LibGUI/ToolBar.h>
2020-04-23 17:44:49 +02:00
# include <LibGUI/ToolBarContainer.h>
2021-01-02 11:59:55 +01:00
# include <LibGUI/VimEditingEngine.h>
2020-02-16 09:17:49 +01:00
# include <LibGfx/Font.h>
2020-04-28 21:42:45 +02:00
# include <LibMarkdown/Document.h>
2020-10-08 21:12:10 +01:00
# include <LibWeb/OutOfProcessWebView.h>
2020-03-08 12:05:14 +01:00
# include <string.h>
2019-07-11 13:52:33 -05:00
TextEditorWidget : : TextEditorWidget ( )
{
2020-12-23 01:12:27 +01:00
load_from_gml ( text_editor_window_gml ) ;
2019-07-11 13:52:33 -05:00
2021-01-01 00:57:48 -07:00
auto & toolbar = * find_descendant_of_type_named < GUI : : ToolBar > ( " toolbar " ) ;
2020-04-28 21:42:45 +02:00
2021-01-01 00:57:48 -07:00
m_editor = * find_descendant_of_type_named < GUI : : TextEditor > ( " editor " ) ;
2019-07-11 13:52:33 -05:00
m_editor - > set_ruler_visible ( true ) ;
m_editor - > set_automatic_indentation_enabled ( true ) ;
2019-08-27 17:05:01 +02:00
m_editor - > set_line_wrapping_enabled ( true ) ;
2021-01-02 11:59:55 +01:00
m_editor - > set_editing_engine ( make < GUI : : RegularEditingEngine > ( ) ) ;
2019-08-21 21:30:20 +02:00
2019-08-27 20:18:19 +02:00
m_editor - > on_change = [ this ] {
2020-07-04 21:19:01 +02:00
update_preview ( ) ;
2020-06-26 22:47:29 +02:00
2020-04-28 22:10:01 +01:00
// Do not mark as dirty on the first change (When document is first opened.)
2019-12-04 22:57:54 -08:00
if ( m_document_opening ) {
m_document_opening = false ;
return ;
}
2019-08-27 20:18:19 +02:00
bool was_dirty = m_document_dirty ;
m_document_dirty = true ;
if ( ! was_dirty )
update_title ( ) ;
} ;
2021-01-01 00:57:48 -07:00
m_page_view = * find_descendant_of_type_named < Web : : OutOfProcessWebView > ( " webview " ) ;
2020-08-29 13:44:25 +02:00
m_page_view - > on_link_hover = [ this ] ( auto & url ) {
2020-08-11 15:32:35 +02:00
if ( url . is_valid ( ) )
m_statusbar - > set_text ( url . to_string ( ) ) ;
else
update_statusbar_cursor_position ( ) ;
} ;
2020-08-11 15:13:07 +02:00
m_page_view - > on_link_click = [ & ] ( auto & url , auto & , unsigned ) {
if ( ! Desktop : : Launcher : : open ( url ) ) {
GUI : : MessageBox : : show (
window ( ) ,
2020-10-06 19:25:44 +02:00
String : : formatted ( " The link to '{}' could not be opened. " , url ) ,
2020-08-11 15:13:07 +02:00
" Failed to open link " ,
GUI : : MessageBox : : Type : : Error ) ;
}
} ;
2020-04-28 21:42:45 +02:00
2021-01-01 00:57:48 -07:00
m_find_replace_widget = * find_descendant_of_type_named < GUI : : Widget > ( " find_replace_widget " ) ;
2020-01-11 20:52:47 +02:00
2021-01-01 00:57:48 -07:00
m_find_widget = * find_descendant_of_type_named < GUI : : Widget > ( " find_widget " ) ;
2019-08-21 21:30:20 +02:00
2021-01-01 00:57:48 -07:00
m_replace_widget = * find_descendant_of_type_named < GUI : : Widget > ( " replace_widget " ) ;
2020-01-11 20:52:47 +02:00
2020-02-23 10:57:42 +01:00
m_find_textbox = m_find_widget - > add < GUI : : TextBox > ( ) ;
m_replace_textbox = m_replace_widget - > add < GUI : : TextBox > ( ) ;
2019-08-21 21:30:20 +02:00
2020-04-20 21:54:16 +00:00
m_find_next_action = GUI : : Action : : create ( " Find next " , { Mod_Ctrl , Key_G } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/find-next.png " ) , [ & ] ( auto & ) {
2019-08-21 21:30:20 +02:00
auto needle = m_find_textbox - > text ( ) ;
2019-10-15 02:05:45 -04:00
if ( needle . is_empty ( ) ) {
2020-10-06 19:25:44 +02:00
dbgln ( " find_next( \" \" ) " ) ;
2019-10-15 02:05:45 -04:00
return ;
}
2020-04-23 20:29:38 +02:00
if ( m_find_use_regex )
m_editor - > document ( ) . update_regex_matches ( needle ) ;
auto found_range = m_editor - > document ( ) . find_next ( needle , m_editor - > normalized_selection ( ) . end ( ) , GUI : : TextDocument : : SearchShouldWrap : : Yes , m_find_use_regex ) ;
2021-01-09 00:11:15 +01:00
dbgln ( " find_next('{}') returned {} " , needle , found_range ) ;
2019-08-24 12:09:35 -06:00
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
} else {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 19:25:44 +02:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2019-08-24 12:09:35 -06:00
" Not found " ,
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : Type : : Information ) ;
2019-08-24 12:09:35 -06:00
}
2019-08-25 21:35:58 +02:00
} ) ;
2020-01-11 20:52:47 +02:00
2021-01-02 11:58:37 +01:00
m_find_regex_action = GUI : : Action : : create ( " Find regex " , { Mod_Ctrl | Mod_Shift , Key_R } , [ & ] ( auto & ) {
2020-04-23 20:29:38 +02:00
m_find_regex_button - > set_checked ( ! m_find_regex_button - > is_checked ( ) ) ;
m_find_use_regex = m_find_regex_button - > is_checked ( ) ;
} ) ;
m_find_previous_action = GUI : : Action : : create ( " Find previous " , { Mod_Ctrl | Mod_Shift , Key_G } , [ & ] ( auto & ) {
2019-08-24 12:09:35 -06:00
auto needle = m_find_textbox - > text ( ) ;
2019-10-15 02:05:45 -04:00
if ( needle . is_empty ( ) ) {
2020-10-06 19:25:44 +02:00
dbgln ( " find_prev( \" \" ) " ) ;
2019-10-15 02:05:45 -04:00
return ;
}
2019-08-24 12:09:35 -06:00
auto selection_start = m_editor - > normalized_selection ( ) . start ( ) ;
if ( ! selection_start . is_valid ( ) )
selection_start = m_editor - > normalized_selection ( ) . end ( ) ;
2020-04-23 20:29:38 +02:00
if ( m_find_use_regex )
m_editor - > document ( ) . update_regex_matches ( needle ) ;
auto found_range = m_editor - > document ( ) . find_previous ( needle , selection_start , GUI : : TextDocument : : SearchShouldWrap : : Yes , m_find_use_regex ) ;
2019-08-25 12:23:34 +02:00
2020-10-06 19:25:44 +02:00
dbgln ( " find_prev( \" {} \" ) returned {} " , needle , found_range ) ;
2019-08-21 21:30:20 +02:00
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
} else {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 19:25:44 +02:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2019-08-21 21:30:20 +02:00
" Not found " ,
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : Type : : Information ) ;
2019-08-21 21:30:20 +02:00
}
2019-08-25 21:35:58 +02:00
} ) ;
2020-02-02 15:07:41 +01:00
m_replace_next_action = GUI : : Action : : create ( " Replace next " , { Mod_Ctrl , Key_F1 } , [ & ] ( auto & ) {
2020-01-11 20:52:47 +02:00
auto needle = m_find_textbox - > text ( ) ;
auto substitute = m_replace_textbox - > text ( ) ;
if ( needle . is_empty ( ) )
return ;
auto selection_start = m_editor - > normalized_selection ( ) . start ( ) ;
if ( ! selection_start . is_valid ( ) )
selection_start = m_editor - > normalized_selection ( ) . start ( ) ;
2020-04-23 20:29:38 +02:00
if ( m_find_use_regex )
m_editor - > document ( ) . update_regex_matches ( needle ) ;
auto found_range = m_editor - > document ( ) . find_next ( needle , selection_start , GUI : : TextDocument : : SearchShouldWrap : : Yes , m_find_use_regex ) ;
2020-01-11 20:52:47 +02:00
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
m_editor - > insert_at_cursor_or_replace_selection ( substitute ) ;
} else {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 19:25:44 +02:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2020-01-11 20:52:47 +02:00
" Not found " ,
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : Type : : Information ) ;
2020-01-11 20:52:47 +02:00
}
} ) ;
2020-02-02 15:07:41 +01:00
m_replace_previous_action = GUI : : Action : : create ( " Replace previous " , { Mod_Ctrl | Mod_Shift , Key_F1 } , [ & ] ( auto & ) {
2020-01-11 20:52:47 +02:00
auto needle = m_find_textbox - > text ( ) ;
auto substitute = m_replace_textbox - > text ( ) ;
if ( needle . is_empty ( ) )
return ;
auto selection_start = m_editor - > normalized_selection ( ) . start ( ) ;
if ( ! selection_start . is_valid ( ) )
selection_start = m_editor - > normalized_selection ( ) . start ( ) ;
2020-04-23 20:29:38 +02:00
if ( m_find_use_regex )
m_editor - > document ( ) . update_regex_matches ( needle ) ;
2020-01-11 20:52:47 +02:00
auto found_range = m_editor - > document ( ) . find_previous ( needle , selection_start ) ;
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
m_editor - > insert_at_cursor_or_replace_selection ( substitute ) ;
} else {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 19:25:44 +02:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2020-01-11 20:52:47 +02:00
" Not found " ,
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : Type : : Information ) ;
2020-01-11 20:52:47 +02:00
}
} ) ;
2020-02-02 15:07:41 +01:00
m_replace_all_action = GUI : : Action : : create ( " Replace all " , { Mod_Ctrl , Key_F2 } , [ & ] ( auto & ) {
2020-01-11 20:52:47 +02:00
auto needle = m_find_textbox - > text ( ) ;
auto substitute = m_replace_textbox - > text ( ) ;
if ( needle . is_empty ( ) )
return ;
2020-04-23 20:29:38 +02:00
if ( m_find_use_regex )
m_editor - > document ( ) . update_regex_matches ( needle ) ;
2020-01-11 20:52:47 +02:00
2020-04-23 20:29:38 +02:00
auto found_range = m_editor - > document ( ) . find_next ( needle , { } , GUI : : TextDocument : : SearchShouldWrap : : Yes , m_find_use_regex ) ;
2020-02-14 13:18:34 +01:00
while ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
m_editor - > insert_at_cursor_or_replace_selection ( substitute ) ;
2020-04-23 20:29:38 +02:00
found_range = m_editor - > document ( ) . find_next ( needle , { } , GUI : : TextDocument : : SearchShouldWrap : : Yes , m_find_use_regex ) ;
2020-01-11 20:52:47 +02:00
}
} ) ;
2021-01-01 00:57:48 -07:00
m_find_previous_button = * find_descendant_of_type_named < GUI : : Button > ( " find_previous_button " ) ;
2019-08-25 21:35:58 +02:00
m_find_previous_button - > set_action ( * m_find_previous_action ) ;
2021-01-01 00:57:48 -07:00
m_find_next_button = * find_descendant_of_type_named < GUI : : Button > ( " find_next_button " ) ;
2019-08-25 21:35:58 +02:00
m_find_next_button - > set_action ( * m_find_next_action ) ;
2019-08-21 21:30:20 +02:00
2019-08-22 11:09:25 +02:00
m_find_textbox - > on_return_pressed = [ this ] {
2019-08-24 12:09:35 -06:00
m_find_next_button - > click ( ) ;
2019-08-22 11:09:25 +02:00
} ;
2020-04-23 20:29:38 +02:00
m_find_regex_button = m_find_widget - > add < GUI : : Button > ( " .* " ) ;
2020-12-30 01:23:32 +01:00
m_find_regex_button - > set_fixed_width ( 20 ) ;
2020-04-23 20:29:38 +02:00
m_find_regex_button - > set_action ( * m_find_regex_action ) ;
2019-08-22 11:09:25 +02:00
m_find_textbox - > on_escape_pressed = [ this ] {
2020-01-11 20:52:47 +02:00
m_find_replace_widget - > set_visible ( false ) ;
m_editor - > set_focus ( true ) ;
} ;
2021-01-01 00:57:48 -07:00
m_replace_previous_button = * find_descendant_of_type_named < GUI : : Button > ( " replace_previous_button " ) ;
2020-01-11 20:52:47 +02:00
m_replace_previous_button - > set_action ( * m_replace_previous_action ) ;
2021-01-01 00:57:48 -07:00
m_replace_next_button = * find_descendant_of_type_named < GUI : : Button > ( " replace_next_button " ) ;
2020-01-11 20:52:47 +02:00
m_replace_next_button - > set_action ( * m_replace_next_action ) ;
2021-01-01 00:57:48 -07:00
m_replace_all_button = * find_descendant_of_type_named < GUI : : Button > ( " replace_all_button " ) ;
2020-01-11 20:52:47 +02:00
m_replace_all_button - > set_action ( * m_replace_all_action ) ;
m_replace_textbox - > on_return_pressed = [ this ] {
m_replace_next_button - > click ( ) ;
} ;
m_replace_textbox - > on_escape_pressed = [ this ] {
m_find_replace_widget - > set_visible ( false ) ;
2019-08-22 11:09:25 +02:00
m_editor - > set_focus ( true ) ;
} ;
2021-01-02 11:59:55 +01:00
m_vim_emulation_setting_action = GUI : : Action : : create_checkable ( " Vim emulation " , { Mod_Ctrl | Mod_Shift | Mod_Alt , Key_V } , [ & ] ( auto & action ) {
if ( action . is_checked ( ) )
m_editor - > set_editing_engine ( make < GUI : : VimEditingEngine > ( ) ) ;
else
m_editor - > set_editing_engine ( make < GUI : : RegularEditingEngine > ( ) ) ;
} ) ;
m_vim_emulation_setting_action - > set_checked ( false ) ;
2020-02-06 13:39:17 +01:00
m_find_replace_action = GUI : : Action : : create ( " Find/Replace... " , { Mod_Ctrl , Key_F } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/find.png " ) , [ this ] ( auto & ) {
2020-01-11 20:52:47 +02:00
m_find_replace_widget - > set_visible ( true ) ;
2019-08-22 11:09:25 +02:00
m_find_widget - > set_visible ( true ) ;
2020-01-11 20:52:47 +02:00
m_replace_widget - > set_visible ( true ) ;
2019-08-22 11:02:03 +02:00
m_find_textbox - > set_focus ( true ) ;
2020-01-11 21:08:35 +02:00
if ( m_editor - > has_selection ( ) ) {
auto selected_text = m_editor - > document ( ) . text_in_range ( m_editor - > normalized_selection ( ) ) ;
m_find_textbox - > set_text ( selected_text ) ;
}
2019-08-25 21:44:59 +02:00
m_find_textbox - > select_all ( ) ;
2019-08-22 11:02:03 +02:00
} ) ;
2020-01-11 20:52:47 +02:00
m_editor - > add_custom_context_menu_action ( * m_find_replace_action ) ;
2019-08-25 21:38:13 +02:00
m_editor - > add_custom_context_menu_action ( * m_find_next_action ) ;
m_editor - > add_custom_context_menu_action ( * m_find_previous_action ) ;
2021-01-01 00:57:48 -07:00
m_statusbar = * find_descendant_of_type_named < GUI : : StatusBar > ( " statusbar " ) ;
2019-07-11 13:52:33 -05:00
2020-08-29 13:44:25 +02:00
m_editor - > on_cursor_change = [ this ] { update_statusbar_cursor_position ( ) ; } ;
2019-07-11 13:52:33 -05:00
2020-02-06 11:56:38 +01:00
m_new_action = GUI : : Action : : create ( " New " , { Mod_Ctrl , Key_N } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/new.png " ) , [ this ] ( const GUI : : Action & ) {
2019-09-05 23:05:28 -06:00
if ( m_document_dirty ) {
2020-12-26 00:47:49 +01:00
auto save_document_first_result = GUI : : MessageBox : : show ( window ( ) , " Save changes to current document first? " , " Warning " , GUI : : MessageBox : : Type : : Warning , GUI : : MessageBox : : InputType : : YesNoCancel ) ;
2020-02-17 00:06:11 -05:00
if ( save_document_first_result = = GUI : : Dialog : : ExecResult : : ExecYes )
m_save_action - > activate ( ) ;
if ( save_document_first_result = = GUI : : Dialog : : ExecResult : : ExecCancel )
2019-09-14 22:10:44 +02:00
return ;
2019-09-05 23:05:28 -06:00
}
2019-09-14 22:10:44 +02:00
2019-09-05 23:05:28 -06:00
m_document_dirty = false ;
m_editor - > set_text ( StringView ( ) ) ;
2020-05-26 14:52:44 +03:00
set_path ( LexicalPath ( ) ) ;
2019-09-05 23:05:28 -06:00
update_title ( ) ;
2019-07-11 13:52:33 -05:00
} ) ;
2020-02-02 15:07:41 +01:00
m_open_action = GUI : : CommonActions : : make_open_action ( [ this ] ( auto & ) {
2020-07-15 19:52:02 -06:00
Optional < String > open_path = GUI : : FilePicker : : get_open_filepath ( window ( ) ) ;
2019-07-11 13:52:33 -05:00
2019-07-28 23:45:50 -05:00
if ( ! open_path . has_value ( ) )
2019-07-13 19:58:04 -05:00
return ;
2019-12-21 23:20:43 +01:00
if ( m_document_dirty ) {
2020-12-26 00:47:49 +01:00
auto save_document_first_result = GUI : : MessageBox : : show ( window ( ) , " Save changes to current document first? " , " Warning " , GUI : : MessageBox : : Type : : Warning , GUI : : MessageBox : : InputType : : YesNoCancel ) ;
2020-02-17 00:06:11 -05:00
if ( save_document_first_result = = GUI : : Dialog : : ExecResult : : ExecYes )
2019-12-21 23:20:43 +01:00
m_save_action - > activate ( ) ;
2020-02-17 00:06:11 -05:00
if ( save_document_first_result = = GUI : : Dialog : : ExecResult : : ExecCancel )
return ;
2019-12-21 23:20:43 +01:00
}
2019-07-28 23:45:50 -05:00
open_sesame ( open_path . value ( ) ) ;
2019-07-11 13:52:33 -05:00
} ) ;
2020-11-01 21:32:27 +00:00
m_save_as_action = GUI : : CommonActions : : make_save_as_action ( [ & ] ( auto & ) {
2020-07-15 19:52:02 -06:00
Optional < String > save_path = GUI : : FilePicker : : get_save_filepath ( window ( ) , m_name . is_null ( ) ? " Untitled " : m_name , m_extension . is_null ( ) ? " txt " : m_extension ) ;
2019-07-28 23:45:50 -05:00
if ( ! save_path . has_value ( ) )
2019-07-13 19:58:04 -05:00
return ;
2019-07-28 23:45:50 -05:00
if ( ! m_editor - > write_to_file ( save_path . value ( ) ) ) {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) , " Unable to save file. \n " , " Error " , GUI : : MessageBox : : Type : : Error ) ;
2019-07-13 19:58:04 -05:00
return ;
}
2019-08-27 20:18:19 +02:00
m_document_dirty = false ;
2020-05-26 14:52:44 +03:00
set_path ( LexicalPath ( save_path . value ( ) ) ) ;
2020-10-06 19:25:44 +02:00
dbgln ( " Wrote document to {} " , save_path . value ( ) ) ;
2019-07-24 06:32:30 +02:00
} ) ;
2020-11-01 21:32:27 +00:00
m_save_action = GUI : : CommonActions : : make_save_action ( [ & ] ( auto & ) {
2019-07-24 06:32:30 +02:00
if ( ! m_path . is_empty ( ) ) {
2019-08-27 20:18:19 +02:00
if ( ! m_editor - > write_to_file ( m_path ) ) {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) , " Unable to save file. \n " , " Error " , GUI : : MessageBox : : Type : : Error ) ;
2019-08-27 20:18:19 +02:00
} else {
m_document_dirty = false ;
update_title ( ) ;
}
2019-07-24 06:32:30 +02:00
return ;
}
2019-07-25 23:48:39 -05:00
m_save_as_action - > activate ( ) ;
2019-07-11 13:52:33 -05:00
} ) ;
2020-04-21 17:19:27 +02:00
m_line_wrapping_setting_action = GUI : : Action : : create_checkable ( " Line wrapping " , [ & ] ( auto & action ) {
2019-08-25 12:23:34 +02:00
m_editor - > set_line_wrapping_enabled ( action . is_checked ( ) ) ;
} ) ;
m_line_wrapping_setting_action - > set_checked ( m_editor - > is_line_wrapping_enabled ( ) ) ;
2020-04-21 16:01:00 +02:00
auto menubar = GUI : : MenuBar : : construct ( ) ;
2020-04-04 12:18:40 +02:00
auto & app_menu = menubar - > add_menu ( " Text Editor " ) ;
app_menu . add_action ( * m_new_action ) ;
app_menu . add_action ( * m_open_action ) ;
app_menu . add_action ( * m_save_action ) ;
app_menu . add_action ( * m_save_as_action ) ;
app_menu . add_separator ( ) ;
app_menu . add_action ( GUI : : CommonActions : : make_quit_action ( [ this ] ( auto & ) {
2019-08-27 20:37:41 +02:00
if ( ! request_close ( ) )
return ;
2020-07-04 16:52:01 +02:00
GUI : : Application : : the ( ) - > quit ( ) ;
2019-07-11 13:52:33 -05:00
} ) ) ;
2020-04-04 12:18:40 +02:00
auto & edit_menu = menubar - > add_menu ( " Edit " ) ;
edit_menu . add_action ( m_editor - > undo_action ( ) ) ;
edit_menu . add_action ( m_editor - > redo_action ( ) ) ;
edit_menu . add_separator ( ) ;
edit_menu . add_action ( m_editor - > cut_action ( ) ) ;
edit_menu . add_action ( m_editor - > copy_action ( ) ) ;
edit_menu . add_action ( m_editor - > paste_action ( ) ) ;
edit_menu . add_action ( m_editor - > delete_action ( ) ) ;
edit_menu . add_separator ( ) ;
2021-01-02 11:59:55 +01:00
edit_menu . add_action ( * m_vim_emulation_setting_action ) ;
edit_menu . add_separator ( ) ;
2020-04-04 12:18:40 +02:00
edit_menu . add_action ( * m_find_replace_action ) ;
edit_menu . add_action ( * m_find_next_action ) ;
2020-04-23 20:29:38 +02:00
edit_menu . add_action ( * m_find_regex_action ) ;
2020-04-04 12:18:40 +02:00
edit_menu . add_action ( * m_find_previous_action ) ;
edit_menu . add_action ( * m_replace_next_action ) ;
edit_menu . add_action ( * m_replace_previous_action ) ;
edit_menu . add_action ( * m_replace_all_action ) ;
2020-07-04 21:19:01 +02:00
m_no_preview_action = GUI : : Action : : create_checkable (
" No preview " , [ this ] ( auto & ) {
set_preview_mode ( PreviewMode : : None ) ;
} ) ;
2020-04-29 11:48:11 +02:00
m_markdown_preview_action = GUI : : Action : : create_checkable (
2020-07-04 21:19:01 +02:00
" Markdown preview " , [ this ] ( auto & ) {
set_preview_mode ( PreviewMode : : Markdown ) ;
2020-04-29 11:48:11 +02:00
} ,
this ) ;
2020-06-26 22:47:29 +02:00
m_html_preview_action = GUI : : Action : : create_checkable (
2020-07-04 21:19:01 +02:00
" HTML preview " , [ this ] ( auto & ) {
set_preview_mode ( PreviewMode : : HTML ) ;
2020-06-26 22:47:29 +02:00
} ,
this ) ;
2020-07-04 21:19:01 +02:00
m_preview_actions . add_action ( * m_no_preview_action ) ;
2020-06-26 22:47:29 +02:00
m_preview_actions . add_action ( * m_markdown_preview_action ) ;
m_preview_actions . add_action ( * m_html_preview_action ) ;
m_preview_actions . set_exclusive ( true ) ;
2020-04-29 11:48:11 +02:00
auto & view_menu = menubar - > add_menu ( " View " ) ;
2020-12-30 18:04:55 +01:00
view_menu . add_action ( GUI : : Action : : create ( " Editor font... " , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-font-editor.png " ) ,
[ & ] ( auto & ) {
auto picker = GUI : : FontPicker : : construct ( window ( ) , & m_editor - > font ( ) , true ) ;
if ( picker - > exec ( ) = = GUI : : Dialog : : ExecOK ) {
dbgln ( " setting font {} " , picker - > font ( ) - > qualified_name ( ) ) ;
m_editor - > set_font ( picker - > font ( ) ) ;
}
} ) ) ;
view_menu . add_separator ( ) ;
2020-04-29 11:48:11 +02:00
view_menu . add_action ( * m_line_wrapping_setting_action ) ;
view_menu . add_separator ( ) ;
2020-07-04 21:19:01 +02:00
view_menu . add_action ( * m_no_preview_action ) ;
2020-04-29 11:48:11 +02:00
view_menu . add_action ( * m_markdown_preview_action ) ;
2020-06-26 22:47:29 +02:00
view_menu . add_action ( * m_html_preview_action ) ;
2020-04-29 11:48:11 +02:00
view_menu . add_separator ( ) ;
2020-03-11 03:02:37 +02:00
syntax_actions . set_exclusive ( true ) ;
2020-04-29 11:48:11 +02:00
auto & syntax_menu = view_menu . add_submenu ( " Syntax " ) ;
2020-04-21 17:19:27 +02:00
m_plain_text_highlight = GUI : : Action : : create_checkable ( " Plain text " , [ & ] ( auto & ) {
2021-01-10 16:29:28 -07:00
m_editor - > set_syntax_highlighter ( { } ) ;
2020-03-11 03:02:37 +02:00
m_editor - > update ( ) ;
} ) ;
m_plain_text_highlight - > set_checked ( true ) ;
syntax_actions . add_action ( * m_plain_text_highlight ) ;
2020-04-04 12:18:40 +02:00
syntax_menu . add_action ( * m_plain_text_highlight ) ;
2020-03-11 03:02:37 +02:00
2020-04-21 17:19:27 +02:00
m_cpp_highlight = GUI : : Action : : create_checkable ( " C++ " , [ & ] ( auto & ) {
2020-03-11 03:02:37 +02:00
m_editor - > set_syntax_highlighter ( make < GUI : : CppSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
syntax_actions . add_action ( * m_cpp_highlight ) ;
2020-04-04 12:18:40 +02:00
syntax_menu . add_action ( * m_cpp_highlight ) ;
2020-03-11 03:02:37 +02:00
2020-04-21 17:19:27 +02:00
m_js_highlight = GUI : : Action : : create_checkable ( " JavaScript " , [ & ] ( auto & ) {
2020-03-13 00:53:22 +02:00
m_editor - > set_syntax_highlighter ( make < GUI : : JSSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
syntax_actions . add_action ( * m_js_highlight ) ;
2020-04-04 12:18:40 +02:00
syntax_menu . add_action ( * m_js_highlight ) ;
2020-03-13 00:53:22 +02:00
2020-12-21 13:59:21 +01:00
m_gml_highlight = GUI : : Action : : create_checkable ( " GML " , [ & ] ( auto & ) {
m_editor - > set_syntax_highlighter ( make < GUI : : GMLSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
syntax_actions . add_action ( * m_gml_highlight ) ;
syntax_menu . add_action ( * m_gml_highlight ) ;
2020-05-01 01:57:06 +03:00
m_ini_highlight = GUI : : Action : : create_checkable ( " INI File " , [ & ] ( auto & ) {
m_editor - > set_syntax_highlighter ( make < GUI : : IniSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
syntax_actions . add_action ( * m_ini_highlight ) ;
syntax_menu . add_action ( * m_ini_highlight ) ;
2020-09-28 14:28:44 +03:30
m_shell_highlight = GUI : : Action : : create_checkable ( " Shell File " , [ & ] ( auto & ) {
m_editor - > set_syntax_highlighter ( make < GUI : : ShellSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
syntax_actions . add_action ( * m_shell_highlight ) ;
syntax_menu . add_action ( * m_shell_highlight ) ;
2020-04-04 12:18:40 +02:00
auto & help_menu = menubar - > add_menu ( " Help " ) ;
2021-01-04 23:51:49 +01:00
help_menu . add_action ( GUI : : CommonActions : : make_about_action ( " Text Editor " , GUI : : Icon : : default_icon ( " app-text-editor " ) , window ( ) ) ) ;
2019-07-11 13:52:33 -05:00
2020-07-04 16:52:01 +02:00
GUI : : Application : : the ( ) - > set_menubar ( move ( menubar ) ) ;
2019-07-11 13:52:33 -05:00
2020-03-04 19:07:55 +01:00
toolbar . add_action ( * m_new_action ) ;
toolbar . add_action ( * m_open_action ) ;
toolbar . add_action ( * m_save_action ) ;
2019-07-11 13:52:33 -05:00
2020-03-04 19:07:55 +01:00
toolbar . add_separator ( ) ;
2019-07-11 13:52:33 -05:00
2020-03-04 19:07:55 +01:00
toolbar . add_action ( m_editor - > cut_action ( ) ) ;
toolbar . add_action ( m_editor - > copy_action ( ) ) ;
toolbar . add_action ( m_editor - > paste_action ( ) ) ;
toolbar . add_action ( m_editor - > delete_action ( ) ) ;
2019-07-11 13:52:33 -05:00
2020-03-04 19:07:55 +01:00
toolbar . add_separator ( ) ;
2019-07-11 13:52:33 -05:00
2020-03-04 19:07:55 +01:00
toolbar . add_action ( m_editor - > undo_action ( ) ) ;
toolbar . add_action ( m_editor - > redo_action ( ) ) ;
2019-07-11 13:52:33 -05:00
}
TextEditorWidget : : ~ TextEditorWidget ( )
{
}
2020-05-26 14:52:44 +03:00
void TextEditorWidget : : set_path ( const LexicalPath & lexical_path )
2019-07-24 06:32:30 +02:00
{
2020-05-26 14:52:44 +03:00
m_path = lexical_path . string ( ) ;
m_name = lexical_path . title ( ) ;
m_extension = lexical_path . extension ( ) ;
2020-02-07 20:12:25 +01:00
2020-08-11 15:13:07 +02:00
if ( m_extension = = " c " | | m_extension = = " cc " | | m_extension = = " cxx " | | m_extension = = " cpp " | | m_extension = = " h " ) {
2020-03-11 03:02:37 +02:00
m_cpp_highlight - > activate ( ) ;
2020-05-09 16:48:28 +02:00
} else if ( m_extension = = " js " | | m_extension = = " json " ) {
2020-03-13 00:53:22 +02:00
m_js_highlight - > activate ( ) ;
2020-12-21 13:59:21 +01:00
} else if ( m_extension = = " gml " ) {
m_gml_highlight - > activate ( ) ;
2020-05-01 01:57:06 +03:00
} else if ( m_extension = = " ini " ) {
m_ini_highlight - > activate ( ) ;
2020-04-28 21:42:45 +02:00
} else {
2020-03-11 03:02:37 +02:00
m_plain_text_highlight - > activate ( ) ;
2020-04-28 21:42:45 +02:00
}
2020-07-06 18:48:49 +04:30
if ( m_auto_detect_preview_mode ) {
if ( m_extension = = " md " )
set_preview_mode ( PreviewMode : : Markdown ) ;
else if ( m_extension = = " html " )
set_preview_mode ( PreviewMode : : HTML ) ;
else
set_preview_mode ( PreviewMode : : None ) ;
}
2020-02-07 20:12:25 +01:00
2019-08-27 20:18:19 +02:00
update_title ( ) ;
}
void TextEditorWidget : : update_title ( )
{
2019-07-24 06:32:30 +02:00
StringBuilder builder ;
2020-12-30 03:44:38 +01:00
if ( m_path . is_empty ( ) )
builder . append ( " Untitled " ) ;
else
builder . append ( m_path ) ;
2019-08-27 20:18:19 +02:00
if ( m_document_dirty )
builder . append ( " (*) " ) ;
2020-03-13 23:21:35 +01:00
builder . append ( " - Text Editor " ) ;
2019-07-24 06:32:30 +02:00
window ( ) - > set_title ( builder . to_string ( ) ) ;
}
2019-07-11 13:52:33 -05:00
void TextEditorWidget : : open_sesame ( const String & path )
{
2020-02-02 12:34:39 +01:00
auto file = Core : : File : : construct ( path ) ;
2020-06-04 22:15:10 +02:00
if ( ! file - > open ( Core : : IODevice : : ReadOnly ) & & file - > error ( ) ! = ENOENT ) {
2020-10-06 19:25:44 +02:00
GUI : : MessageBox : : show ( window ( ) , String : : formatted ( " Opening \" {} \" failed: {} " , path , strerror ( errno ) ) , " Error " , GUI : : MessageBox : : Type : : Error ) ;
2019-08-23 19:10:14 +02:00
return ;
2019-07-11 13:52:33 -05:00
}
2019-09-21 20:50:06 +02:00
m_editor - > set_text ( file - > read_all ( ) ) ;
2019-12-04 22:57:54 -08:00
m_document_dirty = false ;
m_document_opening = true ;
2020-05-26 14:52:44 +03:00
set_path ( LexicalPath ( path ) ) ;
2020-01-23 21:29:59 +01:00
m_editor - > set_focus ( true ) ;
2019-07-16 21:32:10 +02:00
}
2019-08-27 20:37:41 +02:00
bool TextEditorWidget : : request_close ( )
{
if ( ! m_document_dirty )
return true ;
2020-07-15 20:45:11 -06:00
auto result = GUI : : MessageBox : : show ( window ( ) , " The document has been modified. Would you like to save? " , " Unsaved changes " , GUI : : MessageBox : : Type : : Warning , GUI : : MessageBox : : InputType : : YesNoCancel ) ;
2020-02-17 00:06:11 -05:00
2020-03-10 15:23:28 +02:00
if ( result = = GUI : : MessageBox : : ExecYes ) {
2020-02-17 00:06:11 -05:00
m_save_action - > activate ( ) ;
2020-03-10 15:23:28 +02:00
return true ;
}
2020-02-17 00:06:11 -05:00
if ( result = = GUI : : MessageBox : : ExecNo )
return true ;
return false ;
2019-08-27 20:37:41 +02:00
}
2019-12-19 20:20:20 +01:00
2020-02-02 15:07:41 +01:00
void TextEditorWidget : : drop_event ( GUI : : DropEvent & event )
2019-12-19 20:20:20 +01:00
{
event . accept ( ) ;
window ( ) - > move_to_front ( ) ;
2020-02-14 13:18:34 +01:00
if ( event . mime_data ( ) . has_urls ( ) ) {
auto urls = event . mime_data ( ) . urls ( ) ;
2020-02-16 09:17:49 +01:00
if ( urls . is_empty ( ) )
2019-12-19 20:20:20 +01:00
return ;
2020-02-14 13:18:34 +01:00
if ( urls . size ( ) > 1 ) {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) , " TextEditor can only open one file at a time! " , " One at a time please! " , GUI : : MessageBox : : Type : : Error ) ;
2019-12-19 20:20:20 +01:00
return ;
}
2020-02-14 13:18:34 +01:00
open_sesame ( urls . first ( ) . path ( ) ) ;
2019-12-19 20:20:20 +01:00
}
}
2020-04-28 21:42:45 +02:00
2020-07-04 21:19:01 +02:00
void TextEditorWidget : : set_preview_mode ( PreviewMode mode )
2020-06-26 22:47:29 +02:00
{
2020-07-04 21:19:01 +02:00
if ( m_preview_mode = = mode )
2020-06-26 22:47:29 +02:00
return ;
2020-07-04 21:19:01 +02:00
m_preview_mode = mode ;
if ( m_preview_mode = = PreviewMode : : HTML ) {
m_html_preview_action - > set_checked ( true ) ;
m_page_view - > set_visible ( true ) ;
2020-06-26 22:47:29 +02:00
update_html_preview ( ) ;
2020-07-04 21:19:01 +02:00
} else if ( m_preview_mode = = PreviewMode : : Markdown ) {
m_markdown_preview_action - > set_checked ( true ) ;
m_page_view - > set_visible ( true ) ;
update_markdown_preview ( ) ;
} else {
m_no_preview_action - > set_checked ( true ) ;
m_page_view - > set_visible ( false ) ;
}
2020-06-26 22:47:29 +02:00
}
2020-07-04 21:19:01 +02:00
void TextEditorWidget : : update_preview ( )
2020-04-28 21:42:45 +02:00
{
2020-07-04 21:19:01 +02:00
switch ( m_preview_mode ) {
case PreviewMode : : Markdown :
2020-04-28 21:42:45 +02:00
update_markdown_preview ( ) ;
2020-07-04 21:19:01 +02:00
break ;
case PreviewMode : : HTML :
update_html_preview ( ) ;
break ;
default :
break ;
}
2020-04-28 21:42:45 +02:00
}
void TextEditorWidget : : update_markdown_preview ( )
{
2020-05-11 13:55:31 -04:00
auto document = Markdown : : Document : : parse ( m_editor - > text ( ) ) ;
if ( document ) {
auto html = document - > render_to_html ( ) ;
2020-07-20 22:22:07 +10:00
auto current_scroll_pos = m_page_view - > visible_content_rect ( ) ;
2020-06-21 21:52:51 +02:00
m_page_view - > load_html ( html , URL : : create_with_file_protocol ( m_path ) ) ;
2020-07-20 22:22:07 +10:00
m_page_view - > scroll_into_view ( current_scroll_pos , true , true ) ;
2020-04-28 21:42:45 +02:00
}
}
2020-06-26 22:47:29 +02:00
void TextEditorWidget : : update_html_preview ( )
{
2020-07-20 22:22:07 +10:00
auto current_scroll_pos = m_page_view - > visible_content_rect ( ) ;
2020-06-26 22:47:29 +02:00
m_page_view - > load_html ( m_editor - > text ( ) , URL : : create_with_file_protocol ( m_path ) ) ;
2020-07-20 22:22:07 +10:00
m_page_view - > scroll_into_view ( current_scroll_pos , true , true ) ;
2020-06-26 22:47:29 +02:00
}
2020-08-29 13:44:25 +02:00
void TextEditorWidget : : update_statusbar_cursor_position ( )
{
StringBuilder builder ;
2020-10-06 19:25:44 +02:00
builder . appendff ( " Line: {}, Column: {} " , m_editor - > cursor ( ) . line ( ) + 1 , m_editor - > cursor ( ) . column ( ) ) ;
2020-08-29 13:44:25 +02:00
m_statusbar - > set_text ( builder . to_string ( ) ) ;
}