Browse Source

Fixed save and save as

master
oyvang 11 years ago
parent
commit
aac51b73ee
  1. 1
      juci/config.json
  2. 1
      juci/menu.xml
  3. 65
      juci/notebook.cc
  4. 5
      juci/notebook.h
  5. 2
      juci/source.cc
  6. 7
      juci/source.h
  7. 37
      juci/window.cc
  8. 3
      juci/window.h

1
juci/config.json

@ -24,6 +24,7 @@
"new_cc_file": "<alt>c",
"close_tab": "<control>w",
"open_folder": "<control><alt>o",
"save": "<control>s",
"save_as": "<control><shift>s",
"compile_and_run": "<control><alt>r>",
"compile": "<control>r"

1
juci/menu.xml

@ -8,6 +8,7 @@
</menu>
<menuitem action='FileOpenFile'/>
<menuitem action='FileOpenFolder'/>
<menuitem action='FileSave'/>
<menuitem action='FileSaveAs'/>
<separator/>
<menuitem action='FileQuit'/>

65
juci/notebook.cc

@ -13,17 +13,24 @@ Notebook::View::View() {
view_.set_position(120);
}
Notebook::Controller::Controller(Gtk::Window& window, Keybindings::Controller& keybindings,
Notebook::Controller::Controller(Gtk::Window* window, Keybindings::Controller& keybindings,
Source::Config& source_cfg,
Directories::Config& dir_cfg) :
source_config_(source_cfg),
directories_(dir_cfg) {
window_ = &window;
std::cout << "NOTEBOOK CONTROLLER"<< std::endl;
window_ = window;
std::cout << "1"<< std::endl;
OnNewPage("juCi++");
std::cout << "2"<< std::endl;
refClipboard_ = Gtk::Clipboard::get();
std::cout << "3"<< std::endl;
ispopup = false;
std::cout << "4"<< std::endl;
view().pack1(directories_.widget(), true, true);
CreateKeybindings(keybindings);
std::cout << "5"<< std::endl;
CreateKeybindings(keybindings);
std::cout << "FERDIG"<< std::endl;
} // Constructor
@ -232,6 +239,7 @@ void Notebook::Controller::OnNewPage(std::string name) {
void Notebook::Controller::OnOpenFile(std::string path) {
OnCreatePage();
text_vec_.back()->OnOpenFile(path);
text_vec_.back()->set_is_saved(true);
unsigned pos = path.find_last_of("/\\");
Notebook().append_page(*editor_vec_.back(), path.substr(pos+1));
Notebook().show_all_children();
@ -519,9 +527,50 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview,
}
}
void Notebook::Controller:: OnSaveFile(std::string path){
std::ofstream file;
file.open (path);
file << CurrentTextView().get_buffer()->get_text();
file.close();
void Notebook::Controller:: OnSaveFile() {
if (text_vec_.at(CurrentPage())->is_saved()) {
std::ofstream file;
file.open (text_vec_.at(CurrentPage())->path());
file << CurrentTextView().get_buffer()->get_text();
file.close();
} else {
std::string path = OnSaveFileAs();
if (path != "") {
std::ofstream file;
file.open (path);
file << CurrentTextView().get_buffer()->get_text();
file.close();
text_vec_.at(CurrentPage())->set_path(path);
text_vec_.at(CurrentPage())->set_is_saved(true);
}
}
}
std::string Notebook::Controller::OnSaveFileAs(){
Gtk::FileChooserDialog dialog("Please choose a file",
Gtk::FILE_CHOOSER_ACTION_SAVE);
dialog.set_transient_for(*window_);
dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS);
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Save", Gtk::RESPONSE_OK);
int result = dialog.run();
switch (result) {
case(Gtk::RESPONSE_OK): {
std::string path = dialog.get_filename();
unsigned pos = path.find_last_of("/\\");
std::cout << path<< std::endl;
//notebook_.OnSaveFile(path);
return path;
break;
}
case(Gtk::RESPONSE_CANCEL): {
break;
}
default: {
std::cout << "Unexpected button clicked." << std::endl;
break;
}
}
return "";
}

5
juci/notebook.h

@ -29,7 +29,7 @@ namespace Notebook {
};
class Controller {
public:
Controller(Gtk::Window& window, Keybindings::Controller& keybindings,
Controller(Gtk::Window* window, Keybindings::Controller& keybindings,
Source::Config& config,
Directories::Config& dir_cfg);
~Controller();
@ -49,7 +49,7 @@ namespace Notebook {
void OnFileNewEmptyfile();
void OnFileNewHeaderFile();
void OnFileOpenFolder();
void OnSaveFile(std::string path);
void OnSaveFile();
void OnDirectoryNavigation(const Gtk::TreeModel::Path& path,
Gtk::TreeViewColumn* column);
void OnNewPage(std::string name);
@ -64,6 +64,7 @@ namespace Notebook {
const Source::Config& source_config() { return source_config_; }
bool OnMouseRelease(GdkEventButton* button);
bool OnKeyRelease(GdkEventKey* key);
std::string OnSaveFileAs();
protected:
void TextViewHandlers(Gtk::TextView& textview);
void PopupSelectHandler(Gtk::Dialog &popup,

2
juci/source.cc

@ -282,6 +282,7 @@ void Source::Controller::OnNewEmptyFile() {
model().set_file_path(filename);
model().set_project_path(filename);
s.save("");
//OnOpenFile(filename); //OYVANG ADDED; REMOVE IF WRONG
}
string extract_file_path(const std::string &file_path) {
@ -334,6 +335,7 @@ void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
}
void Source::Controller::OnOpenFile(const string &filepath) {
path_=filepath;
sourcefile s(filepath);
buffer()->set_text(s.get_content());
int start_offset = buffer()->begin().get_offset();

7
juci/source.h

@ -129,13 +129,20 @@ namespace Source {
int column,
std::vector<std::string> *suggestions);
Glib::RefPtr<Gtk::TextBuffer> buffer();
bool is_saved(){return is_saved_;};
std::string path(){return path_;};
void set_is_saved(bool isSaved){is_saved_ = isSaved;};
void set_path(std::string path){path_ = path;};
private:
void OnLineEdit();
void OnSaveFile();
std::mutex syntax;
std::mutex parsing;
std::string path_;
bool go = false;
bool is_saved_ = false;
protected:
View view_;

37
juci/window.cc

@ -4,7 +4,7 @@ Window::Window() :
window_box_(Gtk::ORIENTATION_VERTICAL),
main_config_(),
keybindings_(main_config_.keybindings_cfg()),
notebook_(*this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()),
notebook_(this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()),
menu_(keybindings()) {
set_title("juCi++");
set_default_size(600, 400);
@ -32,7 +32,15 @@ Window::Window() :
Gtk::AccelKey(keybindings_.config_
.key_map()["save_as"]),
[this]() {
OnSaveFileAs();
notebook_.OnSaveFile();
});
keybindings_.action_group_menu()->add(Gtk::Action::create("FileSave",
"Save"),
Gtk::AccelKey(keybindings_.config_
.key_map()["save"]),
[this]() {
notebook_.OnSaveFile();
});
keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompileAndRun",
@ -165,31 +173,6 @@ void Window::OnOpenFile() {
}
}
}
void Window::OnSaveFileAs(){
Gtk::FileChooserDialog dialog("Please choose a file",
Gtk::FILE_CHOOSER_ACTION_SAVE);
dialog.set_transient_for(*this);
dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS);
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Save", Gtk::RESPONSE_OK);
int result = dialog.run();
switch (result) {
case(Gtk::RESPONSE_OK): {
std::string path = dialog.get_filename();
unsigned pos = path.find_last_of("/\\");
std::cout << path<< std::endl;
notebook_.OnSaveFile(path);
break;
}
case(Gtk::RESPONSE_CANCEL): {
break;
}
default: {
std::cout << "Unexpected button clicked." << std::endl;
break;
}
}
}
bool Window::OnMouseRelease(GdkEventButton *button){
return notebook_.OnMouseRelease(button);
}

3
juci/window.h

@ -11,6 +11,7 @@ class Window : public Gtk::Window {
public:
Window();
MainConfig& main_config() { return main_config_; }
// std::string OnSaveFileAs();
Gtk::Box window_box_;
@ -28,7 +29,7 @@ public:
void OnWindowHide();
void OnOpenFile();
void OnFileOpenFolder();
void OnSaveFileAs();
bool OnMouseRelease(GdkEventButton* button);
};

Loading…
Cancel
Save