diff --git a/juci/config.json b/juci/config.json index 7c0b9d1..90723a7 100644 --- a/juci/config.json +++ b/juci/config.json @@ -40,8 +40,6 @@ }, "keybindings": { "new_file": "n", - "new_h_file": "h", - "new_cc_file": "c", "open_folder": "o", "open_file": "o", "save": "s", diff --git a/juci/entry.cc b/juci/entry.cc index 011ce6c..429016c 100644 --- a/juci/entry.cc +++ b/juci/entry.cc @@ -1,74 +1,61 @@ #include "entry.h" -Entry::View::View() : - view_(Gtk::ORIENTATION_HORIZONTAL), - button_apply_(Gtk::Stock::APPLY), - button_close_(Gtk::Stock::CLOSE), - button_next_("Next"), - button_prev_("Prev"){ -} -Gtk::Box& Entry::View::view() { - return view_; -} -void Entry::View::OnShowSetFilenName(std::string exstension) { - entry_.set_max_length(50); - entry_.set_text(exstension); - view_.pack_start(entry_); - view_.pack_end(button_close_, Gtk::PACK_SHRINK); - view_.pack_end(button_apply_, Gtk::PACK_SHRINK); -} -void Entry::View::OnShowSearch(std::string current){ - entry_.set_max_length(50); - entry_.set_text(current); - view_.pack_start(entry_); - view_.pack_start(button_next_, Gtk::PACK_SHRINK); - view_.pack_start(button_prev_, Gtk::PACK_SHRINK); - view_.pack_end(button_close_, Gtk::PACK_SHRINK); -} -void Entry::View::OnHideEntry(bool is_new_file) -{ - view_.remove(entry_); - view_.remove(button_close_); - if(!is_new_file){ - view_.remove(button_next_); - view_.remove(button_prev_); - }else{ - view_.remove(button_apply_); - } -} -Entry::Controller::Controller() { -} -Gtk::Box& Entry::Controller::view() { - return view_.view(); -} -void Entry::Controller::OnShowSetFilenName(std::string exstension) { - view_.OnShowSetFilenName(exstension); - view_.view().show_all(); - view_.entry().grab_focus(); - view_.entry().set_position(0); -} -void Entry::Controller::OnShowSearch(std::string current){ - view_.OnShowSearch(current); - view_.view().show_all(); - view_.entry().grab_focus(); - view_.entry().set_position(0); +Entry::Entry() : + Gtk::Box(Gtk::ORIENTATION_HORIZONTAL), + button_apply_set_filename(Gtk::Stock::APPLY), + button_close(Gtk::Stock::CLOSE), + button_next("Next"), + button_prev("Prev"){ + entry.signal_activate().connect([this](){ + if(activate) { + activate(); + } + }); + entry.signal_key_press_event().connect(sigc::mem_fun(*this, &Entry::on_key_press), false); } -void Entry::Controller::OnHideEntries(bool is_new_file){ - view_.OnHideEntry(is_new_file); -} -std::string Entry::Controller::text(){ - return view_.entry().get_text(); -} -Gtk::Button& Entry::Controller::button_apply(){ - return view_.button_apply(); -} -Gtk::Button& Entry::Controller::button_close(){ - return view_.button_close(); + +bool Entry::on_key_press(GdkEventKey* key) { + if(key->keyval==GDK_KEY_Escape) + hide(); + return false; } -Gtk::Button& Entry::Controller::button_next(){ - return view_.button_next(); + +void Entry::show_set_filename() { + hide(); + entry.set_max_length(50); + entry.set_text(""); + pack_start(entry); + pack_end(button_close, Gtk::PACK_SHRINK); + pack_end(button_apply_set_filename, Gtk::PACK_SHRINK); + show_all(); + entry.grab_focus(); + entry.set_position(0); + activate=[this](){ + button_apply_set_filename.clicked(); + }; } -Gtk::Button& Entry::Controller::button_prev(){ - return view_.button_prev(); + +void Entry::show_search(const std::string& current){ + hide(); + entry.set_max_length(50); + entry.set_text(current); + pack_start(entry); + pack_start(button_next, Gtk::PACK_SHRINK); + pack_start(button_prev, Gtk::PACK_SHRINK); + pack_end(button_close, Gtk::PACK_SHRINK); + show_all(); + entry.grab_focus(); + entry.set_position(0); + activate=[this](){ + button_next.clicked(); + }; +} +void Entry::hide() { + auto widgets=get_children(); + for(auto &w: widgets) + remove(*w); } +std::string Entry::operator()() { + return entry.get_text(); +} diff --git a/juci/entry.h b/juci/entry.h index ec8b888..36a45e2 100644 --- a/juci/entry.h +++ b/juci/entry.h @@ -2,43 +2,21 @@ #define JUCI_ENTRY_H_ #include +#include #include "gtkmm.h" -#include "keybindings.h" - -namespace Entry { - class View { - public: - View(); - Gtk::Box& view(); - Gtk::Entry& entry(){return entry_;} - Gtk::Button& button_apply(){return button_apply_;}; - Gtk::Button& button_close(){return button_close_;}; - Gtk::Button& button_next(){return button_next_;}; - Gtk::Button& button_prev(){return button_prev_;}; - void OnShowSetFilenName(std::string exstension); - void OnShowSearch(std::string current); - void OnHideEntry(bool is_new_file); - protected: - Gtk::Box view_; - Gtk::Entry entry_; - Gtk::Button button_apply_, button_close_, button_next_, button_prev_; - }; - class Controller { - public: - Controller(); - Gtk::Box& view(); - Gtk::Button& button_apply(); - Gtk::Button& button_close(); - Gtk::Button& button_next(); - Gtk::Button& button_prev(); - - std::string text(); - void OnShowSetFilenName(std::string exstension); - void OnShowSearch(std::string current); - void OnHideEntries(bool is_new_file); - View view_; - };// class controller -} // namespace notebook +class Entry: public Gtk::Box { +public: + Entry(); + void show_set_filename(); + void show_search(const std::string& current); + void hide(); + std::string operator()(); + Gtk::Entry entry; + Gtk::Button button_apply_set_filename, button_close, button_next, button_prev; +private: + bool on_key_press(GdkEventKey* key); + std::function activate; +}; #endif // JUCI_ENTRY_H_ diff --git a/juci/menu.xml b/juci/menu.xml index 2eab08a..18f82e3 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -1,11 +1,7 @@ - - - - - + diff --git a/juci/notebook.cc b/juci/notebook.cc index d0ba733..c1889d7 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -2,12 +2,6 @@ #include "notebook.h" #include "logging.h" -Notebook::Model::Model() { - cc_extension_ = ".cpp"; - h_extension_ = ".hpp"; - scrollvalue_ = 50; -} - Notebook::View::View() : notebook_() { view_.pack2(notebook_); view_.set_position(120); @@ -40,31 +34,12 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller Gtk::Stock::FILE)); keybindings.action_group_menu()-> - add(Gtk::Action::create("FileNewStandard", - "New empty file"), + add(Gtk::Action::create("FileNewFile", + "New file"), Gtk::AccelKey(keybindings.config_ .key_map()["new_file"]), [this]() { - is_new_file_ = true; - 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(); + OnFileNewFile(); }); keybindings.action_group_menu()-> add(Gtk::Action::create("WindowCloseTab", @@ -80,7 +55,6 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller Gtk::AccelKey(keybindings.config_ .key_map()["edit_find"]), [this]() { - is_new_file_ = false; OnEditSearch(); // TODO(Oyvang) Zalox, Forgi)Create function OnEditFind(); }); @@ -141,33 +115,41 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller INFO("Done Redo"); }); - entry_.view_.entry().signal_activate(). - connect( - [this]() { - if (is_new_file_) { - //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_); - } else { - Search(true); - } - }); - entry_.button_apply().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(). + entry.button_apply_set_filename.signal_clicked().connect([this]() { + std::string filename=entry(); + if(filename!="") { + if(project_path!="" && !boost::filesystem::path(filename).is_absolute()) + filename=project_path+"/"+filename; + boost::filesystem::path p(filename); + if(boost::filesystem::exists(p)) { + //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_close.signal_clicked(). connect( [this]() { - entry_.OnHideEntries(is_new_file_); + entry.hide(); }); - entry_.button_next().signal_clicked(). + entry.button_next.signal_clicked(). connect( [this]() { Search(true); }); - entry_.button_prev().signal_clicked(). + entry.button_prev.signal_clicked(). connect( [this]() { Search(false); @@ -184,9 +166,6 @@ Notebook::Controller::~Controller() { Gtk::Paned& Notebook::Controller::view() { return view_.view(); } -Gtk::Box& Notebook::Controller::entry_view() { - return entry_.view(); -} void Notebook::Controller::OnOpenFile(std::string path) { INFO("Notebook open file"); @@ -233,28 +212,22 @@ void Notebook::Controller::OnCloseCurrentPage() { editor_vec_.erase(editor_vec_.begin()+page); } } -void Notebook::Controller::OnFileNewEmptyfile() { - entry_.OnShowSetFilenName(""); -} -void Notebook::Controller::OnFileNewCCFile() { - entry_.OnShowSetFilenName(model_.cc_extension_); -} -void Notebook::Controller::OnFileNewHeaderFile() { - entry_.OnShowSetFilenName(model_.h_extension_); +void Notebook::Controller::OnFileNewFile() { + entry.show_set_filename(); } void Notebook::Controller::OnEditCopy() { if (Pages() != 0) { - Buffer(*text_vec_.at(CurrentPage()))->copy_clipboard(refClipboard_); + CurrentTextView().get_buffer()->copy_clipboard(refClipboard_); } } void Notebook::Controller::OnEditPaste() { if (Pages() != 0) { - Buffer(*text_vec_.at(CurrentPage()))->paste_clipboard(refClipboard_); + CurrentTextView().get_buffer()->paste_clipboard(refClipboard_); } } void Notebook::Controller::OnEditCut() { if (Pages() != 0) { - Buffer(*text_vec_.at(CurrentPage()))->cut_clipboard(refClipboard_); + CurrentTextView().get_buffer()->cut_clipboard(refClipboard_); } } @@ -263,8 +236,8 @@ std::string Notebook::Controller::GetCursorWord() { 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(); + start = CurrentTextView().get_buffer()->get_insert()->get_iter(); + end = CurrentTextView().get_buffer()->get_insert()->get_iter(); if (!end.ends_line()) { while (!end.ends_word()) { end.forward_char(); @@ -275,28 +248,28 @@ std::string Notebook::Controller::GetCursorWord() { start.backward_char(); } } - word = Buffer(*text_vec_.at(page))->get_text(start, end); + word = CurrentTextView().get_buffer()->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()); + CurrentTextView().get_buffer()->get_iter_at_offset(0); + entry.show_search(GetCursorWord()); } void Notebook::Controller::Search(bool forward) { INFO("Notebook search"); int page = CurrentPage(); std::string search_word; - search_word = entry_.text(); + search_word = entry(); Gtk::TextIter test; if ( !forward ) { if ( search_match_start_ == 0 || search_match_start_.get_line_offset() == 0) { - search_match_start_ = Buffer(*text_vec_.at(CurrentPage()))->end(); + search_match_start_ = CurrentTextView().get_buffer()->end(); } search_match_start_. backward_search(search_word, @@ -306,7 +279,7 @@ void Notebook::Controller::Search(bool forward) { search_match_end_); } else { if ( search_match_end_ == 0 ) { - search_match_end_ = Buffer(*text_vec_.at(CurrentPage()))->begin(); + search_match_end_ = CurrentTextView().get_buffer()->begin(); } search_match_end_. forward_search(search_word, @@ -348,11 +321,6 @@ int Notebook::Controller::CurrentPage() { return Notebook().get_current_page(); } -Glib::RefPtr -Notebook::Controller::Buffer(Source::Controller &source) { - return source.view->get_buffer(); -} - int Notebook::Controller::Pages() { return Notebook().get_n_pages(); } @@ -360,14 +328,6 @@ Gtk::Notebook& Notebook::Controller::Notebook() { return view_.notebook(); } -void Notebook::Controller::BufferChangeHandler(Glib::RefPtr - buffer) { - buffer->signal_end_user_action().connect( - [this]() { - //UpdateHistory(); - }); -} - std::string Notebook::Controller::CurrentPagePath(){ return text_vec_.at(CurrentPage())->view->file_path; } diff --git a/juci/notebook.h b/juci/notebook.h index 98af702..597ec6f 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -11,15 +11,9 @@ #include #include #include "clangmm.h" +#include "keybindings.h" namespace Notebook { - class Model { - public: - Model(); - std::string cc_extension_; - std::string h_extension_; - int scrollvalue_; - }; class View { public: View(); @@ -35,37 +29,29 @@ namespace Notebook { Source::Config& config, Directories::Config& dir_cfg); ~Controller(); - Glib::RefPtr Buffer(Source::Controller &source); Source::View& CurrentTextView(); int CurrentPage(); - Gtk::Box& entry_view(); Gtk::Notebook& Notebook(); std::string CurrentPagePath(); - void OnBufferChange(); - void BufferChangeHandler(Glib::RefPtr - buffer); void OnCloseCurrentPage(); std::string GetCursorWord(); void OnEditCopy(); void OnEditCut(); void OnEditPaste(); void OnEditSearch(); - void OnFileNewCCFile(); - void OnFileNewEmptyfile(); - void OnFileNewHeaderFile(); - void OnFileOpenFolder(); + void OnFileNewFile(); bool OnSaveFile(); bool OnSaveFile(std::string path); void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void OnOpenFile(std::string filename); - bool ScrollEventCallback(GdkEventScroll* scroll_event); int Pages(); Gtk::Paned& view(); void Search(bool forward); std::string OnSaveFileAs(); std::string project_path; - Directories::Controller directories; + Directories::Controller directories; //Todo: make private after creating open_directory() + Entry entry; private: void CreateKeybindings(Keybindings::Controller& keybindings); void AskToSaveDialog(); @@ -73,9 +59,6 @@ namespace Notebook { Glib::RefPtr refActionGroup; Source::Config& source_config; View view_; - Model model_; - bool is_new_file_; //TODO: Remove this - Entry::Controller entry_; std::vector > text_vec_; std::vector scrolledtext_vec_; diff --git a/juci/window.cc b/juci/window.cc index 47ef193..4c61bc0 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -111,7 +111,7 @@ Window::Window() : window_box_.pack_start(menu_.view(), Gtk::PACK_SHRINK); - window_box_.pack_start(notebook_.entry_view(), Gtk::PACK_SHRINK); + window_box_.pack_start(notebook_.entry, Gtk::PACK_SHRINK); paned_.set_position(300); paned_.pack1(notebook_.view(), true, false); paned_.pack2(terminal_.view(), true, true);