Browse Source

working undo redo, not optimal, will create a more efficient version now

master
tedjk 11 years ago
parent
commit
445f63210a
  1. 3
      juci/config.json
  2. 5
      juci/menu.xml
  3. 106
      juci/notebook.cc
  4. 14
      juci/notebook.h

3
juci/config.json

@ -23,7 +23,8 @@
"new_h_file": "<control><alt>h", "new_h_file": "<control><alt>h",
"new_cc_file": "<alt>c", "new_cc_file": "<alt>c",
"close_tab": "<control>w", "close_tab": "<control>w",
"open_folder": "<control><alt>o" "open_folder": "<control><alt>o",
"edit_undo": "<control>z"
}, },
"directoryfilter": { "directoryfilter": {
"ignore": [ "ignore": [

5
juci/menu.xml

@ -16,7 +16,8 @@
<menuitem action='EditCut'/> <menuitem action='EditCut'/>
<menuitem action='EditPaste'/> <menuitem action='EditPaste'/>
<separator/> <separator/>
<menuitem action='EditFind'/> <menuitem action='EditFind'/>
<menuitem action='EditUndo'/>
</menu> </menu>
<menu action='WindowMenu'> <menu action='WindowMenu'>
<menuitem action='WindowCloseTab'/> <menuitem action='WindowCloseTab'/>
@ -26,6 +27,6 @@
</menu> </menu>
<menu action='HelpMenu'> <menu action='HelpMenu'>
<menuitem action='HelpAbout'/> <menuitem action='HelpAbout'/>
</menu> </menu>
</menubar> </menubar>
</ui> </ui>

106
juci/notebook.cc

@ -93,6 +93,16 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
[this]() { [this]() {
OnEditPaste(); OnEditPaste();
}); });
keybindings.action_group_menu()->
add(Gtk::Action::create("EditUndo",
"Undo"),
Gtk::AccelKey(keybindings.config_
.key_map()["edit_undo"]),
[this]() {
OnUndo();
});
keybindings.action_group_hidden()-> keybindings.action_group_hidden()->
add(Gtk::Action::create("EditPaste", add(Gtk::Action::create("EditPaste",
Gtk::Stock::PASTE), Gtk::Stock::PASTE),
@ -219,6 +229,7 @@ void Notebook::Controller::OnNewPage(std::string name) {
Notebook().show_all_children(); Notebook().show_all_children();
Notebook().set_current_page(Pages()-1); Notebook().set_current_page(Pages()-1);
Notebook().set_focus_child(text_vec_.at(Pages()-1)->view()); Notebook().set_focus_child(text_vec_.at(Pages()-1)->view());
NewBufferHistory(text_vec_.back()->view().get_buffer());
} }
void Notebook::Controller::OnOpenFile(std::string path) { void Notebook::Controller::OnOpenFile(std::string path) {
@ -232,6 +243,9 @@ void Notebook::Controller::OnOpenFile(std::string path) {
std::cout << "current page set" << std::endl; std::cout << "current page set" << std::endl;
Notebook().set_focus_child(text_vec_.back()->view()); Notebook().set_focus_child(text_vec_.back()->view());
OnBufferChange(); OnBufferChange();
NewBufferHistory(text_vec_.back()->view().get_buffer());
} }
void Notebook::Controller::OnCreatePage() { void Notebook::Controller::OnCreatePage() {
@ -383,13 +397,15 @@ void Notebook::Controller::OnBufferChange() {
delete scroll; delete scroll;
} }
Gtk::TextIter start, end; Gtk::TextIter start, end;
std::string word; std::string word, last_word;
start = Buffer(text_vec_.at(page))->get_insert()->get_iter(); start = Buffer(text_vec_.at(page))->get_insert()->get_iter();
end = Buffer(text_vec_.at(page))->get_insert()->get_iter(); end = Buffer(text_vec_.at(page))->get_insert()->get_iter();
start.backward_char(); start.backward_char();
word = Buffer(text_vec_.at(page))->get_text(start, end); word = Buffer(text_vec_.at(page))->get_text(start, end);
last_word = Buffer(text_vec_.at(page))->get_text(--start, --end);
if (word == ".") { if (word == ".") {
// TODO(Forgie) Zalox,Forgie) Remove TEST // TODO(Forgie) Zalox,Forgie) Remove TEST
UpdateHistory();
std::vector<std::string> TEST; std::vector<std::string> TEST;
TEST.push_back("toString()"); TEST.push_back("toString()");
TEST.push_back("toLower()"); TEST.push_back("toLower()");
@ -446,5 +462,93 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr<Gtk::TextBuffer>
[this]() { [this]() {
OnBufferChange(); OnBufferChange();
}); });
buffer->signal_end_user_action().connect(
[this]() {
UpdateHistory();
});
}
// History methods
void Notebook::Controller::
NewBufferHistory(Glib::RefPtr<Gtk::TextBuffer> buffer) {
Glib::ustring text = buffer->get_text();
std::deque<Glib::ustring> queue;
queue.push_back(text);
history_.push_back(queue);
} }
void Notebook::Controller::UpdateHistory() {
Gtk::TextIter start, end;
std::string word, last_word;
int page = CurrentPage();
start = Buffer(text_vec_.at(page))->get_insert()->get_iter();
end = Buffer(text_vec_.at(page))->get_insert()->get_iter();
start.backward_char();
word = Buffer(text_vec_.at(page))->get_text(start, end);
last_word = Buffer(text_vec_.at(page))->get_text(--start, --end);
/*if(word == "."
|| word == " "
|| word == ";"
|| word == ":"
|| word == "}"
|| word == ")"
|| word == "]"
|| word == ">") {
if(last_word != "."
&& last_word != " "
&& last_word != ";"
&& last_word != ":"
&& last_word != "}"
&& last_word != ")"
&& last_word != "]"
&& last_word != ">") {*/
AppendBufferState();
// }
// }
}
void Notebook::Controller::
AppendBufferState() {
Glib::ustring text = CurrentTextView().get_buffer()->get_text();
std::cout << "buf.size(): " << text.size() << std::endl;
if(BufferHistory().size() < kHistorySize) {
BufferHistory().push_back(text);
} else {
BufferHistory().pop_front();
BufferHistory().push_back(text);
}
}
void Notebook::Controller::RemoveBufferHistory() {
history_.erase(history_.begin()+CurrentPage());
}
std::deque<Glib::ustring>& Notebook::Controller::BufferHistory() {
return history_.at(CurrentPage());
}
Glib::ustring& Notebook::Controller::LastBufferState() {
if(BufferHistory().size() > 1) {
BufferHistory().pop_back();
}else {
std::cout << "Reached end of history, can't undo any more" << std::endl;
}
return BufferHistory().back();
}
void Notebook::Controller::OnUndo() {
// PrintQue();
// std::cout << "UNDOING SOMETHING TERRIBLE" << std::endl;
Glib::RefPtr<Gtk::TextBuffer> buf = CurrentTextView().get_buffer();
buf->set_text(LastBufferState());
std::cout << "Undoing.."<< std::endl;
PrintQue();
}
void Notebook::Controller::PrintQue(){
int size = BufferHistory().back().size();
std::cout << "buffer size: "<< size << std::endl
<< "buffer #: " << CurrentPage() << std::endl
<< "historylength: "<< BufferHistory().size() << std::endl;
}
//TODO remove first history event after open file

14
juci/notebook.h

@ -9,6 +9,7 @@
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <type_traits> #include <type_traits>
#include <sigc++/sigc++.h> #include <sigc++/sigc++.h>
#include <deque>
namespace Notebook { namespace Notebook {
class Model { class Model {
@ -69,7 +70,19 @@ namespace Notebook {
protected: protected:
void BufferChangeHandler(Glib::RefPtr<Gtk::TextBuffer> buffer); void BufferChangeHandler(Glib::RefPtr<Gtk::TextBuffer> buffer);
private: private:
std::vector<std::deque<Glib::ustring>> history_;
const int kHistorySize = 30;
void NewBufferHistory(Glib::RefPtr<Gtk::TextBuffer> buffer);
void RemoveBufferHistory();
void UpdateHistory();
void AppendBufferState();
std::deque<Glib::ustring>& BufferHistory();
void PrintQue();
Glib::ustring& LastBufferState();
void OnUndo();
void CreateKeybindings(Keybindings::Controller& keybindings); void CreateKeybindings(Keybindings::Controller& keybindings);
Glib::RefPtr<Gtk::Builder> m_refBuilder; Glib::RefPtr<Gtk::Builder> m_refBuilder;
Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup; Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup;
@ -79,6 +92,7 @@ namespace Notebook {
Model model_; Model model_;
bool is_new_file_; bool is_new_file_;
Entry::Controller entry_; Entry::Controller entry_;
std::vector<Source::Controller*> text_vec_, linenumbers_vec_; std::vector<Source::Controller*> text_vec_, linenumbers_vec_;
std::vector<Gtk::ScrolledWindow*> scrolledtext_vec_, scrolledline_vec_; std::vector<Gtk::ScrolledWindow*> scrolledtext_vec_, scrolledline_vec_;
std::vector<Gtk::HBox*> editor_vec_; std::vector<Gtk::HBox*> editor_vec_;

Loading…
Cancel
Save