@ -1,203 +1,217 @@
# include "window.h"
# include "window.h"
# include "logging.h"
# include "logging.h"
# include "singletons.h"
# include "singletons.h"
# include "sourcefile.h"
# include "config.h"
# include "api.h"
namespace sigc {
namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
}
Window : : Window ( ) : notebook ( ) , plugin_api ( & notebook ) , box ( Gtk : : ORIENTATION_VERTICAL ) {
Window : : Window ( ) : box ( Gtk : : ORIENTATION_VERTICAL ) {
INFO ( " Create Window " ) ;
INFO ( " Create Window " ) ;
set_title ( " juCi++ " ) ;
set_title ( " juCi++ " ) ;
set_default_size ( 600 , 400 ) ;
set_default_size ( 600 , 400 ) ;
set_events ( Gdk : : POINTER_MOTION_MASK | Gdk : : FOCUS_CHANGE_MASK | Gdk : : SCROLL_MASK ) ;
set_events ( Gdk : : POINTER_MOTION_MASK | Gdk : : FOCUS_CHANGE_MASK | Gdk : : SCROLL_MASK ) ;
add ( box ) ;
add ( box ) ;
//TODO: see TODO Window::on_directory_navigation
directories . m_TreeView . signal_row_activated ( ) . connect ( sigc : : mem_fun ( * this , & Window : : on_directory_navigation ) ) ;
MainConfig ( ) ; //Read the configs here
PluginApi ( & this - > notebook ) ; //Initialise plugins
add_menu ( ) ;
box . pack_start ( entry_box , Gtk : : PACK_SHRINK ) ;
directory_and_notebook_panes . pack1 ( directories , true , true ) ; //TODO: should be pack1(directories, ...) Clean up directories.*
directory_and_notebook_panes . pack2 ( notebook ) ;
directory_and_notebook_panes . set_position ( 120 ) ;
vpaned . set_position ( 300 ) ;
vpaned . pack1 ( directory_and_notebook_panes , true , false ) ;
vpaned . pack2 ( Singleton : : terminal ( ) - > view , true , true ) ;
box . pack_end ( vpaned ) ;
show_all_children ( ) ;
directories . on_row_activated = [ this ] ( const std : : string & file ) {
notebook . open ( file ) ;
} ;
entry_box . signal_show ( ) . connect ( [ this ] ( ) {
std : : vector < Gtk : : Widget * > focus_chain ;
focus_chain . emplace_back ( & entry_box ) ;
box . set_focus_chain ( focus_chain ) ;
} ) ;
entry_box . signal_hide ( ) . connect ( [ this ] ( ) {
box . unset_focus_chain ( ) ;
} ) ;
entry_box . signal_hide ( ) . connect ( [ this ] ( ) {
if ( notebook . get_current_page ( ) ! = - 1 ) {
notebook . get_current_view ( ) - > grab_focus ( ) ;
}
} ) ;
notebook . signal_switch_page ( ) . connect ( [ this ] ( Gtk : : Widget * page , guint page_num ) {
if ( search_entry_shown & & entry_box . labels . size ( ) > 0 & & notebook . get_current_page ( ) ! = - 1 ) {
notebook . get_current_view ( ) - > update_search_occurrences = [ this ] ( int number ) {
entry_box . labels . begin ( ) - > update ( 0 , std : : to_string ( number ) ) ;
} ;
notebook . get_current_view ( ) - > search_highlight ( last_search , case_sensitive_search , regex_search ) ;
}
if ( notebook . get_current_page ( ) ! = - 1 ) {
if ( auto menu_item = dynamic_cast < Gtk : : MenuItem * > ( Singleton : : menu ( ) - > ui_manager - > get_widget ( " /MenuBar/SourceMenu/SourceGotoDeclaration " ) ) )
menu_item - > set_sensitive ( ( bool ) notebook . get_current_view ( ) - > get_declaration_location ) ;
if ( auto menu_item = dynamic_cast < Gtk : : MenuItem * > ( Singleton : : menu ( ) - > ui_manager - > get_widget ( " /MenuBar/SourceMenu/SourceGotoMethod " ) ) )
menu_item - > set_sensitive ( ( bool ) notebook . get_current_view ( ) - > goto_method ) ;
if ( auto menu_item = dynamic_cast < Gtk : : MenuItem * > ( Singleton : : menu ( ) - > ui_manager - > get_widget ( " /MenuBar/SourceMenu/SourceRename " ) ) )
menu_item - > set_sensitive ( ( bool ) notebook . get_current_view ( ) - > rename_similar_tokens ) ;
}
} ) ;
INFO ( " Window created " ) ;
} // Window constructor
void Window : : add_menu ( ) {
auto menu = Singleton : : menu ( ) ;
auto menu = Singleton : : menu ( ) ;
INFO ( " Adding actions to menu " ) ;
INFO ( " Adding actions to menu " ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " FileQuit " , " Quit juCi++ " ) , Gtk : : AccelKey ( menu - > key_map [ " quit " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " FileQuit " , " Quit juCi++ " ) , Gtk : : AccelKey ( menu - > key_map [ " quit " ] ) , [ this ] ( ) {
hide ( ) ;
hide ( ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " FileNewFile " , " New file " ) , Gtk : : AccelKey ( menu - > key_map [ " new_file " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " FileNewFile " , " New file " ) , Gtk : : AccelKey ( menu - > key_map [ " new_file " ] ) , [ this ] ( ) {
new_file_entry ( ) ;
new_file_entry ( ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " FileOpenFile " , " Open file " ) , Gtk : : AccelKey ( menu - > key_map [ " open_file " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " FileOpenFile " , " Open file " ) , Gtk : : AccelKey ( menu - > key_map [ " open_file " ] ) , [ this ] ( ) {
open_file_dialog ( ) ;
open_file_dialog ( ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " FileOpenFolder " , " Open folder " ) , Gtk : : AccelKey ( menu - > key_map [ " open_folder " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " FileOpenFolder " , " Open folder " ) , Gtk : : AccelKey ( menu - > key_map [ " open_folder " ] ) , [ this ] ( ) {
open_folder_dialog ( ) ;
open_folder_dialog ( ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " FileSaveAs " , " Save as " ) , Gtk : : AccelKey ( menu - > key_map [ " save_as " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " FileSaveAs " , " Save as " ) , Gtk : : AccelKey ( menu - > key_map [ " save_as " ] ) , [ this ] ( ) {
save_file_dialog ( ) ;
save_file_dialog ( ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " FileSave " , " Save " ) , Gtk : : AccelKey ( menu - > key_map [ " save " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " FileSave " , " Save " ) , Gtk : : AccelKey ( menu - > key_map [ " save " ] ) , [ this ] ( ) {
notebook . CurrentSourceView ( ) - > save ( ) ;
notebook . save_current ( ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " EditCopy " , " Copy " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_copy " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " EditCopy " , " Copy " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_copy " ] ) , [ this ] ( ) {
auto widget = get_focus ( ) ;
auto widget = get_focus ( ) ;
if ( auto entry = dynamic_cast < Gtk : : Entry * > ( widget ) )
if ( auto entry = dynamic_cast < Gtk : : Entry * > ( widget ) )
entry - > copy_clipboard ( ) ;
entry - > copy_clipboard ( ) ;
else if ( auto text_view = dynamic_cast < Gtk : : TextView * > ( widget ) )
else if ( auto text_view = dynamic_cast < Gtk : : TextView * > ( widget ) )
text_view - > get_buffer ( ) - > copy_clipboard ( Gtk : : Clipboard : : get ( ) ) ;
text_view - > get_buffer ( ) - > copy_clipboard ( Gtk : : Clipboard : : get ( ) ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " EditCut " , " Cut " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_cut " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " EditCut " , " Cut " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_cut " ] ) , [ this ] ( ) {
auto widget = get_focus ( ) ;
auto widget = get_focus ( ) ;
if ( auto entry = dynamic_cast < Gtk : : Entry * > ( widget ) )
if ( auto entry = dynamic_cast < Gtk : : Entry * > ( widget ) )
entry - > cut_clipboard ( ) ;
entry - > cut_clipboard ( ) ;
else {
else if ( notebook . get_current_page ( ) ! = - 1 )
if ( notebook . Pages ( ) ! = 0 )
notebook . get_current_view ( ) - > get_buffer ( ) - > cut_clipboard ( Gtk : : Clipboard : : get ( ) ) ;
notebook . CurrentSourceView ( ) - > get_buffer ( ) - > cut_clipboard ( Gtk : : Clipboard : : get ( ) ) ;
} ) ;
}
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " EditPaste " , " Paste " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_paste " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " EditPaste " , " Paste " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_paste " ] ) , [ this ] ( ) {
auto widget = get_focus ( ) ;
auto widget = get_focus ( ) ;
if ( auto entry = dynamic_cast < Gtk : : Entry * > ( widget ) )
if ( auto entry = dynamic_cast < Gtk : : Entry * > ( widget ) )
entry - > paste_clipboard ( ) ;
entry - > paste_clipboard ( ) ;
else {
else if ( notebook . get_current_page ( ) ! = - 1 )
if ( notebook . Pages ( ) ! = 0 )
notebook . get_current_view ( ) - > get_buffer ( ) - > paste_clipboard ( Gtk : : Clipboard : : get ( ) ) ;
notebook . CurrentSourceView ( ) - > get_buffer ( ) - > paste_clipboard ( Gtk : : Clipboard : : get ( ) ) ;
} ) ;
}
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " EditFind " , " Find " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_find " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " EditFind " , " Find " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_find " ] ) , [ this ] ( ) {
search_and_replace_entry ( ) ;
search_and_replace_entry ( ) ;
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " EditUndo " , " Undo " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_undo " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " SourceRename " , " Rename function/variable " ) , Gtk : : AccelKey ( menu - > key_map [ " source_rename " ] ) , [ this ] ( ) {
INFO ( " On undo " ) ;
entry_box . clear ( ) ;
if ( notebook . get_current_page ( ) ! = - 1 ) {
if ( notebook . CurrentPage ( ) ! = - 1 ) {
auto undo_manager = notebook . get_current_view ( ) - > get_source_buffer ( ) - > get_undo_manager ( ) ;
if ( notebook . CurrentSourceView ( ) - > get_token & & notebook . CurrentSourceView ( ) - > get_token_name ) {
if ( undo_manager - > can_undo ( ) ) {
auto token = std : : make_shared < std : : string > ( notebook . CurrentSourceView ( ) - > get_token ( ) ) ;
undo_manager - > undo ( ) ;
if ( token - > size ( ) > 0 & & notebook . CurrentSourceView ( ) - > get_token_name ) {
}
auto token_name = std : : make_shared < std : : string > ( notebook . CurrentSourceView ( ) - > get_token_name ( ) ) ;
}
for ( int c = 0 ; c < notebook . Pages ( ) ; c + + ) {
INFO ( " Done undo " ) ;
if ( notebook . source_views . at ( c ) - > view - > tag_similar_tokens ) {
} ) ;
notebook . source_views . at ( c ) - > view - > tag_similar_tokens ( * token ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " EditRedo " , " Redo " ) , Gtk : : AccelKey ( menu - > key_map [ " edit_redo " ] ) , [ this ] ( ) {
}
INFO ( " On Redo " ) ;
if ( notebook . get_current_page ( ) ! = - 1 ) {
auto undo_manager = notebook . get_current_view ( ) - > get_source_buffer ( ) - > get_undo_manager ( ) ;
if ( undo_manager - > can_redo ( ) ) {
undo_manager - > redo ( ) ;
}
}
INFO ( " Done Redo " ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " SourceGotoDeclaration " , " Go to declaration " ) , Gtk : : AccelKey ( menu - > key_map [ " source_goto_declaration " ] ) , [ this ] ( ) {
if ( notebook . get_current_page ( ) ! = - 1 ) {
if ( notebook . get_current_view ( ) - > get_declaration_location ) {
auto location = notebook . get_current_view ( ) - > get_declaration_location ( ) ;
if ( location . first . size ( ) > 0 ) {
notebook . open ( location . first ) ;
notebook . get_current_view ( ) - > get_buffer ( ) - > place_cursor ( notebook . get_current_view ( ) - > get_buffer ( ) - > get_iter_at_offset ( location . second ) ) ;
while ( gtk_events_pending ( ) )
gtk_main_iteration ( ) ;
notebook . get_current_view ( ) - > scroll_to ( notebook . get_current_view ( ) - > get_buffer ( ) - > get_insert ( ) , 0.0 , 1.0 , 0.5 ) ;
}
}
entry_box . labels . emplace_back ( ) ;
auto label_it = entry_box . labels . begin ( ) ;
label_it - > update = [ label_it ] ( int state , const std : : string & message ) {
label_it - > set_text ( " Warning: only opened and parsed tabs will have its content renamed, and modified files will be saved. " ) ;
} ;
label_it - > update ( 0 , " " ) ;
entry_box . entries . emplace_back ( * token_name , [ this , token_name , token ] ( const std : : string & content ) {
if ( notebook . CurrentPage ( ) ! = - 1 & & content ! = * token_name ) {
for ( int c = 0 ; c < notebook . Pages ( ) ; c + + ) {
if ( notebook . source_views . at ( c ) - > view - > rename_similar_tokens ) {
auto number = notebook . source_views . at ( c ) - > view - > rename_similar_tokens ( * token , content ) ;
if ( number > 0 ) {
Singleton : : terminal ( ) - > print ( " Replaced " + std : : to_string ( number ) + " occurrences in file " + notebook . source_views . at ( c ) - > view - > file_path + " \n " ) ;
notebook . source_views . at ( c ) - > view - > save ( ) ;
}
}
}
entry_box . hide ( ) ;
}
} ) ;
auto entry_it = entry_box . entries . begin ( ) ;
entry_box . buttons . emplace_back ( " Rename " , [ this , entry_it ] ( ) {
entry_it - > activate ( ) ;
} ) ;
entry_box . show ( ) ;
}
}
}
}
}
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " SourceGotoMethod " , " Go to method " ) , Gtk : : AccelKey ( menu - > key_map [ " source_goto_method " ] ) , [ this ] ( ) {
if ( notebook . get_current_page ( ) ! = - 1 ) {
if ( notebook . get_current_view ( ) - > goto_method ) {
notebook . get_current_view ( ) - > goto_method ( ) ;
}
}
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " SourceRename " , " Rename " ) , Gtk : : AccelKey ( menu - > key_map [ " source_rename " ] ) , [ this ] ( ) {
rename_token_entry ( ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " ProjectCompileAndRun " , " Compile And Run " ) , Gtk : : AccelKey ( menu - > key_map [ " compile_and_run " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " ProjectCompileAndRun " , " Compile And Run " ) , Gtk : : AccelKey ( menu - > key_map [ " compile_and_run " ] ) , [ this ] ( ) {
if ( notebook . CurrentPage ( ) = = - 1 )
if ( notebook . get_current_page ( ) = = - 1 )
return ;
return ;
notebook . CurrentSourceView ( ) - > save ( ) ;
notebook . save_current ( ) ;
if ( running . try_lock ( ) ) {
if ( running . try_lock ( ) ) {
std : : thread execute ( [ this ] ( ) {
std : : thread execute ( [ this ] ( ) {
std : : string path = notebook . CurrentSourceView ( ) - > file_path ;
std : : string path = notebook . get_current_view ( ) - > file_path ;
size_t pos = path . find_last_of ( " / \\ " ) ;
size_t pos = path . find_last_of ( " / \\ " ) ;
if ( pos ! = std : : string : : npos ) {
if ( pos ! = std : : string : : npos ) {
path . erase ( path . begin ( ) + pos , path . end ( ) ) ;
path . erase ( path . begin ( ) + pos , path . end ( ) ) ;
Singleton : : terminal ( ) - > SetFolderCommand ( path ) ;
Singleton : : terminal ( ) - > SetFolderCommand ( path ) ;
}
}
Singleton : : terminal ( ) - > Compile ( ) ;
Singleton : : terminal ( ) - > Compile ( ) ;
std : : string executable = directories . GetCmakeVarValue ( path , " add_executable " ) ;
std : : string executable = directories . get_cmakelists_variable ( path , " add_executable " ) ;
Singleton : : terminal ( ) - > Run ( executable ) ;
Singleton : : terminal ( ) - > Run ( executable ) ;
running . unlock ( ) ;
running . unlock ( ) ;
} ) ;
} ) ;
execute . detach ( ) ;
execute . detach ( ) ;
}
}
} ) ;
} ) ;
menu - > action_group - > add ( Gtk : : Action : : create ( " ProjectCompile " , " Compile " ) , Gtk : : AccelKey ( menu - > key_map [ " compile " ] ) , [ this ] ( ) {
menu - > action_group - > add ( Gtk : : Action : : create ( " ProjectCompile " , " Compile " ) , Gtk : : AccelKey ( menu - > key_map [ " compile " ] ) , [ this ] ( ) {
if ( notebook . CurrentPage ( ) = = - 1 )
if ( notebook . get_current_page ( ) = = - 1 )
return ;
return ;
notebook . CurrentSourceView ( ) - > save ( ) ;
notebook . save_current ( ) ;
if ( running . try_lock ( ) ) {
if ( running . try_lock ( ) ) {
std : : thread execute ( [ this ] ( ) {
std : : thread execute ( [ this ] ( ) {
std : : string path = notebook . CurrentSourceView ( ) - > file_path ;
std : : string path = notebook . get_current_view ( ) - > file_path ;
size_t pos = path . find_last_of ( " / \\ " ) ;
size_t pos = path . find_last_of ( " / \\ " ) ;
if ( pos ! = std : : string : : npos ) {
if ( pos ! = std : : string : : npos ) {
path . erase ( path . begin ( ) + pos , path . end ( ) ) ;
path . erase ( path . begin ( ) + pos , path . end ( ) ) ;
Singleton : : terminal ( ) - > SetFolderCommand ( path ) ;
Singleton : : terminal ( ) - > SetFolderCommand ( path ) ;
}
}
Singleton : : terminal ( ) - > Compile ( ) ;
Singleton : : terminal ( ) - > Compile ( ) ;
running . unlock ( ) ;
running . unlock ( ) ;
} ) ;
} ) ;
execute . detach ( ) ;
execute . detach ( ) ;
}
}
} ) ;
} ) ;
INFO ( " Done adding actions to menu, building menu: " )
menu - > action_group - > add ( Gtk : : Action : : create ( " WindowCloseTab " , " Close tab " ) , Gtk : : AccelKey ( menu - > key_map [ " close_tab " ] ) , [ this ] ( ) {
notebook . close_current_page ( ) ;
} ) ;
add_accel_group ( menu - > ui_manager - > get_accel_group ( ) ) ;
add_accel_group ( menu - > ui_manager - > get_accel_group ( ) ) ;
menu - > build ( ) ;
menu - > build ( ) ;
INFO ( " Menu build " )
INFO ( " Menu build " )
box . pack_start ( menu - > get_widget ( ) , Gtk : : PACK_SHRINK ) ;
box . pack_start ( menu - > get_widget ( ) , Gtk : : PACK_SHRINK ) ;
box . pack_start ( entry_box , Gtk : : PACK_SHRINK ) ;
}
directory_and_notebook_panes . pack1 ( directories . widget ( ) , true , true ) ; //TODO: should be pack1(directories, ...) Clean up directories.*
directory_and_notebook_panes . pack2 ( notebook ) ;
directory_and_notebook_panes . set_position ( 120 ) ;
vpaned . set_position ( 300 ) ;
vpaned . pack1 ( directory_and_notebook_panes , true , false ) ;
vpaned . pack2 ( Singleton : : terminal ( ) - > view , true , true ) ;
box . pack_end ( vpaned ) ;
show_all_children ( ) ;
entry_box . signal_show ( ) . connect ( [ this ] ( ) {
std : : vector < Gtk : : Widget * > focus_chain ;
focus_chain . emplace_back ( & entry_box ) ;
box . set_focus_chain ( focus_chain ) ;
} ) ;
entry_box . signal_hide ( ) . connect ( [ this ] ( ) {
box . unset_focus_chain ( ) ;
} ) ;
entry_box . signal_hide ( ) . connect ( [ this ] ( ) {
if ( notebook . CurrentPage ( ) ! = - 1 ) {
notebook . CurrentSourceView ( ) - > grab_focus ( ) ;
}
} ) ;
notebook . signal_switch_page ( ) . connect ( [ this ] ( Gtk : : Widget * page , guint page_num ) {
if ( search_entry_shown & & entry_box . labels . size ( ) > 0 & & notebook . CurrentPage ( ) ! = - 1 ) {
notebook . CurrentSourceView ( ) - > update_search_occurrences = [ this ] ( int number ) {
entry_box . labels . begin ( ) - > update ( 0 , std : : to_string ( number ) ) ;
} ;
notebook . CurrentSourceView ( ) - > search_highlight ( last_search , case_sensitive_search , regex_search ) ;
}
if ( notebook . CurrentPage ( ) ! = - 1 ) {
if ( auto menu_item = dynamic_cast < Gtk : : MenuItem * > ( Singleton : : menu ( ) - > ui_manager - > get_widget ( " /MenuBar/SourceMenu/SourceGotoDeclaration " ) ) )
menu_item - > set_sensitive ( ( bool ) notebook . CurrentSourceView ( ) - > get_declaration_location ) ;
if ( auto menu_item = dynamic_cast < Gtk : : MenuItem * > ( Singleton : : menu ( ) - > ui_manager - > get_widget ( " /MenuBar/SourceMenu/SourceGotoMethod " ) ) )
menu_item - > set_sensitive ( ( bool ) notebook . CurrentSourceView ( ) - > goto_method ) ;
if ( auto menu_item = dynamic_cast < Gtk : : MenuItem * > ( Singleton : : menu ( ) - > ui_manager - > get_widget ( " /MenuBar/SourceMenu/SourceRename " ) ) )
menu_item - > set_sensitive ( ( bool ) notebook . CurrentSourceView ( ) - > rename_similar_tokens ) ;
}
} ) ;
INFO ( " Window created " ) ;
} // Window constructor
bool Window : : on_key_press_event ( GdkEventKey * event ) {
bool Window : : on_key_press_event ( GdkEventKey * event ) {
if ( event - > keyval = = GDK_KEY_Escape )
if ( event - > keyval = = GDK_KEY_Escape )
@ -236,7 +250,7 @@ bool Window::on_key_press_event(GdkEventKey *event) {
}
}
}
}
# endif
# endif
return Gtk : : Window : : on_key_press_event ( event ) ;
return Gtk : : Window : : on_key_press_event ( event ) ;
}
}
@ -246,8 +260,8 @@ bool Window::on_delete_event (GdkEventAny *event) {
}
}
void Window : : hide ( ) {
void Window : : hide ( ) {
auto size = notebook . source_views . s ize ( ) ;
auto size = notebook . size ( ) ;
for ( size_ t c = 0 ; c < size ; c + + ) {
for ( in t c = 0 ; c < size ; c + + ) {
if ( ! notebook . close_current_page ( ) )
if ( ! notebook . close_current_page ( ) )
return ;
return ;
}
}
@ -266,17 +280,11 @@ void Window::new_file_entry() {
Singleton : : terminal ( ) - > print ( " Error: " + p . string ( ) + " already exists. \n " ) ;
Singleton : : terminal ( ) - > print ( " Error: " + p . string ( ) + " already exists. \n " ) ;
}
}
else {
else {
std : : ofstream f ( p . string ( ) . c_str ( ) ) ;
juci : : filesystem : : save ( p ) ;
if ( f ) {
notebook . open ( boost : : filesystem : : canonical ( p ) . string ( ) ) ;
notebook . open_file ( boost : : filesystem : : canonical ( p ) . string ( ) ) ;
Singleton : : terminal ( ) - > print ( " New file " + p . string ( ) + " created. \n " ) ;
Singleton : : terminal ( ) - > print ( " New file " + p . string ( ) + " created. \n " ) ;
if ( notebook . project_path ! = " " )
if ( notebook . project_path ! = " " )
directories . open_folder ( notebook . project_path ) ; //TODO: Do refresh instead
directories . open_folder ( notebook . project_path ) ; //TODO: Do refresh instead
}
else {
Singleton : : terminal ( ) - > print ( " Error: could not create new file " + p . string ( ) + " . \n " ) ;
}
f . close ( ) ;
}
}
}
}
entry_box . hide ( ) ;
entry_box . hide ( ) ;
@ -310,7 +318,6 @@ void Window::open_file_dialog() {
Gtk : : FileChooserDialog dialog ( " Please choose a file " , Gtk : : FILE_CHOOSER_ACTION_OPEN ) ;
Gtk : : FileChooserDialog dialog ( " Please choose a file " , Gtk : : FILE_CHOOSER_ACTION_OPEN ) ;
if ( notebook . project_path . size ( ) > 0 )
if ( notebook . project_path . size ( ) > 0 )
gtk_file_chooser_set_current_folder ( ( GtkFileChooser * ) dialog . gobj ( ) , notebook . project_path . c_str ( ) ) ;
gtk_file_chooser_set_current_folder ( ( GtkFileChooser * ) dialog . gobj ( ) , notebook . project_path . c_str ( ) ) ;
std : : cout < < notebook . project_path < < std : : endl ;
dialog . set_transient_for ( * this ) ;
dialog . set_transient_for ( * this ) ;
dialog . set_position ( Gtk : : WindowPosition : : WIN_POS_CENTER_ALWAYS ) ;
dialog . set_position ( Gtk : : WindowPosition : : WIN_POS_CENTER_ALWAYS ) ;
@ -340,14 +347,16 @@ void Window::open_file_dialog() {
if ( result = = Gtk : : RESPONSE_OK ) {
if ( result = = Gtk : : RESPONSE_OK ) {
std : : string path = dialog . get_filename ( ) ;
std : : string path = dialog . get_filename ( ) ;
notebook . open_file ( path ) ;
notebook . open ( path ) ;
}
}
}
}
void Window : : save_file_dialog ( ) {
void Window : : save_file_dialog ( ) {
if ( notebook . get_current_page ( ) = = - 1 )
return ;
INFO ( " Save file dialog " ) ;
INFO ( " Save file dialog " ) ;
Gtk : : FileChooserDialog dialog ( * this , " Please choose a file " , Gtk : : FILE_CHOOSER_ACTION_SAVE ) ;
Gtk : : FileChooserDialog dialog ( * this , " Please choose a file " , Gtk : : FILE_CHOOSER_ACTION_SAVE ) ;
gtk_file_chooser_set_filename ( ( GtkFileChooser * ) dialog . gobj ( ) , notebook . CurrentSourceV iew( ) - > file_path . c_str ( ) ) ;
gtk_file_chooser_set_filename ( ( GtkFileChooser * ) dialog . gobj ( ) , notebook . get_current_v iew( ) - > file_path . c_str ( ) ) ;
dialog . set_position ( Gtk : : WindowPosition : : WIN_POS_CENTER_ALWAYS ) ;
dialog . set_position ( Gtk : : WindowPosition : : WIN_POS_CENTER_ALWAYS ) ;
dialog . add_button ( " _Cancel " , Gtk : : RESPONSE_CANCEL ) ;
dialog . add_button ( " _Cancel " , Gtk : : RESPONSE_CANCEL ) ;
dialog . add_button ( " _Save " , Gtk : : RESPONSE_OK ) ;
dialog . add_button ( " _Save " , Gtk : : RESPONSE_OK ) ;
@ -358,10 +367,10 @@ void Window::save_file_dialog() {
if ( path . size ( ) > 0 ) {
if ( path . size ( ) > 0 ) {
std : : ofstream file ( path ) ;
std : : ofstream file ( path ) ;
if ( file ) {
if ( file ) {
file < < notebook . CurrentSourceV iew( ) - > get_buffer ( ) - > get_text ( ) ;
file < < notebook . get_current_v iew( ) - > get_buffer ( ) - > get_text ( ) ;
file . close ( ) ;
file . close ( ) ;
notebook . open_file ( path ) ;
notebook . open ( path ) ;
Singleton : : terminal ( ) - > print ( " File saved to: " + notebook . CurrentSourceV iew( ) - > file_path + " \n " ) ;
Singleton : : terminal ( ) - > print ( " File saved to: " + notebook . get_current_v iew( ) - > file_path + " \n " ) ;
if ( notebook . project_path ! = " " )
if ( notebook . project_path ! = " " )
directories . open_folder ( notebook . project_path ) ; //TODO: Do refresh instead
directories . open_folder ( notebook . project_path ) ; //TODO: Do refresh instead
}
}
@ -371,27 +380,6 @@ void Window::save_file_dialog() {
}
}
}
}
//TODO: move most of it to Directories
void Window : : on_directory_navigation ( const Gtk : : TreeModel : : Path & path , Gtk : : TreeViewColumn * column ) {
INFO ( " Directory navigation " ) ;
Gtk : : TreeModel : : iterator iter = directories . m_refTreeModel - > get_iter ( path ) ;
if ( iter ) {
Gtk : : TreeModel : : Row row = * iter ;
std : : string upath = Glib : : ustring ( row [ directories . view ( ) . m_col_path ] ) ;
boost : : filesystem : : path fs_path ( upath ) ;
if ( boost : : filesystem : : is_directory ( fs_path ) ) {
directories . m_TreeView . row_expanded ( path ) ?
directories . m_TreeView . collapse_row ( path ) :
directories . m_TreeView . expand_row ( path , false ) ;
} else {
std : : stringstream sstm ;
sstm < < row [ directories . view ( ) . m_col_path ] ;
std : : string file = sstm . str ( ) ;
notebook . open_file ( file ) ;
}
}
}
void Window : : search_and_replace_entry ( ) {
void Window : : search_and_replace_entry ( ) {
entry_box . clear ( ) ;
entry_box . clear ( ) ;
entry_box . labels . emplace_back ( ) ;
entry_box . labels . emplace_back ( ) ;
@ -408,81 +396,123 @@ void Window::search_and_replace_entry() {
}
}
} ;
} ;
entry_box . entries . emplace_back ( last_search , [ this ] ( const std : : string & content ) {
entry_box . entries . emplace_back ( last_search , [ this ] ( const std : : string & content ) {
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > search_forward ( ) ;
notebook . get_current_v iew( ) - > search_forward ( ) ;
} ) ;
} ) ;
auto search_entry_it = entry_box . entries . begin ( ) ;
auto search_entry_it = entry_box . entries . begin ( ) ;
search_entry_it - > set_placeholder_text ( " Find " ) ;
search_entry_it - > set_placeholder_text ( " Find " ) ;
if ( notebook . CurrentP age( ) ! = - 1 ) {
if ( notebook . get_current_p age( ) ! = - 1 ) {
notebook . CurrentSourceV iew( ) - > update_search_occurrences = [ label_it ] ( int number ) {
notebook . get_current_v iew( ) - > update_search_occurrences = [ label_it ] ( int number ) {
label_it - > update ( 0 , std : : to_string ( number ) ) ;
label_it - > update ( 0 , std : : to_string ( number ) ) ;
} ;
} ;
notebook . CurrentSourceV iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
notebook . get_current_v iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
}
}
search_entry_it - > signal_key_press_event ( ) . connect ( [ this ] ( GdkEventKey * event ) {
search_entry_it - > signal_key_press_event ( ) . connect ( [ this ] ( GdkEventKey * event ) {
if ( event - > keyval = = GDK_KEY_Return & & event - > state = = GDK_SHIFT_MASK ) {
if ( event - > keyval = = GDK_KEY_Return & & event - > state = = GDK_SHIFT_MASK ) {
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > search_backward ( ) ;
notebook . get_current_v iew( ) - > search_backward ( ) ;
}
}
return false ;
return false ;
} ) ;
} ) ;
search_entry_it - > signal_changed ( ) . connect ( [ this , search_entry_it ] ( ) {
search_entry_it - > signal_changed ( ) . connect ( [ this , search_entry_it ] ( ) {
last_search = search_entry_it - > get_text ( ) ;
last_search = search_entry_it - > get_text ( ) ;
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
notebook . get_current_v iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
} ) ;
} ) ;
entry_box . entries . emplace_back ( last_replace , [ this ] ( const std : : string & content ) {
entry_box . entries . emplace_back ( last_replace , [ this ] ( const std : : string & content ) {
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > replace_forward ( content ) ;
notebook . get_current_v iew( ) - > replace_forward ( content ) ;
} ) ;
} ) ;
auto replace_entry_it = entry_box . entries . begin ( ) ;
auto replace_entry_it = entry_box . entries . begin ( ) ;
replace_entry_it + + ;
replace_entry_it + + ;
replace_entry_it - > set_placeholder_text ( " Replace " ) ;
replace_entry_it - > set_placeholder_text ( " Replace " ) ;
replace_entry_it - > signal_key_press_event ( ) . connect ( [ this , replace_entry_it ] ( GdkEventKey * event ) {
replace_entry_it - > signal_key_press_event ( ) . connect ( [ this , replace_entry_it ] ( GdkEventKey * event ) {
if ( event - > keyval = = GDK_KEY_Return & & event - > state = = GDK_SHIFT_MASK ) {
if ( event - > keyval = = GDK_KEY_Return & & event - > state = = GDK_SHIFT_MASK ) {
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > replace_backward ( replace_entry_it - > get_text ( ) ) ;
notebook . get_current_v iew( ) - > replace_backward ( replace_entry_it - > get_text ( ) ) ;
}
}
return false ;
return false ;
} ) ;
} ) ;
replace_entry_it - > signal_changed ( ) . connect ( [ this , replace_entry_it ] ( ) {
replace_entry_it - > signal_changed ( ) . connect ( [ this , replace_entry_it ] ( ) {
last_replace = replace_entry_it - > get_text ( ) ;
last_replace = replace_entry_it - > get_text ( ) ;
} ) ;
} ) ;
entry_box . buttons . emplace_back ( " Find " , [ this ] ( ) {
entry_box . buttons . emplace_back ( " Find " , [ this ] ( ) {
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > search_forward ( ) ;
notebook . get_current_v iew( ) - > search_forward ( ) ;
} ) ;
} ) ;
entry_box . buttons . emplace_back ( " Replace " , [ this , replace_entry_it ] ( ) {
entry_box . buttons . emplace_back ( " Replace " , [ this , replace_entry_it ] ( ) {
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > replace_forward ( replace_entry_it - > get_text ( ) ) ;
notebook . get_current_v iew( ) - > replace_forward ( replace_entry_it - > get_text ( ) ) ;
} ) ;
} ) ;
entry_box . buttons . emplace_back ( " Replace all " , [ this , replace_entry_it ] ( ) {
entry_box . buttons . emplace_back ( " Replace all " , [ this , replace_entry_it ] ( ) {
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > replace_all ( replace_entry_it - > get_text ( ) ) ;
notebook . get_current_v iew( ) - > replace_all ( replace_entry_it - > get_text ( ) ) ;
} ) ;
} ) ;
entry_box . toggle_buttons . emplace_back ( " Match case " ) ;
entry_box . toggle_buttons . emplace_back ( " Match case " ) ;
entry_box . toggle_buttons . back ( ) . set_active ( case_sensitive_search ) ;
entry_box . toggle_buttons . back ( ) . set_active ( case_sensitive_search ) ;
entry_box . toggle_buttons . back ( ) . on_activate = [ this , search_entry_it ] ( ) {
entry_box . toggle_buttons . back ( ) . on_activate = [ this , search_entry_it ] ( ) {
case_sensitive_search = ! case_sensitive_search ;
case_sensitive_search = ! case_sensitive_search ;
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
notebook . get_current_v iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
} ;
} ;
entry_box . toggle_buttons . emplace_back ( " Use regex " ) ;
entry_box . toggle_buttons . emplace_back ( " Use regex " ) ;
entry_box . toggle_buttons . back ( ) . set_active ( regex_search ) ;
entry_box . toggle_buttons . back ( ) . set_active ( regex_search ) ;
entry_box . toggle_buttons . back ( ) . on_activate = [ this , search_entry_it ] ( ) {
entry_box . toggle_buttons . back ( ) . on_activate = [ this , search_entry_it ] ( ) {
regex_search = ! regex_search ;
regex_search = ! regex_search ;
if ( notebook . CurrentP age( ) ! = - 1 )
if ( notebook . get_current_p age( ) ! = - 1 )
notebook . CurrentSourceV iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
notebook . get_current_v iew( ) - > search_highlight ( search_entry_it - > get_text ( ) , case_sensitive_search , regex_search ) ;
} ;
} ;
entry_box . signal_hide ( ) . connect ( [ this ] ( ) {
entry_box . signal_hide ( ) . connect ( [ this ] ( ) {
for ( int c = 0 ; c < notebook . Pages ( ) ; c + + ) {
for ( int c = 0 ; c < notebook . size ( ) ; c + + ) {
notebook . source_views . at ( c ) - > view - > update_search_occurrences = nullptr ;
notebook . get_view ( c ) - > update_search_occurrences = nullptr ;
notebook . source_views . at ( c ) - > view - > search_highlight ( " " , case_sensitive_search , regex_search ) ;
notebook . get_view ( c ) - > search_highlight ( " " , case_sensitive_search , regex_search ) ;
}
}
search_entry_shown = false ;
search_entry_shown = false ;
} ) ;
} ) ;
search_entry_shown = true ;
search_entry_shown = true ;
entry_box . show ( ) ;
entry_box . show ( ) ;
}
}
void Window : : rename_token_entry ( ) {
entry_box . clear ( ) ;
if ( notebook . get_current_page ( ) ! = - 1 ) {
if ( notebook . get_current_view ( ) - > get_token & & notebook . get_current_view ( ) - > get_token_name ) {
auto token = std : : make_shared < std : : string > ( notebook . get_current_view ( ) - > get_token ( ) ) ;
if ( token - > size ( ) > 0 & & notebook . get_current_view ( ) - > get_token_name ) {
auto token_name = std : : make_shared < std : : string > ( notebook . get_current_view ( ) - > get_token_name ( ) ) ;
for ( int c = 0 ; c < notebook . size ( ) ; c + + ) {
if ( notebook . get_view ( c ) - > tag_similar_tokens ) {
notebook . get_view ( c ) - > tag_similar_tokens ( * token ) ;
}
}
entry_box . labels . emplace_back ( ) ;
auto label_it = entry_box . labels . begin ( ) ;
label_it - > update = [ label_it ] ( int state , const std : : string & message ) {
label_it - > set_text ( " Warning: only opened and parsed tabs will have its content renamed, and modified files will be saved. " ) ;
} ;
label_it - > update ( 0 , " " ) ;
entry_box . entries . emplace_back ( * token_name , [ this , token_name , token ] ( const std : : string & content ) {
if ( notebook . get_current_page ( ) ! = - 1 & & content ! = * token_name ) {
for ( int c = 0 ; c < notebook . size ( ) ; c + + ) {
if ( notebook . get_view ( c ) - > rename_similar_tokens ) {
auto number = notebook . get_view ( c ) - > rename_similar_tokens ( * token , content ) ;
if ( number > 0 ) {
Singleton : : terminal ( ) - > print ( " Replaced " + std : : to_string ( number ) + " occurrences in file " + notebook . get_view ( c ) - > file_path + " \n " ) ;
notebook . save ( c ) ;
}
}
}
entry_box . hide ( ) ;
}
} ) ;
auto entry_it = entry_box . entries . begin ( ) ;
entry_box . buttons . emplace_back ( " Rename " , [ this , entry_it ] ( ) {
entry_it - > activate ( ) ;
} ) ;
entry_box . show ( ) ;
}
}
}
}