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"
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-02-06 17:04:03 +03:00
# include <LibCore/File.h>
2020-02-14 15:18:34 +03:00
# include <LibCore/MimeData.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>
# include <LibGUI/FontDatabase.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>
# include <LibGUI/StatusBar.h>
# include <LibGUI/TextBox.h>
# include <LibGUI/TextEditor.h>
# include <LibGUI/ToolBar.h>
2020-02-16 11:17:49 +03:00
# include <LibGfx/Font.h>
2020-03-08 14:05:14 +03:00
# include <string.h>
2019-07-11 21:52:33 +03:00
TextEditorWidget : : TextEditorWidget ( )
{
2020-03-04 11:43:54 +03:00
set_layout < GUI : : VerticalBoxLayout > ( ) ;
2019-07-11 21:52:33 +03:00
layout ( ) - > set_spacing ( 0 ) ;
2020-03-04 21:07:55 +03:00
auto & toolbar = add < GUI : : ToolBar > ( ) ;
2020-02-23 12:57:42 +03:00
m_editor = add < GUI : : TextEditor > ( ) ;
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 ] {
2019-12-05 09:57:54 +03:00
// Do not mark as diry on the first change (When document is first opened.)
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 ( ) ;
} ;
2020-02-23 12:57:42 +03:00
m_find_replace_widget = add < GUI : : Widget > ( ) ;
2020-01-11 21:52:47 +03:00
m_find_replace_widget - > set_fill_with_background_color ( true ) ;
2020-02-02 17:07:41 +03:00
m_find_replace_widget - > set_size_policy ( GUI : : SizePolicy : : Fill , GUI : : SizePolicy : : Fixed ) ;
2020-01-11 21:52:47 +03:00
m_find_replace_widget - > set_preferred_size ( 0 , 48 ) ;
2020-03-04 11:43:54 +03:00
m_find_replace_widget - > set_layout < GUI : : VerticalBoxLayout > ( ) ;
2020-01-11 21:52:47 +03:00
m_find_replace_widget - > layout ( ) - > set_margins ( { 2 , 2 , 2 , 4 } ) ;
m_find_replace_widget - > set_visible ( false ) ;
2020-02-23 12:57:42 +03:00
m_find_widget = m_find_replace_widget - > add < GUI : : Widget > ( ) ;
2019-08-22 12:09:25 +03:00
m_find_widget - > set_fill_with_background_color ( true ) ;
2020-02-02 17:07:41 +03:00
m_find_widget - > set_size_policy ( GUI : : SizePolicy : : Fill , GUI : : SizePolicy : : Fixed ) ;
2019-08-22 12:09:25 +03:00
m_find_widget - > set_preferred_size ( 0 , 22 ) ;
2020-03-04 11:43:54 +03:00
m_find_widget - > set_layout < GUI : : HorizontalBoxLayout > ( ) ;
2019-08-22 12:09:25 +03:00
m_find_widget - > set_visible ( false ) ;
2019-08-21 22:30:20 +03:00
2020-02-23 12:57:42 +03:00
m_replace_widget = m_find_replace_widget - > add < GUI : : Widget > ( ) ;
2020-01-11 21:52:47 +03:00
m_replace_widget - > set_fill_with_background_color ( true ) ;
2020-02-02 17:07:41 +03:00
m_replace_widget - > set_size_policy ( GUI : : SizePolicy : : Fill , GUI : : SizePolicy : : Fixed ) ;
2020-01-11 21:52:47 +03:00
m_replace_widget - > set_preferred_size ( 0 , 22 ) ;
2020-03-04 11:43:54 +03:00
m_replace_widget - > set_layout < GUI : : HorizontalBoxLayout > ( ) ;
2020-01-11 21:52:47 +03:00
m_replace_widget - > set_visible ( false ) ;
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 ( ) ) {
dbg ( ) < < " find_next( \" \" ) " ;
return ;
}
2019-11-01 21:19:39 +03:00
auto found_range = m_editor - > document ( ) . find_next ( needle , m_editor - > normalized_selection ( ) . end ( ) ) ;
2019-08-24 21:09:35 +03:00
dbg ( ) < < " find_next( \" " < < needle < < " \" ) returned " < < found_range ;
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
} else {
2020-02-02 17:07:41 +03:00
GUI : : MessageBox : : show (
2019-08-24 21:09:35 +03:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" Not found " ,
2020-02-02 17:07:41 +03:00
GUI : : MessageBox : : Type : : Information ,
GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
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-02-02 17:07:41 +03:00
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 ( ) ) {
dbg ( ) < < " find_prev( \" \" ) " ;
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 ( ) ;
2019-11-01 21:19:39 +03:00
auto found_range = m_editor - > document ( ) . find_previous ( needle , selection_start ) ;
2019-08-25 13:23:34 +03:00
2019-08-24 21:09:35 +03:00
dbg ( ) < < " find_prev( \" " < < needle < < " \" ) returned " < < found_range ;
2019-08-21 22:30:20 +03:00
if ( found_range . is_valid ( ) ) {
m_editor - > set_selection ( found_range ) ;
} else {
2020-02-02 17:07:41 +03:00
GUI : : MessageBox : : show (
2019-08-21 22:30:20 +03:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" Not found " ,
2020-02-02 17:07:41 +03:00
GUI : : MessageBox : : Type : : Information ,
GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
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 ( ) ;
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-02-02 17:07:41 +03:00
GUI : : MessageBox : : show (
2020-01-11 21:52:47 +03:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" Not found " ,
2020-02-02 17:07:41 +03:00
GUI : : MessageBox : : Type : : Information ,
GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
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 ( ) ;
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-02-02 17:07:41 +03:00
GUI : : MessageBox : : show (
2020-01-11 21:52:47 +03:00
String : : format ( " Not found: \" %s \" " , needle . characters ( ) ) ,
" Not found " ,
2020-02-02 17:07:41 +03:00
GUI : : MessageBox : : Type : : Information ,
GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
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 ;
auto found_range = m_editor - > document ( ) . find_next ( needle ) ;
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 ) ;
found_range = m_editor - > document ( ) . find_next ( needle ) ;
2020-01-11 21:52:47 +03:00
}
} ) ;
2020-02-23 12:57:42 +03:00
m_find_previous_button = m_find_widget - > add < GUI : : Button > ( " Find previous " ) ;
2020-02-02 17:07:41 +03:00
m_find_previous_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 21:52:47 +03:00
m_find_previous_button - > set_preferred_size ( 150 , 0 ) ;
2019-08-25 22:35:58 +03:00
m_find_previous_button - > set_action ( * m_find_previous_action ) ;
2020-02-23 12:57:42 +03:00
m_find_next_button = m_find_widget - > add < GUI : : Button > ( " Find next " ) ;
2020-02-02 17:07:41 +03:00
m_find_next_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 21:52:47 +03:00
m_find_next_button - > set_preferred_size ( 150 , 0 ) ;
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
} ;
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 ) ;
} ;
2020-02-23 12:57:42 +03:00
m_replace_previous_button = m_replace_widget - > add < GUI : : Button > ( " Replace previous " ) ;
2020-02-02 17:07:41 +03:00
m_replace_previous_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 21:52:47 +03:00
m_replace_previous_button - > set_preferred_size ( 100 , 0 ) ;
m_replace_previous_button - > set_action ( * m_replace_previous_action ) ;
2020-02-23 12:57:42 +03:00
m_replace_next_button = m_replace_widget - > add < GUI : : Button > ( " Replace next " ) ;
2020-02-02 17:07:41 +03:00
m_replace_next_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 21:52:47 +03:00
m_replace_next_button - > set_preferred_size ( 100 , 0 ) ;
m_replace_next_button - > set_action ( * m_replace_next_action ) ;
2020-02-23 12:57:42 +03:00
m_replace_all_button = m_replace_widget - > add < GUI : : Button > ( " Replace all " ) ;
2020-02-02 17:07:41 +03:00
m_replace_all_button - > set_size_policy ( GUI : : SizePolicy : : Fixed , GUI : : SizePolicy : : Fill ) ;
2020-01-11 21:52:47 +03: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 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 ) ;
2020-02-23 12:57:42 +03:00
m_statusbar = add < GUI : : StatusBar > ( ) ;
2019-07-11 21:52:33 +03:00
2019-09-21 17:29:47 +03:00
m_editor - > on_cursor_change = [ this ] {
2019-07-11 21:52:33 +03:00
StringBuilder builder ;
2019-07-27 22:20:38 +03:00
builder . appendf ( " Line: %d, Column: %d " , m_editor - > cursor ( ) . line ( ) + 1 , m_editor - > cursor ( ) . column ( ) ) ;
2019-09-21 17:29:47 +03:00
m_statusbar - > set_text ( builder . to_string ( ) ) ;
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-02-23 12:57:42 +03:00
auto save_document_first_result = GUI : : MessageBox : : show ( " Save 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 ( ) ) ;
set_path ( FileSystemPath ( ) ) ;
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 & ) {
Optional < String > open_path = GUI : : FilePicker : : get_open_filepath ( ) ;
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-02-23 12:57:42 +03:00
auto save_document_first_result = GUI : : MessageBox : : show ( " Save Document First? " , " Warning " , GUI : : MessageBox : : Type : : Warning , GUI : : MessageBox : : InputType : : YesNoCancel , window ( ) ) ;
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-02-06 13:56:38 +03: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-02-02 17:07:41 +03:00
Optional < String > save_path = GUI : : FilePicker : : get_save_filepath ( 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-02-02 17:07:41 +03:00
GUI : : MessageBox : : show ( " Unable to save file. \n " , " Error " , GUI : : MessageBox : : Type : : Error , GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
2019-07-14 03:58:04 +03:00
return ;
}
2019-08-27 21:18:19 +03:00
m_document_dirty = false ;
2019-07-29 07:45:50 +03:00
set_path ( FileSystemPath ( save_path . value ( ) ) ) ;
dbg ( ) < < " Wrote document to " < < save_path . value ( ) ;
2019-07-24 07:32:30 +03:00
} ) ;
2020-02-06 13:56:38 +03: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 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-02-02 17:07:41 +03:00
GUI : : MessageBox : : show ( " Unable to save file. \n " , " Error " , GUI : : MessageBox : : Type : : Error , GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
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-02-02 17:07:41 +03:00
m_line_wrapping_setting_action = GUI : : Action : : create ( " Line wrapping " , [ & ] ( GUI : : Action & action ) {
2019-08-25 13:23:34 +03:00
action . set_checked ( ! action . is_checked ( ) ) ;
m_editor - > set_line_wrapping_enabled ( action . is_checked ( ) ) ;
} ) ;
m_line_wrapping_setting_action - > set_checkable ( true ) ;
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-02-02 17:07:41 +03:00
GUI : : Application : : the ( ) . quit ( 0 ) ;
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 ) ;
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 ) ;
auto & font_menu = menubar - > add_menu ( " Font " ) ;
2020-03-07 02:02:21 +03:00
GUI : : FontDatabase : : the ( ) . for_each_fixed_width_font ( [ & ] ( const StringView & font_name ) {
2020-04-04 13:18:40 +03:00
font_menu . add_action ( GUI : : Action : : create ( font_name , [ this ] ( const GUI : : Action & action ) {
2020-03-07 02:02:21 +03:00
m_editor - > set_font ( GUI : : FontDatabase : : the ( ) . get_by_name ( action . text ( ) ) ) ;
2019-07-11 21:52:33 +03:00
m_editor - > update ( ) ;
} ) ) ;
} ) ;
2020-03-11 04:02:37 +03:00
syntax_actions = GUI : : ActionGroup { } ;
syntax_actions . set_exclusive ( true ) ;
2020-04-04 13:18:40 +03:00
auto & syntax_menu = menubar - > add_menu ( " Syntax " ) ;
2020-03-11 04:02:37 +03:00
m_plain_text_highlight = GUI : : Action : : create ( " Plain Text " , [ & ] ( GUI : : Action & action ) {
action . set_checked ( true ) ;
m_editor - > set_syntax_highlighter ( nullptr ) ;
m_editor - > update ( ) ;
} ) ;
m_plain_text_highlight - > set_checkable ( true ) ;
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
m_cpp_highlight = GUI : : Action : : create ( " C++ " , [ & ] ( GUI : : Action & action ) {
action . set_checked ( true ) ;
m_editor - > set_syntax_highlighter ( make < GUI : : CppSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
m_cpp_highlight - > set_checkable ( true ) ;
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-03-13 01:53:22 +03:00
m_js_highlight = GUI : : Action : : create ( " Javascript " , [ & ] ( GUI : : Action & action ) {
action . set_checked ( true ) ;
m_editor - > set_syntax_highlighter ( make < GUI : : JSSyntaxHighlighter > ( ) ) ;
m_editor - > update ( ) ;
} ) ;
m_js_highlight - > set_checkable ( true ) ;
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-04-04 13:18:40 +03:00
auto & view_menu = menubar - > add_menu ( " View " ) ;
view_menu . add_action ( * m_line_wrapping_setting_action ) ;
view_menu . add_separator ( ) ;
view_menu . add_submenu ( move ( font_menu ) ) ;
view_menu . add_submenu ( move ( syntax_menu ) ) ;
2019-08-27 18:01:52 +03:00
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-02-06 15:39:17 +03:00
GUI : : AboutDialog : : show ( " Text Editor " , Gfx : : Bitmap : : load_from_file ( " /res/icons/32x32/app-texteditor.png " ) , window ( ) ) ;
2019-07-11 21:52:33 +03:00
} ) ) ;
2020-02-02 17:07:41 +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 ( )
{
}
2019-07-29 07:45:50 +03:00
void TextEditorWidget : : set_path ( const FileSystemPath & file )
2019-07-24 07:32:30 +03:00
{
2019-07-29 07:45:50 +03:00
m_path = file . string ( ) ;
m_name = file . title ( ) ;
m_extension = file . extension ( ) ;
2020-02-07 22:12:25 +03:00
if ( m_extension = = " cpp " | | m_extension = = " h " )
2020-03-11 04:02:37 +03:00
m_cpp_highlight - > activate ( ) ;
2020-03-13 01:53:22 +03:00
else if ( m_extension = = " js " )
m_js_highlight - > activate ( ) ;
2020-03-11 04:02:37 +03:00
else
m_plain_text_highlight - > activate ( ) ;
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 ;
2019-08-27 21:18:19 +03:00
builder . append ( m_path ) ;
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 ) ;
if ( ! file - > open ( Core : : IODevice : : ReadOnly ) ) {
2020-02-02 17:07:41 +03:00
GUI : : MessageBox : : show ( String : : format ( " Opening \" %s \" failed: %s " , path . characters ( ) , strerror ( errno ) ) , " Error " , GUI : : MessageBox : : Type : : Error , GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
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 ;
2019-07-29 07:45:50 +03:00
set_path ( FileSystemPath ( 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-02-17 08:06:11 +03:00
auto result = GUI : : MessageBox : : show ( " The document has been modified. Would you like to save? " , " Unsaved changes " , GUI : : MessageBox : : Type : : Warning , GUI : : MessageBox : : InputType : : YesNoCancel , window ( ) ) ;
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-02-02 17:07:41 +03:00
GUI : : MessageBox : : show ( " TextEditor can only open one file at a time! " , " One at a time please! " , GUI : : MessageBox : : Type : : Error , GUI : : MessageBox : : InputType : : OK , window ( ) ) ;
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
}
}