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-09-14 12:51:12 +02:00
# include <Applications/TextEditor/MainWindowUI.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/AboutDialog.h>
# 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>
# include <LibGUI/FontDatabase.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>
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>
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-08-17 15:58:29 +02:00
# include <LibWeb/InProcessWebView.h>
2020-03-08 12:05:14 +01:00
# include <string.h>
2019-07-11 13:52:33 -05:00
TextEditorWidget : : TextEditorWidget ( )
{
2020-09-14 12:51:12 +02:00
load_from_json ( main_window_ui_json ) ;
2019-07-11 13:52:33 -05:00
2020-09-14 12:51:12 +02:00
auto & toolbar = static_cast < GUI : : ToolBar & > ( * find_descendant_by_name ( " toolbar " ) ) ;
2020-04-28 21:42:45 +02:00
2020-09-14 12:51:12 +02:00
m_editor = static_cast < GUI : : TextEditor & > ( * find_descendant_by_name ( " 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 ) ;
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 ( ) ;
} ;
2020-09-14 12:51:12 +02:00
m_page_view = static_cast < Web : : InProcessWebView & > ( * find_descendant_by_name ( " 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 ( ) ,
String : : format ( " The link to '%s' could not be opened. " , url . to_string ( ) . characters ( ) ) ,
" Failed to open link " ,
GUI : : MessageBox : : Type : : Error ) ;
}
} ;
2020-04-28 21:42:45 +02:00
2020-09-14 12:51:12 +02:00
m_find_replace_widget = * find_descendant_by_name ( " find_replace_widget " ) ;
2020-01-11 20:52:47 +02:00
2020-09-14 13:01:32 +02:00
m_find_widget = * find_descendant_by_name ( " find_widget " ) ;
2019-08-21 21:30:20 +02:00
2020-09-14 13:01:32 +02:00
m_replace_widget = * find_descendant_by_name ( " 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 ( ) ) {
dbg ( ) < < " find_next( \" \" ) " ;
return ;
}
2019-11-01 19:19:39 +01:00
auto found_range = m_editor - > document ( ) . find_next ( needle , m_editor - > normalized_selection ( ) . end ( ) ) ;
2019-08-24 12:09:35 -06:00
dbg ( ) < < " find_next( \" " < < needle < < " \" ) returned " < < found_range ;
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
} else {
2020-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) ,
2019-08-24 12:09:35 -06:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" 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
2020-04-22 14:45:15 +00:00
m_find_previous_action = GUI : : Action : : create ( " Find previous " , { Mod_Ctrl | Mod_Shift , Key_G } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/find-previous.png " ) , [ & ] ( 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 ( ) ) {
dbg ( ) < < " find_prev( \" \" ) " ;
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 ( ) ;
2019-11-01 19:19:39 +01:00
auto found_range = m_editor - > document ( ) . find_previous ( needle , selection_start ) ;
2019-08-25 12:23:34 +02:00
2019-08-24 12:09:35 -06:00
dbg ( ) < < " find_prev( \" " < < needle < < " \" ) returned " < < 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 ( ) ,
2019-08-21 21:30:20 +02:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" 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 ( ) ;
auto found_range = m_editor - > document ( ) . find_next ( 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-01-11 20:52:47 +02:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" 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 ( ) ;
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-01-11 20:52:47 +02:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" 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 ;
auto found_range = m_editor - > document ( ) . find_next ( needle ) ;
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 ) ;
found_range = m_editor - > document ( ) . find_next ( needle ) ;
2020-01-11 20:52:47 +02:00
}
} ) ;
2020-02-23 10:57:42 +01:00
m_find_previous_button = m_find_widget - > add < GUI : : Button > ( " Find previous " ) ;
2020-02-02 15:07:41 +01:00
m_find_previous_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 20:52:47 +02:00
m_find_previous_button - > set_preferred_size ( 150 , 0 ) ;
2019-08-25 21:35:58 +02:00
m_find_previous_button - > set_action ( * m_find_previous_action ) ;
2020-02-23 10:57:42 +01:00
m_find_next_button = m_find_widget - > add < GUI : : Button > ( " Find next " ) ;
2020-02-02 15:07:41 +01:00
m_find_next_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 20:52:47 +02:00
m_find_next_button - > set_preferred_size ( 150 , 0 ) ;
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
} ;
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 ) ;
} ;
2020-02-23 10:57:42 +01:00
m_replace_previous_button = m_replace_widget - > add < GUI : : Button > ( " Replace previous " ) ;
2020-02-02 15:07:41 +01:00
m_replace_previous_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 20:52:47 +02:00
m_replace_previous_button - > set_preferred_size ( 100 , 0 ) ;
m_replace_previous_button - > set_action ( * m_replace_previous_action ) ;
2020-02-23 10:57:42 +01:00
m_replace_next_button = m_replace_widget - > add < GUI : : Button > ( " Replace next " ) ;
2020-02-02 15:07:41 +01:00
m_replace_next_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 20:52:47 +02:00
m_replace_next_button - > set_preferred_size ( 100 , 0 ) ;
m_replace_next_button - > set_action ( * m_replace_next_action ) ;
2020-02-23 10:57:42 +01:00
m_replace_all_button = m_replace_widget - > add < GUI : : Button > ( " Replace all " ) ;
2020-02-02 15:07:41 +01:00
m_replace_all_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 20:52:47 +02:00
m_replace_all_button - > set_preferred_size ( 100 , 0 ) ;
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 ) ;
} ;
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 ) ;
2020-09-14 12:51:12 +02:00
m_statusbar = static_cast < GUI : : StatusBar & > ( * find_descendant_by_name ( " 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-07-15 20:45:11 -06:00
auto save_document_first_result = GUI : : MessageBox : : show ( window ( ) , " Save 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-07-15 20:45:11 -06:00
auto save_document_first_result = GUI : : MessageBox : : show ( window ( ) , " Save 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-02-06 11:56:38 +01:00
m_save_as_action = GUI : : Action : : create ( " Save as... " , { Mod_Ctrl | Mod_Shift , Key_S } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/save.png " ) , [ this ] ( const GUI : : Action & ) {
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 ( ) ) ) ;
2019-07-28 23:45:50 -05:00
dbg ( ) < < " Wrote document to " < < save_path . value ( ) ;
2019-07-24 06:32:30 +02:00
} ) ;
2020-02-06 11:56:38 +01:00
m_save_action = GUI : : Action : : create ( " Save " , { Mod_Ctrl , Key_S } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/save.png " ) , [ & ] ( const GUI : : Action & ) {
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 ( ) ;
edit_menu . add_action ( * m_find_replace_action ) ;
edit_menu . add_action ( * m_find_next_action ) ;
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 " ) ;
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-08-16 12:54:11 -04:00
font_actions . set_exclusive ( true ) ;
2020-04-29 11:48:11 +02:00
auto & font_menu = view_menu . add_submenu ( " Font " ) ;
2020-03-07 12:02:21 +13:00
GUI : : FontDatabase : : the ( ) . for_each_fixed_width_font ( [ & ] ( const StringView & font_name ) {
2020-08-16 12:54:11 -04:00
auto action = GUI : : Action : : create_checkable ( font_name , [ & ] ( auto & action ) {
2020-03-07 12:02:21 +13:00
m_editor - > set_font ( GUI : : FontDatabase : : the ( ) . get_by_name ( action . text ( ) ) ) ;
2019-07-11 13:52:33 -05:00
m_editor - > update ( ) ;
2020-08-16 12:54:11 -04:00
} ) ;
if ( m_editor - > font ( ) . name ( ) = = font_name )
action - > set_checked ( true ) ;
font_actions . add_action ( * action ) ;
font_menu . add_action ( * action ) ;
2019-07-11 13:52:33 -05:00
} ) ;
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 & ) {
2020-03-11 03:02:37 +02: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 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-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-04-04 12:18:40 +02:00
auto & help_menu = menubar - > add_menu ( " Help " ) ;
help_menu . add_action ( GUI : : Action : : create ( " About " , [ & ] ( auto & ) {
2020-07-04 19:41:43 +02:00
GUI : : AboutDialog : : show ( " Text Editor " , Gfx : : Bitmap : : load_from_file ( " /res/icons/32x32/app-text-editor.png " ) , 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-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 ;
2019-08-27 20:18:19 +02:00
builder . append ( m_path ) ;
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-07-15 20:45:11 -06:00
GUI : : MessageBox : : show ( window ( ) , String : : format ( " Opening \" %s \" failed: %s " , path . characters ( ) , 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 ;
builder . appendf ( " Line: %d, Column: %d " , m_editor - > cursor ( ) . line ( ) + 1 , m_editor - > cursor ( ) . column ( ) ) ;
m_statusbar - > set_text ( builder . to_string ( ) ) ;
}