@ -1,28 +1,24 @@
# include <fstream>
# include <fstream>
# include "notebook.h"
# include "notebook.h"
# include "logging.h"
# include "logging.h"
# include <gtksourceview/gtksource.h> // c-library
Notebook : : Model : : Model ( ) {
cc_extension_ = " .cpp " ;
h_extension_ = " .hpp " ;
scrollvalue_ = 50 ;
}
Notebook : : View : : View ( ) : notebook_ ( ) {
Notebook : : View : : View ( ) {
view_ . pack2 ( notebook_ ) ;
pack2 ( notebook ) ;
view_ . set_position ( 120 ) ;
set_position ( 120 ) ;
}
}
Notebook : : Controller : : Controller ( Gtk : : Window * window ,
Notebook : : Controller : : Controller ( Keybindings : : Controller & keybindings ,
Keybindings : : Controller & keybindings ,
Terminal : : Controller & terminal ,
Source : : Config & source_cfg ,
Source : : Config & source_cfg ,
Directories : : Config & dir_cfg ) :
Directories : : Config & dir_cfg ) :
terminal ( terminal ) ,
directories ( dir_cfg ) ,
directories ( dir_cfg ) ,
source_config ( source_cfg ) {
source_config ( source_cfg ) {
INFO ( " Create notebook " ) ;
INFO ( " Create notebook " ) ;
window_ = window ;
refClipboard_ = Gtk : : Clipboard : : get ( ) ;
refClipboard_ = Gtk : : Clipboard : : get ( ) ;
view ( ) . pack1 ( directories . widget ( ) , true , true ) ;
view . pack1 ( directories . widget ( ) , true , true ) ;
CreateKeybindings ( keybindings ) ;
CreateKeybindings ( keybindings ) ;
INFO ( " Notebook Controller Success " ) ;
INFO ( " Notebook Controller Success " ) ;
} // Constructor
} // Constructor
@ -40,31 +36,12 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
Gtk : : Stock : : FILE ) ) ;
Gtk : : Stock : : FILE ) ) ;
keybindings . action_group_menu ( ) - >
keybindings . action_group_menu ( ) - >
add ( Gtk : : Action : : create ( " FileNewStandard " ,
add ( Gtk : : Action : : create ( " FileNewFile " ,
" New empty file " ) ,
" New file " ) ,
Gtk : : AccelKey ( keybindings . config_
Gtk : : AccelKey ( keybindings . config_
. key_map ( ) [ " new_file " ] ) ,
. key_map ( ) [ " new_file " ] ) ,
[ this ] ( ) {
[ this ] ( ) {
is_new_file_ = true ;
OnFileNewFile ( ) ;
OnFileNewEmptyfile ( ) ;
} ) ;
keybindings . action_group_menu ( ) - >
add ( Gtk : : Action : : create ( " FileNewCC " ,
" New source file " ) ,
Gtk : : AccelKey ( keybindings . config_
. key_map ( ) [ " new_cc_file " ] ) ,
[ this ] ( ) {
is_new_file_ = true ;
OnFileNewCCFile ( ) ;
} ) ;
keybindings . action_group_menu ( ) - >
add ( Gtk : : Action : : create ( " FileNewH " ,
" New header file " ) ,
Gtk : : AccelKey ( keybindings . config_
. key_map ( ) [ " new_h_file " ] ) ,
[ this ] ( ) {
is_new_file_ = true ;
OnFileNewHeaderFile ( ) ;
} ) ;
} ) ;
keybindings . action_group_menu ( ) - >
keybindings . action_group_menu ( ) - >
add ( Gtk : : Action : : create ( " WindowCloseTab " ,
add ( Gtk : : Action : : create ( " WindowCloseTab " ,
@ -80,9 +57,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
Gtk : : AccelKey ( keybindings . config_
Gtk : : AccelKey ( keybindings . config_
. key_map ( ) [ " edit_find " ] ) ,
. key_map ( ) [ " edit_find " ] ) ,
[ this ] ( ) {
[ this ] ( ) {
is_new_file_ = false ;
entry . show_search ( " " ) ;
OnEditSearch ( ) ;
// TODO(Oyvang) Zalox, Forgi)Create function OnEditFind();
} ) ;
} ) ;
keybindings . action_group_menu ( ) - >
keybindings . action_group_menu ( ) - >
add ( Gtk : : Action : : create ( " EditCopy " ,
add ( Gtk : : Action : : create ( " EditCopy " ,
@ -91,7 +66,9 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
. key_map ( ) [ " edit_copy " ] ) ,
. key_map ( ) [ " edit_copy " ] ) ,
[ this ] ( ) {
[ this ] ( ) {
OnEditCopy ( ) ;
if ( Pages ( ) ! = 0 ) {
CurrentTextView ( ) . get_buffer ( ) - > copy_clipboard ( refClipboard_ ) ;
}
} ) ;
} ) ;
keybindings . action_group_menu ( ) - >
keybindings . action_group_menu ( ) - >
add ( Gtk : : Action : : create ( " EditCut " ,
add ( Gtk : : Action : : create ( " EditCut " ,
@ -99,7 +76,9 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
Gtk : : AccelKey ( keybindings . config_
Gtk : : AccelKey ( keybindings . config_
. key_map ( ) [ " edit_cut " ] ) ,
. key_map ( ) [ " edit_cut " ] ) ,
[ this ] ( ) {
[ this ] ( ) {
OnEditCut ( ) ;
if ( Pages ( ) ! = 0 ) {
CurrentTextView ( ) . get_buffer ( ) - > cut_clipboard ( refClipboard_ ) ;
}
} ) ;
} ) ;
keybindings . action_group_menu ( ) - >
keybindings . action_group_menu ( ) - >
add ( Gtk : : Action : : create ( " EditPaste " ,
add ( Gtk : : Action : : create ( " EditPaste " ,
@ -107,7 +86,9 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
Gtk : : AccelKey ( keybindings . config_
Gtk : : AccelKey ( keybindings . config_
. key_map ( ) [ " edit_paste " ] ) ,
. key_map ( ) [ " edit_paste " ] ) ,
[ this ] ( ) {
[ this ] ( ) {
OnEditPaste ( ) ;
if ( Pages ( ) ! = 0 ) {
CurrentTextView ( ) . get_buffer ( ) - > paste_clipboard ( refClipboard_ ) ;
}
} ) ;
} ) ;
keybindings . action_group_menu ( ) - >
keybindings . action_group_menu ( ) - >
@ -141,36 +122,44 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
INFO ( " Done Redo " ) ;
INFO ( " Done Redo " ) ;
} ) ;
} ) ;
entry_ . view_ . entry ( ) . signal_activate ( ) .
entry . button_apply_set_filename . signal_clicked ( ) . connect ( [ this ] ( ) {
conn ec t(
std : : string fil ename = en try ( ) ;
[ this ] ( ) {
if ( filename ! = " " ) {
if ( is_new_file_ ) {
if ( project_path ! = " " & & ! boost : : filesystem : : path ( filename ) . is_absolute ( ) )
//OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation)
filename = project_path + " / " + filename ;
entry_ . OnHideEntries ( is_new_file_ ) ;
boost : : filesystem : : path p ( filename ) ;
} else {
if ( boost : : filesystem : : exists ( p ) ) {
Search ( true ) ;
//TODO: alert user that file already exists
}
}
else {
std : : ofstream f ( p . string ( ) . c_str ( ) ) ;
if ( f ) {
OnOpenFile ( boost : : filesystem : : canonical ( p ) . string ( ) ) ;
if ( project_path ! = " " )
directories . open_folder ( project_path ) ; //TODO: Do refresh instead
}
else {
//TODO: alert user of error creating file
}
f . close ( ) ;
}
}
entry . hide ( ) ;
} ) ;
} ) ;
entry_ . button_apply ( ) . signal_clicked ( ) .
entry . button_close . signal_clicked ( ) .
connect (
[ this ] ( ) {
//OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation)
entry_ . OnHideEntries ( is_new_file_ ) ;
} ) ;
entry_ . button_close ( ) . signal_clicked ( ) .
connect (
connect (
[ this ] ( ) {
[ this ] ( ) {
entry_ . OnHideEntries ( is_new_file_ ) ;
entry . hide ( ) ;
} ) ;
} ) ;
entry_ . button_next ( ) . signal_clicked ( ) .
entry . button_next . signal_clicked ( ) .
connect (
connect (
[ this ] ( ) {
[ this ] ( ) {
S earch( true ) ;
s earch( true ) ;
} ) ;
} ) ;
entry_ . button_prev ( ) . signal_clicked ( ) .
entry . button_prev . signal_clicked ( ) .
connect (
connect (
[ this ] ( ) {
[ this ] ( ) {
S earch( false ) ;
s earch( false ) ;
} ) ;
} ) ;
INFO ( " Notebook signal handlers sucsess " ) ;
INFO ( " Notebook signal handlers sucsess " ) ;
}
}
@ -181,22 +170,15 @@ Notebook::Controller::~Controller() {
for ( auto & i : scrolledtext_vec_ ) delete i ;
for ( auto & i : scrolledtext_vec_ ) delete i ;
}
}
Gtk : : Paned & Notebook : : Controller : : view ( ) {
return view_ . view ( ) ;
}
Gtk : : Box & Notebook : : Controller : : entry_view ( ) {
return entry_ . view ( ) ;
}
void Notebook : : Controller : : OnOpenFile ( std : : string path ) {
void Notebook : : Controller : : OnOpenFile ( std : : string path ) {
INFO ( " Notebook open file " ) ;
INFO ( " Notebook open file " ) ;
INFO ( " Notebook create page " ) ;
INFO ( " Notebook create page " ) ;
text_vec_ . emplace_back ( new Source : : Controller ( source_config , path , project_path ) ) ;
text_vec_ . emplace_back ( new Source : : Controller ( source_config , path , project_path , terminal ) ) ;
scrolledtext_vec_ . push_back ( new Gtk : : ScrolledWindow ( ) ) ;
scrolledtext_vec_ . push_back ( new Gtk : : ScrolledWindow ( ) ) ;
editor_vec_ . push_back ( new Gtk : : HBox ( ) ) ;
editor_vec_ . push_back ( new Gtk : : HBox ( ) ) ;
scrolledtext_vec_ . back ( ) - > add ( * text_vec_ . back ( ) - > view ) ;
scrolledtext_vec_ . back ( ) - > add ( * text_vec_ . back ( ) - > view ) ;
editor_vec_ . back ( ) - > pack_start ( * scrolledtext_vec_ . back ( ) , true , true ) ;
editor_vec_ . back ( ) - > pack_start ( * scrolledtext_vec_ . back ( ) , true , true ) ;
size_t pos = path . find_last_of ( " / \\ " ) ;
size_t pos = path . find_last_of ( " / \\ " ) ; // TODO #windows
std : : string filename = path ;
std : : string filename = path ;
if ( pos ! = std : : string : : npos )
if ( pos ! = std : : string : : npos )
filename = path . substr ( pos + 1 ) ;
filename = path . substr ( pos + 1 ) ;
@ -207,7 +189,7 @@ void Notebook::Controller::OnOpenFile(std::string path) {
//Add star on tab label when the page is not saved:
//Add star on tab label when the page is not saved:
text_vec_ . back ( ) - > buffer ( ) - > signal_changed ( ) . connect ( [ this ] ( ) {
text_vec_ . back ( ) - > buffer ( ) - > signal_changed ( ) . connect ( [ this ] ( ) {
if ( text_vec_ . at ( CurrentPage ( ) ) - > is_saved ) {
if ( text_vec_ . at ( CurrentPage ( ) ) - > is_saved ) {
std : : string path = text_vec_ . at ( CurrentPage ( ) ) - > view - > file_path ;
std : : string path = CurrentTextView ( ) . file_path ;
size_t pos = path . find_last_of ( " / \\ " ) ;
size_t pos = path . find_last_of ( " / \\ " ) ;
std : : string filename = path ;
std : : string filename = path ;
if ( pos ! = std : : string : : npos )
if ( pos ! = std : : string : : npos )
@ -233,88 +215,42 @@ void Notebook::Controller::OnCloseCurrentPage() {
editor_vec_ . erase ( editor_vec_ . begin ( ) + page ) ;
editor_vec_ . erase ( editor_vec_ . begin ( ) + page ) ;
}
}
}
}
void Notebook : : Controller : : OnFileNewEmptyfile ( ) {
void Notebook : : Controller : : OnFileNewFile ( ) {
entry_ . OnShowSetFilenName ( " " ) ;
entry . show_set_filename ( ) ;
}
void Notebook : : Controller : : OnFileNewCCFile ( ) {
entry_ . OnShowSetFilenName ( model_ . cc_extension_ ) ;
}
void Notebook : : Controller : : OnFileNewHeaderFile ( ) {
entry_ . OnShowSetFilenName ( model_ . h_extension_ ) ;
}
void Notebook : : Controller : : OnEditCopy ( ) {
if ( Pages ( ) ! = 0 ) {
Buffer ( * text_vec_ . at ( CurrentPage ( ) ) ) - > copy_clipboard ( refClipboard_ ) ;
}
}
void Notebook : : Controller : : OnEditPaste ( ) {
if ( Pages ( ) ! = 0 ) {
Buffer ( * text_vec_ . at ( CurrentPage ( ) ) ) - > paste_clipboard ( refClipboard_ ) ;
}
}
void Notebook : : Controller : : OnEditCut ( ) {
if ( Pages ( ) ! = 0 ) {
Buffer ( * text_vec_ . at ( CurrentPage ( ) ) ) - > cut_clipboard ( refClipboard_ ) ;
}
}
std : : string Notebook : : Controller : : GetCursorWord ( ) {
INFO ( " Notebook get cursor word " ) ;
int page = CurrentPage ( ) ;
std : : string word ;
Gtk : : TextIter start , end ;
start = Buffer ( * text_vec_ . at ( page ) ) - > get_insert ( ) - > get_iter ( ) ;
end = Buffer ( * text_vec_ . at ( page ) ) - > get_insert ( ) - > get_iter ( ) ;
if ( ! end . ends_line ( ) ) {
while ( ! end . ends_word ( ) ) {
end . forward_char ( ) ;
}
}
if ( ! start . starts_line ( ) ) {
while ( ! start . starts_word ( ) ) {
start . backward_char ( ) ;
}
}
word = Buffer ( * text_vec_ . at ( page ) ) - > get_text ( start , end ) ;
// TODO(Oyvang) fix selected text
return word ;
}
void Notebook : : Controller : : OnEditSearch ( ) {
search_match_end_ =
Buffer ( * text_vec_ . at ( CurrentPage ( ) ) ) - > get_iter_at_offset ( 0 ) ;
entry_ . OnShowSearch ( GetCursorWord ( ) ) ;
}
}
void Notebook : : Controller : : S earch( bool forward ) {
void Notebook : : Controller : : search ( bool forward ) {
INFO ( " Notebook search " ) ;
INFO ( " Notebook search " ) ;
int page = CurrentPage ( ) ;
auto start = CurrentTextView ( ) . search_start ;
std : : string search_word ;
auto end = CurrentTextView ( ) . search_end ;
search_word = entry_ . text ( ) ;
// fetch buffer and greate settings
Gtk : : TextIter test ;
auto buffer = CurrentTextView ( ) . get_source_buffer ( ) ;
auto settings = gtk_source_search_settings_new ( ) ;
if ( ! forward ) {
// get search text from entry
if ( search_match_start_ = = 0 | |
gtk_source_search_settings_set_search_text ( settings , entry ( ) . c_str ( ) ) ;
search_match_start_ . get_line_offset ( ) = = 0 ) {
// make sure the search continues
search_match_start_ = Buffer ( * text_vec_ . at ( CurrentPage ( ) ) ) - > end ( ) ;
gtk_source_search_settings_set_wrap_around ( settings , true ) ;
}
auto context = gtk_source_search_context_new ( buffer - > gobj ( ) , settings ) ;
search_match_start_ .
gtk_source_search_context_set_highlight ( context , forward ) ;
backward_search ( search_word ,
auto itr = buffer - > get_insert ( ) - > get_iter ( ) ;
Gtk : : TextSearchFlags : : TEXT_SEARCH_TEXT_ONLY |
buffer - > remove_tag_by_name ( " search " , start ? start : itr , end ? end : itr ) ;
Gtk : : TextSearchFlags : : TEXT_SEARCH_VISIBLE_ONLY ,
if ( forward ) {
search_match_start_ ,
DEBUG ( " Doing forward search " ) ;
search_match_end_ ) ;
gtk_source_search_context_forward ( context ,
end ? end . gobj ( ) : itr . gobj ( ) ,
start . gobj ( ) ,
end . gobj ( ) ) ;
} else {
} else {
if ( search_match_end_ = = 0 ) {
DEBUG ( " Doing backward search " ) ;
search_match_end_ = Buffer ( * text_vec_ . at ( CurrentPage ( ) ) ) - > begin ( ) ;
gtk_source_search_context_backward ( context ,
}
start ? start . gobj ( ) : itr . gobj ( ) ,
search_match_end_ .
start . gobj ( ) ,
forward_search ( search_word ,
end . gobj ( ) ) ;
Gtk : : TextSearchFlags : : TEXT_SEARCH_TEXT_ONLY |
Gtk : : TextSearchFlags : : TEXT_SEARCH_VISIBLE_ONLY ,
search_match_start_ ,
search_match_end_ ) ;
}
}
buffer - > apply_tag_by_name ( " search " , start , end ) ;
CurrentTextView ( ) . scroll_to ( end ) ;
CurrentTextView ( ) . search_start = start ;
CurrentTextView ( ) . search_end = end ;
}
}
void Notebook : : Controller
void Notebook : : Controller
@ -348,32 +284,15 @@ int Notebook::Controller::CurrentPage() {
return Notebook ( ) . get_current_page ( ) ;
return Notebook ( ) . get_current_page ( ) ;
}
}
Glib : : RefPtr < Gtk : : TextBuffer >
Notebook : : Controller : : Buffer ( Source : : Controller & source ) {
return source . view - > get_buffer ( ) ;
}
int Notebook : : Controller : : Pages ( ) {
int Notebook : : Controller : : Pages ( ) {
return Notebook ( ) . get_n_pages ( ) ;
return Notebook ( ) . get_n_pages ( ) ;
}
}
Gtk : : Notebook & Notebook : : Controller : : Notebook ( ) {
Gtk : : Notebook & Notebook : : Controller : : Notebook ( ) {
return view_ . notebook ( ) ;
return view . notebook ;
}
void Notebook : : Controller : : BufferChangeHandler ( Glib : : RefPtr < Gtk : : TextBuffer >
buffer ) {
buffer - > signal_end_user_action ( ) . connect (
[ this ] ( ) {
//UpdateHistory();
} ) ;
}
std : : string Notebook : : Controller : : CurrentPagePath ( ) {
return text_vec_ . at ( CurrentPage ( ) ) - > view - > file_path ;
}
}
bool Notebook : : Controller : : OnSaveFile ( ) {
bool Notebook : : Controller : : OnSaveFile ( ) {
std : : string path = text_vec_ . at ( CurrentPage ( ) ) - > view - > file_path ;
std : : string path = CurrentTextView ( ) . file_path ;
return OnSaveFile ( path ) ;
return OnSaveFile ( path ) ;
}
}
bool Notebook : : Controller : : OnSaveFile ( std : : string path ) {
bool Notebook : : Controller : : OnSaveFile ( std : : string path ) {
@ -383,7 +302,7 @@ bool Notebook::Controller:: OnSaveFile(std::string path) {
file . open ( path ) ;
file . open ( path ) ;
file < < CurrentTextView ( ) . get_buffer ( ) - > get_text ( ) ;
file < < CurrentTextView ( ) . get_buffer ( ) - > get_text ( ) ;
file . close ( ) ;
file . close ( ) ;
text_vec_ . at ( CurrentPage ( ) ) - > view - > file_path = path ;
CurrentTextView ( ) . file_path = path ;
size_t pos = path . find_last_of ( " / \\ " ) ;
size_t pos = path . find_last_of ( " / \\ " ) ;
std : : string filename = path ;
std : : string filename = path ;
if ( pos ! = std : : string : : npos )
if ( pos ! = std : : string : : npos )
@ -398,10 +317,9 @@ bool Notebook::Controller:: OnSaveFile(std::string path) {
std : : string Notebook : : Controller : : OnSaveFileAs ( ) {
std : : string Notebook : : Controller : : OnSaveFileAs ( ) {
INFO ( " Notebook save as " ) ;
INFO ( " Notebook save as " ) ;
Gtk : : FileChooserDialog dialog ( " Please choose a file " ,
Gtk : : FileChooserDialog dialog ( ( Gtk : : Window & ) ( * view . get_toplevel ( ) ) , " Please choose a file " ,
Gtk : : FILE_CHOOSER_ACTION_SAVE ) ;
Gtk : : FILE_CHOOSER_ACTION_SAVE ) ;
DEBUG ( " SET TRANSISTEN FPR " ) ;
DEBUG ( " SET TRANSISTEN FPR " ) ;
dialog . set_transient_for ( * window_ ) ;
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 ) ;
@ -413,7 +331,6 @@ std::string Notebook::Controller::OnSaveFileAs(){
case ( Gtk : : RESPONSE_OK ) : {
case ( Gtk : : RESPONSE_OK ) : {
DEBUG ( " get_filename() " ) ;
DEBUG ( " get_filename() " ) ;
std : : string path = dialog . get_filename ( ) ;
std : : string path = dialog . get_filename ( ) ;
unsigned pos = path . find_last_of ( " / \\ " ) ;
return path ;
return path ;
}
}
case ( Gtk : : RESPONSE_CANCEL ) : {
case ( Gtk : : RESPONSE_CANCEL ) : {
@ -430,11 +347,11 @@ std::string Notebook::Controller::OnSaveFileAs(){
void Notebook : : Controller : : AskToSaveDialog ( ) {
void Notebook : : Controller : : AskToSaveDialog ( ) {
INFO ( " AskToSaveDialog " ) ;
INFO ( " AskToSaveDialog " ) ;
DEBUG ( " AskToSaveDialog: Finding file path " ) ;
DEBUG ( " AskToSaveDialog: Finding file path " ) ;
Gtk : : MessageDialog dialog ( * window_ , " Save file! " ,
Gtk : : MessageDialog dialog ( ( Gtk : : Window & ) ( * view . get_toplevel ( ) ) , " Save file! " ,
false , Gtk : : MESSAGE_QUESTION , Gtk : : BUTTONS_YES_NO ) ;
false , Gtk : : MESSAGE_QUESTION , Gtk : : BUTTONS_YES_NO ) ;
dialog . set_secondary_text (
dialog . set_secondary_text (
" Do you want to save: " +
" Do you want to save: " +
text_vec_ . at ( CurrentPage ( ) ) - > view - > file_path + " ? " ) ;
CurrentTextView ( ) . file_path + " ? " ) ;
DEBUG ( " AskToSaveDialog: run dialog " ) ;
DEBUG ( " AskToSaveDialog: run dialog " ) ;
int result = dialog . run ( ) ;
int result = dialog . run ( ) ;