2020-01-18 11:38:21 +03: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 21:52:33 +03:00
# include "TextEditorWidget.h"
2020-09-14 13:51:12 +03:00
# include <AK/JsonObject.h>
# include <AK/JsonValue.h>
2019-07-14 03:58:04 +03:00
# include <AK/Optional.h>
2019-07-11 21:52:33 +03:00
# include <AK/StringBuilder.h>
2019-12-19 22:20:20 +03:00
# include <AK/URL.h>
2020-12-23 03:12:27 +03:00
# include <Applications/TextEditor/TextEditorWindowGML.h>
2020-02-06 17:04:03 +03:00
# include <LibCore/File.h>
2020-02-14 15:18:34 +03:00
# include <LibCore/MimeData.h>
2020-08-11 16:13:07 +03:00
# include <LibDesktop/Launcher.h>
2020-02-06 22:33:02 +03:00
# include <LibGUI/AboutDialog.h>
# include <LibGUI/Action.h>
2020-03-11 04:02:37 +03:00
# include <LibGUI/ActionGroup.h>
2020-02-06 22:33:02 +03:00
# include <LibGUI/BoxLayout.h>
# include <LibGUI/Button.h>
2020-02-07 22:12:25 +03:00
# include <LibGUI/CppSyntaxHighlighter.h>
2020-02-06 22:33:02 +03:00
# include <LibGUI/FilePicker.h>
2020-12-30 20:04:55 +03:00
# include <LibGUI/FontPicker.h>
2020-12-21 15:59:21 +03:00
# include <LibGUI/GMLSyntaxHighlighter.h>
2020-05-01 01:57:06 +03:00
# include <LibGUI/INISyntaxHighlighter.h>
2020-03-13 01:53:22 +03:00
# include <LibGUI/JSSyntaxHighlighter.h>
2020-02-15 03:56:30 +03:00
# include <LibGUI/Menu.h>
2020-02-06 22:33:02 +03:00
# include <LibGUI/MenuBar.h>
# include <LibGUI/MessageBox.h>
2020-09-28 13:58:44 +03:00
# include <LibGUI/ShellSyntaxHighlighter.h>
2020-04-28 22:42:45 +03:00
# include <LibGUI/Splitter.h>
2020-02-06 22:33:02 +03:00
# include <LibGUI/StatusBar.h>
# include <LibGUI/TextBox.h>
# include <LibGUI/TextEditor.h>
# include <LibGUI/ToolBar.h>
2020-04-23 18:44:49 +03:00
# include <LibGUI/ToolBarContainer.h>
2020-02-16 11:17:49 +03:00
# include <LibGfx/Font.h>
2020-04-28 22:42:45 +03:00
# include <LibMarkdown/Document.h>
2020-10-08 23:12:10 +03:00
# include <LibWeb/OutOfProcessWebView.h>
2020-03-08 14:05:14 +03:00
# include <string.h>
2019-07-11 21:52:33 +03:00
TextEditorWidget : : TextEditorWidget ( )
{
2020-12-23 03:12:27 +03:00
load_from_gml ( text_editor_window_gml ) ;
2019-07-11 21:52:33 +03:00
2021-01-01 10:57:48 +03:00
auto & toolbar = * find_descendant_of_type_named < GUI : : ToolBar > ( " toolbar " ) ;
2020-04-28 22:42:45 +03:00
2021-01-01 10:57:48 +03:00
m_editor = * find_descendant_of_type_named < GUI : : TextEditor > ( " editor " ) ;
2019-07-11 21:52:33 +03:00
m_editor - > set_ruler_visible ( true ) ;
m_editor - > set_automatic_indentation_enabled ( true ) ;
2019-08-27 18:05:01 +03:00
m_editor - > set_line_wrapping_enabled ( true ) ;
2019-08-21 22:30:20 +03:00
2019-08-27 21:18:19 +03:00
m_editor - > on_change = [ this ] {
2020-07-04 22:19:01 +03:00
update_preview ( ) ;
2020-06-26 23:47:29 +03:00
2020-04-29 00:10:01 +03:00
// Do not mark as dirty on the first change (When document is first opened.)
2019-12-05 09:57:54 +03:00
if ( m_document_opening ) {
m_document_opening = false ;
return ;
}
2019-08-27 21:18:19 +03:00
bool was_dirty = m_document_dirty ;
m_document_dirty = true ;
if ( ! was_dirty )
update_title ( ) ;
} ;
2021-01-01 10:57:48 +03:00
m_page_view = * find_descendant_of_type_named < Web : : OutOfProcessWebView > ( " webview " ) ;
2020-08-29 14:44:25 +03:00
m_page_view - > on_link_hover = [ this ] ( auto & url ) {
2020-08-11 16:32:35 +03:00
if ( url . is_valid ( ) )
m_statusbar - > set_text ( url . to_string ( ) ) ;
else
update_statusbar_cursor_position ( ) ;
} ;
2020-08-11 16:13:07 +03:00
m_page_view - > on_link_click = [ & ] ( auto & url , auto & , unsigned ) {
if ( ! Desktop : : Launcher : : open ( url ) ) {
GUI : : MessageBox : : show (
window ( ) ,
2020-10-06 20:25:44 +03:00
String : : formatted ( " The link to '{}' could not be opened. " , url ) ,
2020-08-11 16:13:07 +03:00
" Failed to open link " ,
GUI : : MessageBox : : Type : : Error ) ;
}
} ;
2020-04-28 22:42:45 +03:00
2021-01-01 10:57:48 +03:00
m_find_replace_widget = * find_descendant_of_type_named < GUI : : Widget > ( " find_replace_widget " ) ;
2020-01-11 21:52:47 +03:00
2021-01-01 10:57:48 +03:00
m_find_widget = * find_descendant_of_type_named < GUI : : Widget > ( " find_widget " ) ;
2019-08-21 22:30:20 +03:00
2021-01-01 10:57:48 +03:00
m_replace_widget = * find_descendant_of_type_named < GUI : : Widget > ( " replace_widget " ) ;
2020-01-11 21:52:47 +03:00
2020-02-23 12:57:42 +03:00
m_find_textbox = m_find_widget - > add < GUI : : TextBox > ( ) ;
m_replace_textbox = m_replace_widget - > add < GUI : : TextBox > ( ) ;
2019-08-21 22:30:20 +03:00
2020-04-21 00:54:16 +03: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 22:30:20 +03:00
auto needle = m_find_textbox - > text ( ) ;
2019-10-15 09:05:45 +03:00
if ( needle . is_empty ( ) ) {
2020-10-06 20:25:44 +03:00
dbgln ( " find_next( \" \" ) " ) ;
2019-10-15 09:05:45 +03:00
return ;
}
2020-04-23 21:29:38 +03: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 ) ;
dbg ( ) < < " find_next( \" " < < needle < < " \" ) returned " < < found_range ;
2019-08-24 21:09:35 +03:00
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
} else {
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 20:25:44 +03:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2019-08-24 21:09:35 +03:00
" Not found " ,
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : Type : : Information ) ;
2019-08-24 21:09:35 +03:00
}
2019-08-25 22:35:58 +03:00
} ) ;
2020-01-11 21:52:47 +03:00
2020-04-23 21:29:38 +03:00
m_find_regex_action = GUI : : Action : : create ( " Find regex " , { Mod_Ctrl , Key_R } , [ & ] ( auto & ) {
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 21:09:35 +03:00
auto needle = m_find_textbox - > text ( ) ;
2019-10-15 09:05:45 +03:00
if ( needle . is_empty ( ) ) {
2020-10-06 20:25:44 +03:00
dbgln ( " find_prev( \" \" ) " ) ;
2019-10-15 09:05:45 +03:00
return ;
}
2019-08-24 21:09:35 +03:00
auto selection_start = m_editor - > normalized_selection ( ) . start ( ) ;
if ( ! selection_start . is_valid ( ) )
selection_start = m_editor - > normalized_selection ( ) . end ( ) ;
2020-04-23 21:29:38 +03: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 13:23:34 +03:00
2020-10-06 20:25:44 +03:00
dbgln ( " find_prev( \" {} \" ) returned {} " , needle , found_range ) ;
2019-08-21 22:30:20 +03:00
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
} else {
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 20:25:44 +03:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2019-08-21 22:30:20 +03:00
" Not found " ,
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : Type : : Information ) ;
2019-08-21 22:30:20 +03:00
}
2019-08-25 22:35:58 +03:00
} ) ;
2020-02-02 17:07:41 +03:00
m_replace_next_action = GUI : : Action : : create ( " Replace next " , { Mod_Ctrl , Key_F1 } , [ & ] ( auto & ) {
2020-01-11 21:52:47 +03: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 21:29:38 +03: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 21:52:47 +03:00
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
m_editor - > insert_at_cursor_or_replace_selection ( substitute ) ;
} else {
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 20:25:44 +03:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2020-01-11 21:52:47 +03:00
" Not found " ,
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : Type : : Information ) ;
2020-01-11 21:52:47 +03:00
}
} ) ;
2020-02-02 17:07:41 +03:00
m_replace_previous_action = GUI : : Action : : create ( " Replace previous " , { Mod_Ctrl | Mod_Shift , Key_F1 } , [ & ] ( auto & ) {
2020-01-11 21:52:47 +03: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 21:29:38 +03:00
if ( m_find_use_regex )
m_editor - > document ( ) . update_regex_matches ( needle ) ;
2020-01-11 21:52:47 +03: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-16 05:45:11 +03:00
GUI : : MessageBox : : show ( window ( ) ,
2020-10-06 20:25:44 +03:00
String : : formatted ( " Not found: \" {} \" " , needle ) ,
2020-01-11 21:52:47 +03:00
" Not found " ,
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : Type : : Information ) ;
2020-01-11 21:52:47 +03:00
}
} ) ;
2020-02-02 17:07:41 +03:00
m_replace_all_action = GUI : : Action : : create ( " Replace all " , { Mod_Ctrl , Key_F2 } , [ & ] ( auto & ) {
2020-01-11 21:52:47 +03:00
auto needle = m_find_textbox - > text ( ) ;
auto substitute = m_replace_textbox - > text ( ) ;
if ( needle . is_empty ( ) )
return ;
2020-04-23 21:29:38 +03:00
if ( m_find_use_regex )
m_editor - > document ( ) . update_regex_matches ( needle ) ;
2020-01-11 21:52:47 +03:00
2020-04-23 21:29:38 +03:00
auto found_range = m_editor - > document ( ) . find_next ( needle , { } , GUI : : TextDocument : : SearchShouldWrap : : Yes , m_find_use_regex ) ;
2020-02-14 15:18:34 +03:00
while ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
m_editor - > insert_at_cursor_or_replace_selection ( substitute ) ;
2020-04-23 21:29:38 +03:00
found_range = m_editor - > document ( ) . find_next ( needle , { } , GUI : : TextDocument : : SearchShouldWrap : : Yes , m_find_use_regex ) ;
2020-01-11 21:52:47 +03:00
}
} ) ;
2021-01-01 10:57:48 +03:00
m_find_previous_button = * find_descendant_of_type_named < GUI : : Button > ( " find_previous_button " ) ;
2019-08-25 22:35:58 +03:00
m_find_previous_button - > set_action ( * m_find_previous_action ) ;
2021-01-01 10:57:48 +03:00
m_find_next_button = * find_descendant_of_type_named < GUI : : Button > ( " find_next_button " ) ;
2019-08-25 22:35:58 +03:00
m_find_next_button - > set_action ( * m_find_next_action ) ;
2019-08-21 22:30:20 +03:00
2019-08-22 12:09:25 +03:00
m_find_textbox - > on_return_pressed = [ this ] {
2019-08-24 21:09:35 +03:00
m_find_next_button - > click ( ) ;
2019-08-22 12:09:25 +03:00
} ;
2020-04-23 21:29:38 +03:00
m_find_regex_button = m_find_widget - > add < GUI : : Button > ( " .* " ) ;
2020-12-30 03:23:32 +03:00
m_find_regex_button - > set_fixed_width ( 20 ) ;
2020-04-23 21:29:38 +03:00
m_find_regex_button - > set_action ( * m_find_regex_action ) ;
2019-08-22 12:09:25 +03:00
m_find_textbox - > on_escape_pressed = [ this ] {
2020-01-11 21:52:47 +03:00
m_find_replace_widget - > set_visible ( false ) ;
m_editor - > set_focus ( true ) ;
} ;
2021-01-01 10:57:48 +03:00
m_replace_previous_button = * find_descendant_of_type_named < GUI : : Button > ( " replace_previous_button " ) ;
2020-01-11 21:52:47 +03:00
m_replace_previous_button - > set_action ( * m_replace_previous_action ) ;
2021-01-01 10:57:48 +03:00
m_replace_next_button = * find_descendant_of_type_named < GUI : : Button > ( " replace_next_button " ) ;
2020-01-11 21:52:47 +03:00
m_replace_next_button - > set_action ( * m_replace_next_action ) ;
2021-01-01 10:57:48 +03:00
m_replace_all_button = * find_descendant_of_type_named < GUI : : Button > ( " replace_all_button " ) ;
2020-01-11 21:52:47 +03: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 12:09:25 +03:00
m_editor - > set_focus ( true ) ;
} ;
2020-02-06 15:39:17 +03: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 21:52:47 +03:00
m_find_replace_widget - > set_visible ( true ) ;
2019-08-22 12:09:25 +03:00
m_find_widget - > set_visible ( true ) ;
2020-01-11 21:52:47 +03:00
m_replace_widget - > set_visible ( true ) ;
2019-08-22 12:02:03 +03:00
m_find_textbox - > set_focus ( true ) ;
2020-01-11 22:08:35 +03: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 22:44:59 +03:00
m_find_textbox - > select_all ( ) ;
2019-08-22 12:02:03 +03:00
} ) ;
2020-01-11 21:52:47 +03:00
m_editor - > add_custom_context_menu_action ( * m_find_replace_action ) ;
2019-08-25 22:38:13 +03: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 10:57:48 +03:00
m_statusbar = * find_descendant_of_type_named < GUI : : StatusBar > ( " statusbar " ) ;
2019-07-11 21:52:33 +03:00
2020-08-29 14:44:25 +03:00
m_editor - > on_cursor_change = [ this ] { update_statusbar_cursor_position ( ) ; } ;
2019-07-11 21:52:33 +03:00
2020-02-06 13:56:38 +03: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-06 08:05:28 +03:00
if ( m_document_dirty ) {
2020-12-26 02:47:49 +03: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 08:06:11 +03: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 23:10:44 +03:00
return ;
2019-09-06 08:05:28 +03:00
}
2019-09-14 23:10:44 +03:00
2019-09-06 08:05:28 +03:00
m_document_dirty = false ;
m_editor - > set_text ( StringView ( ) ) ;
2020-05-26 14:52:44 +03:00
set_path ( LexicalPath ( ) ) ;
2019-09-06 08:05:28 +03:00
update_title ( ) ;
2019-07-11 21:52:33 +03:00
} ) ;
2020-02-02 17:07:41 +03:00
m_open_action = GUI : : CommonActions : : make_open_action ( [ this ] ( auto & ) {
2020-07-16 04:52:02 +03:00
Optional < String > open_path = GUI : : FilePicker : : get_open_filepath ( window ( ) ) ;
2019-07-11 21:52:33 +03:00
2019-07-29 07:45:50 +03:00
if ( ! open_path . has_value ( ) )
2019-07-14 03:58:04 +03:00
return ;
2019-12-22 01:20:43 +03:00
if ( m_document_dirty ) {
2020-12-26 02:47:49 +03: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 08:06:11 +03:00
if ( save_document_first_result = = GUI : : Dialog : : ExecResult : : ExecYes )
2019-12-22 01:20:43 +03:00
m_save_action - > activate ( ) ;
2020-02-17 08:06:11 +03:00
if ( save_document_first_result = = GUI : : Dialog : : ExecResult : : ExecCancel )
return ;
2019-12-22 01:20:43 +03:00
}
2019-07-29 07:45:50 +03:00
open_sesame ( open_path . value ( ) ) ;
2019-07-11 21:52:33 +03:00
} ) ;
2020-11-02 00:32:27 +03:00
m_save_as_action = GUI : : CommonActions : : make_save_as_action ( [ & ] ( auto & ) {
2020-07-16 04:52:02 +03: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-29 07:45:50 +03:00
if ( ! save_path . has_value ( ) )
2019-07-14 03:58:04 +03:00
return ;
2019-07-29 07:45:50 +03:00
if ( ! m_editor - > write_to_file ( save_path . value ( ) ) ) {
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : show ( window ( ) , " Unable to save file. \n " , " Error " , GUI : : MessageBox : : Type : : Error ) ;
2019-07-14 03:58:04 +03:00
return ;
}
2019-08-27 21:18:19 +03:00
m_document_dirty = false ;
2020-05-26 14:52:44 +03:00
set_path ( LexicalPath ( save_path . value ( ) ) ) ;
2020-10-06 20:25:44 +03:00
dbgln ( " Wrote document to {} " , save_path . value ( ) ) ;
2019-07-24 07:32:30 +03:00
} ) ;
2020-11-02 00:32:27 +03:00
m_save_action = GUI : : CommonActions : : make_save_action ( [ & ] ( auto & ) {
2019-07-24 07:32:30 +03:00
if ( ! m_path . is_empty ( ) ) {
2019-08-27 21:18:19 +03:00
if ( ! m_editor - > write_to_file ( m_path ) ) {
2020-07-16 05:45:11 +03:00
GUI : : MessageBox : : show ( window ( ) , " Unable to save file. \n " , " Error " , GUI : : MessageBox : : Type : : Error ) ;
2019-08-27 21:18:19 +03:00
} else {
m_document_dirty = false ;
update_title ( ) ;
}
2019-07-24 07:32:30 +03:00
return ;
}
2019-07-26 07:48:39 +03:00
m_save_as_action - > activate ( ) ;
2019-07-11 21:52:33 +03:00
} ) ;
2020-04-21 18:19:27 +03:00
m_line_wrapping_setting_action = GUI : : Action : : create_checkable ( " Line wrapping " , [ & ] ( auto & action ) {
2019-08-25 13:23:34 +03: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 17:01:00 +03:00
auto menubar = GUI : : MenuBar : : construct ( ) ;
2020-04-04 13:18:40 +03: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 21:37:41 +03:00
if ( ! request_close ( ) )
return ;
2020-07-04 17:52:01 +03:00
GUI : : Application : : the ( ) - > quit ( ) ;
2019-07-11 21:52:33 +03:00
} ) ) ;
2020-04-04 13:18:40 +03: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 ( ) ;
edit_menu . add_action ( * m_find_replace_action ) ;
edit_menu . add_action ( * m_find_next_action ) ;
2020-04-23 21:29:38 +03:00
edit_menu . add_action ( * m_find_regex_action ) ;
2020-04-04 13:18:40 +03: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 22:19:01 +03:00
m_no_preview_action = GUI : : Action : : create_checkable (
" No preview " , [ this ] ( auto & ) {
set_preview_mode ( PreviewMode : : None ) ;
} ) ;
2020-04-29 12:48:11 +03:00
m_markdown_preview_action = GUI : : Action : : create_checkable (
2020-07-04 22:19:01 +03:00
" Markdown preview " , [ this ] ( auto & ) {
set_preview_mode ( PreviewMode : : Markdown ) ;
2020-04-29 12:48:11 +03:00
} ,
this ) ;
2020-06-26 23:47:29 +03:00
m_html_preview_action = GUI : : Action : : create_checkable (
2020-07-04 22:19:01 +03:00
" HTML preview " , [ this ] ( auto & ) {
set_preview_mode ( PreviewMode : : HTML ) ;
2020-06-26 23:47:29 +03:00
} ,
this ) ;
2020-07-04 22:19:01 +03:00
m_preview_actions . add_action ( * m_no_preview_action ) ;
2020-06-26 23:47:29 +03: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 12:48:11 +03:00
auto & view_menu = menubar - > add_menu ( " View " ) ;
2020-12-30 20:04:55 +03: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 12:48:11 +03:00
view_menu . add_action ( * m_line_wrapping_setting_action ) ;
view_menu . add_separator ( ) ;
2020-07-04 22:19:01 +03:00
view_menu . add_action ( * m_no_preview_action ) ;
2020-04-29 12:48:11 +03:00
view_menu . add_action ( * m_markdown_preview_action ) ;
2020-06-26 23:47:29 +03:00
view_menu . add_action ( * m_html_preview_action ) ;
2020-04-29 12:48:11 +03:00
view_menu . add_separator ( ) ;
2020-03-11 04:02:37 +03:00
syntax_actions . set_exclusive ( true ) ;
2020-04-29 12:48:11 +03:00
auto & syntax_menu = view_menu . add_submenu ( " Syntax " ) ;
2020-04-21 18:19:27 +03:00
m_plain_text_highlight = GUI : : Action : : create_checkable ( " Plain text " , [ & ] ( auto & ) {
2020-03-11 04:02:37 +03:00
m_editor - > set_syntax_highlighter ( nullptr ) ;
m_editor - > update ( ) ;
} ) ;
m_plain_text_highlight - > set_checked ( true ) ;
syntax_actions . add_action ( * m_plain_text_highlight ) ;
2020-04-04 13:18:40 +03:00
syntax_menu . add_action ( * m_plain_text_highlight ) ;
2020-03-11 04:02:37 +03:00
2020-04-21 18:19:27 +03:00
m_cpp_highlight = GUI : : Action : : create_checkable ( " C++ " , [ & ] ( auto & ) {
2020-03-11 04:02:37 +03:00
m_editor - > set_syntax_highlighter ( make < GUI : : CppSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
syntax_actions . add_action ( * m_cpp_highlight ) ;
2020-04-04 13:18:40 +03:00
syntax_menu . add_action ( * m_cpp_highlight ) ;
2020-03-11 04:02:37 +03:00
2020-04-21 18:19:27 +03:00
m_js_highlight = GUI : : Action : : create_checkable ( " JavaScript " , [ & ] ( auto & ) {
2020-03-13 01:53:22 +03:00
m_editor - > set_syntax_highlighter ( make < GUI : : JSSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
syntax_actions . add_action ( * m_js_highlight ) ;
2020-04-04 13:18:40 +03:00
syntax_menu . add_action ( * m_js_highlight ) ;
2020-03-13 01:53:22 +03:00
2020-12-21 15:59:21 +03: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 13:58:44 +03:00
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 13:18:40 +03:00
auto & help_menu = menubar - > add_menu ( " Help " ) ;
help_menu . add_action ( GUI : : Action : : create ( " About " , [ & ] ( auto & ) {
2020-07-04 20:41:43 +03:00
GUI : : AboutDialog : : show ( " Text Editor " , Gfx : : Bitmap : : load_from_file ( " /res/icons/32x32/app-text-editor.png " ) , window ( ) ) ;
2019-07-11 21:52:33 +03:00
} ) ) ;
2020-07-04 17:52:01 +03:00
GUI : : Application : : the ( ) - > set_menubar ( move ( menubar ) ) ;
2019-07-11 21:52:33 +03:00
2020-03-04 21:07:55 +03:00
toolbar . add_action ( * m_new_action ) ;
toolbar . add_action ( * m_open_action ) ;
toolbar . add_action ( * m_save_action ) ;
2019-07-11 21:52:33 +03:00
2020-03-04 21:07:55 +03:00
toolbar . add_separator ( ) ;
2019-07-11 21:52:33 +03:00
2020-03-04 21:07:55 +03: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 21:52:33 +03:00
2020-03-04 21:07:55 +03:00
toolbar . add_separator ( ) ;
2019-07-11 21:52:33 +03:00
2020-03-04 21:07:55 +03:00
toolbar . add_action ( m_editor - > undo_action ( ) ) ;
toolbar . add_action ( m_editor - > redo_action ( ) ) ;
2019-07-11 21:52:33 +03:00
}
TextEditorWidget : : ~ TextEditorWidget ( )
{
}
2020-05-26 14:52:44 +03:00
void TextEditorWidget : : set_path ( const LexicalPath & lexical_path )
2019-07-24 07:32:30 +03: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 22:12:25 +03:00
2020-08-11 16:13:07 +03:00
if ( m_extension = = " c " | | m_extension = = " cc " | | m_extension = = " cxx " | | m_extension = = " cpp " | | m_extension = = " h " ) {
2020-03-11 04:02:37 +03:00
m_cpp_highlight - > activate ( ) ;
2020-05-09 17:48:28 +03:00
} else if ( m_extension = = " js " | | m_extension = = " json " ) {
2020-03-13 01:53:22 +03:00
m_js_highlight - > activate ( ) ;
2020-12-21 15:59:21 +03: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 22:42:45 +03:00
} else {
2020-03-11 04:02:37 +03:00
m_plain_text_highlight - > activate ( ) ;
2020-04-28 22:42:45 +03:00
}
2020-07-06 17:18:49 +03:00
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 22:12:25 +03:00
2019-08-27 21:18:19 +03:00
update_title ( ) ;
}
void TextEditorWidget : : update_title ( )
{
2019-07-24 07:32:30 +03:00
StringBuilder builder ;
2020-12-30 05:44:38 +03:00
if ( m_path . is_empty ( ) )
builder . append ( " Untitled " ) ;
else
builder . append ( m_path ) ;
2019-08-27 21:18:19 +03:00
if ( m_document_dirty )
builder . append ( " (*) " ) ;
2020-03-14 01:21:35 +03:00
builder . append ( " - Text Editor " ) ;
2019-07-24 07:32:30 +03:00
window ( ) - > set_title ( builder . to_string ( ) ) ;
}
2019-07-11 21:52:33 +03:00
void TextEditorWidget : : open_sesame ( const String & path )
{
2020-02-02 14:34:39 +03:00
auto file = Core : : File : : construct ( path ) ;
2020-06-04 23:15:10 +03:00
if ( ! file - > open ( Core : : IODevice : : ReadOnly ) & & file - > error ( ) ! = ENOENT ) {
2020-10-06 20:25:44 +03:00
GUI : : MessageBox : : show ( window ( ) , String : : formatted ( " Opening \" {} \" failed: {} " , path , strerror ( errno ) ) , " Error " , GUI : : MessageBox : : Type : : Error ) ;
2019-08-23 20:10:14 +03:00
return ;
2019-07-11 21:52:33 +03:00
}
2019-09-21 21:50:06 +03:00
m_editor - > set_text ( file - > read_all ( ) ) ;
2019-12-05 09:57:54 +03:00
m_document_dirty = false ;
m_document_opening = true ;
2020-05-26 14:52:44 +03:00
set_path ( LexicalPath ( path ) ) ;
2020-01-23 23:29:59 +03:00
m_editor - > set_focus ( true ) ;
2019-07-16 22:32:10 +03:00
}
2019-08-27 21:37:41 +03:00
bool TextEditorWidget : : request_close ( )
{
if ( ! m_document_dirty )
return true ;
2020-07-16 05:45:11 +03: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 08:06:11 +03:00
2020-03-10 16:23:28 +03:00
if ( result = = GUI : : MessageBox : : ExecYes ) {
2020-02-17 08:06:11 +03:00
m_save_action - > activate ( ) ;
2020-03-10 16:23:28 +03:00
return true ;
}
2020-02-17 08:06:11 +03:00
if ( result = = GUI : : MessageBox : : ExecNo )
return true ;
return false ;
2019-08-27 21:37:41 +03:00
}
2019-12-19 22:20:20 +03:00
2020-02-02 17:07:41 +03:00
void TextEditorWidget : : drop_event ( GUI : : DropEvent & event )
2019-12-19 22:20:20 +03:00
{
event . accept ( ) ;
window ( ) - > move_to_front ( ) ;
2020-02-14 15:18:34 +03:00
if ( event . mime_data ( ) . has_urls ( ) ) {
auto urls = event . mime_data ( ) . urls ( ) ;
2020-02-16 11:17:49 +03:00
if ( urls . is_empty ( ) )
2019-12-19 22:20:20 +03:00
return ;
2020-02-14 15:18:34 +03:00
if ( urls . size ( ) > 1 ) {
2020-07-16 05:45:11 +03: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 22:20:20 +03:00
return ;
}
2020-02-14 15:18:34 +03:00
open_sesame ( urls . first ( ) . path ( ) ) ;
2019-12-19 22:20:20 +03:00
}
}
2020-04-28 22:42:45 +03:00
2020-07-04 22:19:01 +03:00
void TextEditorWidget : : set_preview_mode ( PreviewMode mode )
2020-06-26 23:47:29 +03:00
{
2020-07-04 22:19:01 +03:00
if ( m_preview_mode = = mode )
2020-06-26 23:47:29 +03:00
return ;
2020-07-04 22:19:01 +03: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 23:47:29 +03:00
update_html_preview ( ) ;
2020-07-04 22:19:01 +03: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 23:47:29 +03:00
}
2020-07-04 22:19:01 +03:00
void TextEditorWidget : : update_preview ( )
2020-04-28 22:42:45 +03:00
{
2020-07-04 22:19:01 +03:00
switch ( m_preview_mode ) {
case PreviewMode : : Markdown :
2020-04-28 22:42:45 +03:00
update_markdown_preview ( ) ;
2020-07-04 22:19:01 +03:00
break ;
case PreviewMode : : HTML :
update_html_preview ( ) ;
break ;
default :
break ;
}
2020-04-28 22:42:45 +03:00
}
void TextEditorWidget : : update_markdown_preview ( )
{
2020-05-11 20:55:31 +03:00
auto document = Markdown : : Document : : parse ( m_editor - > text ( ) ) ;
if ( document ) {
auto html = document - > render_to_html ( ) ;
2020-07-20 15:22:07 +03:00
auto current_scroll_pos = m_page_view - > visible_content_rect ( ) ;
2020-06-21 22:52:51 +03:00
m_page_view - > load_html ( html , URL : : create_with_file_protocol ( m_path ) ) ;
2020-07-20 15:22:07 +03:00
m_page_view - > scroll_into_view ( current_scroll_pos , true , true ) ;
2020-04-28 22:42:45 +03:00
}
}
2020-06-26 23:47:29 +03:00
void TextEditorWidget : : update_html_preview ( )
{
2020-07-20 15:22:07 +03:00
auto current_scroll_pos = m_page_view - > visible_content_rect ( ) ;
2020-06-26 23:47:29 +03:00
m_page_view - > load_html ( m_editor - > text ( ) , URL : : create_with_file_protocol ( m_path ) ) ;
2020-07-20 15:22:07 +03:00
m_page_view - > scroll_into_view ( current_scroll_pos , true , true ) ;
2020-06-26 23:47:29 +03:00
}
2020-08-29 14:44:25 +03:00
void TextEditorWidget : : update_statusbar_cursor_position ( )
{
StringBuilder builder ;
2020-10-06 20:25:44 +03:00
builder . appendff ( " Line: {}, Column: {} " , m_editor - > cursor ( ) . line ( ) + 1 , m_editor - > cursor ( ) . column ( ) ) ;
2020-08-29 14:44:25 +03:00
m_statusbar - > set_text ( builder . to_string ( ) ) ;
}