Browse Source

Further cleanup of window.* and notebook.*. Also now using gtksourceview to identify c/c++/objc files.

merge-requests/365/head
eidheim 11 years ago
parent
commit
a4f57060cb
  1. 3
      juci/api.cc
  2. 37
      juci/config.cc
  3. 12
      juci/config.json
  4. 2
      juci/juci.cc
  5. 125
      juci/notebook.cc
  6. 16
      juci/notebook.h
  7. 67
      juci/source.cc
  8. 15
      juci/source.h
  9. 141
      juci/window.cc
  10. 3
      juci/window.h

3
juci/api.cc

@ -211,8 +211,7 @@ void libjuci::IterToWordEnd(Gtk::TextIter &iter) {
}
Glib::RefPtr<Gtk::TextBuffer> libjuci::BufferFromNotebook() {
return Glib::RefPtr<Gtk::TextBuffer>(PluginApi::notebook
->CurrentSourceView()->get_buffer());
return Glib::RefPtr<Gtk::TextBuffer>(PluginApi::notebook->get_current_view()->get_buffer());
}
Gtk::TextIter libjuci::IterFromNotebook() {

37
juci/config.cc

@ -21,41 +21,28 @@ void MainConfig::GenerateSource() {
auto source_json = cfg_.get_child("source");
auto syntax_json = source_json.get_child("syntax");
auto colors_json = source_json.get_child("colors");
auto extensions_json = source_json.get_child("extensions");
auto visual_json = source_json.get_child("visual");
for (auto &i : visual_json) {
if (i.first == "background") {
if (i.first == "background")
source_cfg->background = i.second.get_value<std::string>();
}
else if (i.first == "background_selected") {
else if (i.first == "background_selected")
source_cfg->background_selected = i.second.get_value<std::string>();
}
else if (i.first == "background_tooltips") {
else if (i.first == "background_tooltips")
source_cfg->background_tooltips = i.second.get_value<std::string>();
}
else if (i.first == "show_line_numbers") {
else if (i.first == "show_line_numbers")
source_cfg->show_line_numbers = i.second.get_value<std::string>() == "1" ? true : false;
}
else if (i.first == "highlight_current_line") {
else if (i.first == "highlight_current_line")
source_cfg->highlight_current_line = i.second.get_value<std::string>() == "1" ? true : false;
}
else if (i.first == "font") {
else if (i.first == "font")
source_cfg->font = i.second.get_value<std::string>();
}
}
source_cfg->tab_size = source_json.get<unsigned>("tab_size");
for (unsigned c = 0; c < source_cfg->tab_size; c++) {
for (unsigned c = 0; c < source_cfg->tab_size; c++)
source_cfg->tab+=" ";
}
for (auto &i : colors_json) {
for (auto &i : colors_json)
source_cfg->tags[i.first]=i.second.get_value<std::string>();
}
for (auto &i : syntax_json) {
for (auto &i : syntax_json)
source_cfg->types[i.first]=i.second.get_value<std::string>();
}
for (auto &i : extensions_json) {
source_cfg->extensions.emplace_back(i.second.get_value<std::string>());
}
DEBUG("Source cfg fetched");
}
@ -64,12 +51,10 @@ void MainConfig::GenerateTerminalCommands() {
boost::property_tree::ptree source_json = cfg_.get_child("project");
boost::property_tree::ptree compile_commands_json = source_json.get_child("compile_commands");
boost::property_tree::ptree run_commands_json = source_json.get_child("run_commands");
for (auto &i : compile_commands_json) {
for (auto &i : compile_commands_json)
terminal_cfg->compile_commands.emplace_back(i.second.get_value<std::string>());
}
for (auto &i : run_commands_json) {
for (auto &i : run_commands_json)
terminal_cfg->run_command=(i.second.get_value<std::string>()); //TODO: run_commands array->one run_command?
}
}
void MainConfig::GenerateKeybindings() {

12
juci/config.json

@ -19,18 +19,6 @@
"703": "own",
"705": "comment"
},
"extensions": [
"c",
"cc",
"cpp",
"cxx",
"c++",
"h",
"hh",
"hpp",
"hxx",
"h++"
],
"visual": {
"background": "white",
"background_selected": "blue",

2
juci/juci.cc

@ -40,7 +40,7 @@ void Juci::on_activate() {
window->directories.open_folder(directory);
}
for(auto &f: files)
window->notebook.open_file(f);
window->notebook.open(f);
}
int main(int argc, char *argv[]) {

125
juci/notebook.cc

@ -23,8 +23,8 @@ Notebook::Notebook() : Gtk::Notebook() {
menu->action_group->add(Gtk::Action::create("EditUndo", "Undo"), Gtk::AccelKey(menu->key_map["edit_undo"]), [this]() {
INFO("On undo");
Glib::RefPtr<Gsv::UndoManager> undo_manager = CurrentSourceView()->get_source_buffer()->get_undo_manager();
if (Pages() != 0 && undo_manager->can_undo()) {
Glib::RefPtr<Gsv::UndoManager> undo_manager = get_current_view()->get_source_buffer()->get_undo_manager();
if (size() != 0 && undo_manager->can_undo()) {
undo_manager->undo();
}
INFO("Done undo");
@ -33,69 +33,96 @@ Notebook::Notebook() : Gtk::Notebook() {
menu->action_group->add(Gtk::Action::create("EditRedo", "Redo"), Gtk::AccelKey(menu->key_map["edit_redo"]), [this]() {
INFO("On Redo");
Glib::RefPtr<Gsv::UndoManager> undo_manager =
CurrentSourceView()->get_source_buffer()->get_undo_manager();
if (Pages() != 0 && undo_manager->can_redo()) {
get_current_view()->get_source_buffer()->get_undo_manager();
if (size() != 0 && 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(CurrentPage()!=-1) {
if(CurrentSourceView()->get_declaration_location) {
auto location=CurrentSourceView()->get_declaration_location();
if(get_current_page()!=-1) {
if(get_current_view()->get_declaration_location) {
auto location=get_current_view()->get_declaration_location();
if(location.first.size()>0) {
open_file(location.first);
CurrentSourceView()->get_buffer()->place_cursor(CurrentSourceView()->get_buffer()->get_iter_at_offset(location.second));
open(location.first);
get_current_view()->get_buffer()->place_cursor(get_current_view()->get_buffer()->get_iter_at_offset(location.second));
while(gtk_events_pending())
gtk_main_iteration();
CurrentSourceView()->scroll_to(CurrentSourceView()->get_buffer()->get_insert(), 0.0, 1.0, 0.5);
get_current_view()->scroll_to(get_current_view()->get_buffer()->get_insert(), 0.0, 1.0, 0.5);
}
}
}
});
menu->action_group->add(Gtk::Action::create("SourceGotoMethod", "Go to method"), Gtk::AccelKey(menu->key_map["source_goto_method"]), [this]() {
if(CurrentPage()!=-1) {
if(CurrentSourceView()->goto_method) {
CurrentSourceView()->goto_method();
if(get_current_page()!=-1) {
if(get_current_view()->goto_method) {
get_current_view()->goto_method();
}
}
});
}
void Notebook::open_file(std::string path) {
int Notebook::size() {
return get_n_pages();
}
Source::View* Notebook::get_view(int page) {
if(page>=size())
return nullptr;
return source_views.at(page).get();
}
Source::View* Notebook::get_current_view() {
INFO("Getting sourceview");
if(get_current_page()==-1)
return nullptr;
return get_view(get_current_page());
}
void Notebook::open(std::string path) {
INFO("Notebook open file");
INFO("Notebook create page");
for(int c=0;c<Pages();c++) {
if(path==source_views.at(c)->view->file_path) {
for(int c=0;c<size();c++) {
if(path==get_view(c)->file_path) {
set_current_page(c);
return;
}
}
source_views.emplace_back(new Source(path, project_path));
auto tmp_project_path=project_path;
if(tmp_project_path=="") {
tmp_project_path=boost::filesystem::path(path).parent_path().string();
}
auto language=Source::guess_language(path);
if(language && (language->get_id()=="chdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc"))
source_views.emplace_back(new Source::ClangView(path, tmp_project_path));
else
source_views.emplace_back(new Source::GenericView(path, tmp_project_path, language));
scrolled_windows.emplace_back(new Gtk::ScrolledWindow());
hboxes.emplace_back(new Gtk::HBox());
scrolled_windows.back()->add(*source_views.back()->view);
scrolled_windows.back()->add(*source_views.back());
hboxes.back()->pack_start(*scrolled_windows.back(), true, true);
boost::filesystem::path file_path(source_views.back()->view->file_path);
boost::filesystem::path file_path(source_views.back()->file_path);
std::string title=file_path.filename().string();
append_page(*hboxes.back(), title);
show_all_children();
set_current_page(Pages()-1);
set_focus_child(*source_views.back()->view);
CurrentSourceView()->get_buffer()->set_modified(false);
set_current_page(size()-1);
set_focus_child(*source_views.back());
get_current_view()->get_buffer()->set_modified(false);
//Add star on tab label when the page is not saved:
auto source_view=CurrentSourceView();
CurrentSourceView()->get_buffer()->signal_modified_changed().connect([this, source_view]() {
auto source_view=get_current_view();
get_current_view()->get_buffer()->signal_modified_changed().connect([this, source_view]() {
boost::filesystem::path file_path(source_view->file_path);
std::string title=file_path.filename().string();
if(source_view->get_buffer()->get_modified())
title+="*";
int page=-1;
for(int c=0;c<Pages();c++) {
if(source_views.at(c)->view.get()==source_view) {
for(int c=0;c<size();c++) {
if(get_view(c)==source_view) {
page=c;
break;
}
@ -105,14 +132,37 @@ void Notebook::open_file(std::string path) {
});
}
bool Notebook::save(int page) {
if(page>=size())
return false;
auto view=get_view(page);
if (view->file_path != "" && view->get_buffer()->get_modified()) {
std::ofstream file;
file.open(view->file_path);
file << view->get_buffer()->get_text();
file.close();
view->get_buffer()->set_modified(false);
Singleton::terminal()->print("File saved to: " +view->file_path+"\n");
return true;
}
return false;
}
bool Notebook::save_current() {
INFO("Source save file");
if(get_current_page()==-1)
return false;
return save(get_current_page());
}
bool Notebook::close_current_page() {
INFO("Notebook close page");
if (Pages() != 0) {
if(CurrentSourceView()->get_buffer()->get_modified()){
if (size() != 0) {
if(get_current_view()->get_buffer()->get_modified()){
if(!save_modified_dialog())
return false;
}
int page = CurrentPage();
int page = get_current_page();
remove_page(page);
source_views.erase(source_views.begin()+ page);
scrolled_windows.erase(scrolled_windows.begin()+page);
@ -121,26 +171,13 @@ bool Notebook::close_current_page() {
return true;
}
Source::View* Notebook::CurrentSourceView() {
INFO("Getting sourceview");
return source_views.at(CurrentPage())->view.get();
}
int Notebook::CurrentPage() {
return get_current_page();
}
int Notebook::Pages() {
return get_n_pages();
}
bool Notebook::save_modified_dialog() {
INFO("Notebook::save_dialog");
Gtk::MessageDialog dialog((Gtk::Window&)(*get_toplevel()), "Save file!", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog.set_secondary_text("Do you want to save: " + CurrentSourceView()->file_path+" ?");
dialog.set_secondary_text("Do you want to save: " + get_current_view()->file_path+" ?");
int result = dialog.run();
if(result==Gtk::RESPONSE_YES) {
CurrentSourceView()->save();
save_current();
return true;
}
else if(result==Gtk::RESPONSE_NO) {

16
juci/notebook.h

@ -3,29 +3,27 @@
#include <iostream>
#include "gtkmm.h"
#include "entrybox.h"
#include "source.h"
#include "directories.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <type_traits>
#include <map>
#include <sigc++/sigc++.h>
#include "clangmm.h"
class Notebook : public Gtk::Notebook {
public:
Notebook();
Source::View* CurrentSourceView();
int CurrentPage();
Source::View* get_view(int page);
int size();
Source::View* get_current_view();
bool close_current_page();
void open_file(std::string filename);
int Pages();
void open(std::string filename);
bool save(int page);
bool save_current();
std::string project_path;
std::vector<std::unique_ptr<Source> > source_views;
private:
bool save_modified_dialog();
std::vector<std::unique_ptr<Source::View> > source_views;
std::vector<std::unique_ptr<Gtk::ScrolledWindow> > scrolled_windows;
std::vector<std::unique_ptr<Gtk::HBox> > hboxes;
};

67
juci/source.cc

@ -16,14 +16,22 @@ namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
bool Source::Config::legal_extension(std::string e) const {
std::transform(e.begin(), e.end(),e.begin(), ::tolower);
if (find(extensions.begin(), extensions.end(), e) != extensions.end()) {
DEBUG("Legal extension");
return true;
Glib::RefPtr<Gsv::Language> Source::guess_language(const std::string &file_path) {
auto language_manager=Gsv::LanguageManager::get_default();
bool result_uncertain = false;
auto content_type = Gio::content_type_guess(file_path, NULL, 0, result_uncertain);
if(result_uncertain) {
content_type.clear();
}
DEBUG("Ilegal extension");
return false;
auto language=language_manager->guess_language(file_path, content_type);
if(!language) {
auto path=boost::filesystem::path(file_path);
auto filename=path.filename().string();
auto extension=path.extension();
if(filename=="CMakeLists.txt")
language=language_manager->get_language("cmake");
}
return language;
}
//////////////
@ -56,20 +64,6 @@ file_path(file_path), project_path(project_path) {
g_signal_connect(search_context, "notify::occurrences-count", G_CALLBACK(search_occurrences_updated), this);
}
bool Source::View::save() {
INFO("Source save file");
if (file_path != "" && get_buffer()->get_modified()) {
std::ofstream file;
file.open (file_path);
file << get_buffer()->get_text();
file.close();
get_buffer()->set_modified(false);
Singleton::terminal()->print("File saved to: " +file_path+"\n");
return true;
}
return false;
}
void Source::View::search_occurrences_updated(GtkWidget* widget, GParamSpec* property, gpointer data) {
auto view=(Source::View*)data;
if(view->update_search_occurrences)
@ -241,7 +235,7 @@ bool Source::View::on_key_press_event(GdkEventKey* key) {
/////////////////////
//// GenericView ////
/////////////////////
Source::GenericView::GenericView(const std::string& file_path, const std::string& project_path) : View(file_path, project_path) {
Source::GenericView::GenericView(const std::string& file_path, const std::string& project_path, Glib::RefPtr<Gsv::Language> language) : View(file_path, project_path) {
auto style_scheme_manager=Gsv::StyleSchemeManager::get_default();
//TODO: add?: style_scheme_manager->prepend_search_path("~/.juci/");
auto scheme=style_scheme_manager->get_scheme("classic");
@ -252,20 +246,6 @@ Source::GenericView::GenericView(const std::string& file_path, const std::string
cout << "TODO, in progress: def:comment in scheme " << scheme->get_name() << " has color " << style->property_foreground() << endl;
}
auto language_manager=Gsv::LanguageManager::get_default();
bool result_uncertain = false;
auto content_type = Gio::content_type_guess(file_path, get_buffer()->get_text(), result_uncertain);
if(result_uncertain) {
content_type.clear();
}
auto language=language_manager->guess_language(file_path, content_type);
if(!language) {
auto path=boost::filesystem::path(file_path);
auto filename=path.filename().string();
auto extension=path.extension();
if(filename=="CMakeLists.txt")
language=language_manager->get_language("cmake");
}
if(language) {
get_source_buffer()->set_language(language);
Singleton::terminal()->print("Language for file "+file_path+" set to "+language->get_name()+".\n");
@ -1038,18 +1018,3 @@ Source::ClangViewAutocomplete(file_path, project_path) {
}
};
}
////////////////
//// Source ////
////////////////
Source::Source(const std::string& file_path, std::string project_path) {
if(project_path=="") {
project_path=boost::filesystem::path(file_path).parent_path().string();
}
if (Singleton::Config::source()->legal_extension(file_path.substr(file_path.find_last_of(".") + 1)))
view=std::unique_ptr<View>(new ClangView(file_path, project_path));
else
view=std::unique_ptr<View>(new GenericView(file_path, project_path));
INFO("Source Controller with childs constructed");
}

15
juci/source.h

@ -15,16 +15,15 @@
#include "selectiondialog.h"
#include <set>
class Source {
public:
namespace Source {
Glib::RefPtr<Gsv::Language> guess_language(const std::string &file_path);
class Config {
public:
bool legal_extension(std::string e) const ;
unsigned tab_size;
bool show_line_numbers, highlight_current_line;
std::string tab, background, background_selected, background_tooltips, font;
char tab_char=' ';
std::vector<std::string> extensions;
std::unordered_map<std::string, std::string> tags, types;
}; // class Config
@ -50,8 +49,6 @@ public:
View(const std::string& file_path, const std::string& project_path);
~View();
bool save();
void search_highlight(const std::string &text, bool case_sensitive, bool regex);
std::function<void(int number)> update_search_occurrences;
void search_forward();
@ -82,7 +79,7 @@ public:
class GenericView : public View {
public:
GenericView(const std::string& file_path, const std::string& project_path);
GenericView(const std::string& file_path, const std::string& project_path, Glib::RefPtr<Gsv::Language> language);
};
class ClangViewParse : public View {
@ -167,9 +164,5 @@ public:
ClangView(const std::string& file_path, const std::string& project_path):
ClangViewRefactor(file_path, project_path) {}
};
Source(const std::string& file_path, std::string project_path);
std::unique_ptr<Source::View> view;
}; // class Source
#endif // JUCI_SOURCE_H_

141
juci/window.cc

@ -32,7 +32,7 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
});
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]() {
@ -47,8 +47,8 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
if(auto entry=dynamic_cast<Gtk::Entry*>(widget))
entry->cut_clipboard();
else {
if (notebook.Pages() != 0)
notebook.CurrentSourceView()->get_buffer()->cut_clipboard(Gtk::Clipboard::get());
if (notebook.size() != 0)
notebook.get_current_view()->get_buffer()->cut_clipboard(Gtk::Clipboard::get());
}
});
menu->action_group->add(Gtk::Action::create("EditPaste", "Paste"), Gtk::AccelKey(menu->key_map["edit_paste"]), [this]() {
@ -56,8 +56,8 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
if(auto entry=dynamic_cast<Gtk::Entry*>(widget))
entry->paste_clipboard();
else {
if (notebook.Pages() != 0)
notebook.CurrentSourceView()->get_buffer()->paste_clipboard(Gtk::Clipboard::get());
if (notebook.size() != 0)
notebook.get_current_view()->get_buffer()->paste_clipboard(Gtk::Clipboard::get());
}
});
menu->action_group->add(Gtk::Action::create("EditFind", "Find"), Gtk::AccelKey(menu->key_map["edit_find"]), [this]() {
@ -66,14 +66,14 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
menu->action_group->add(Gtk::Action::create("SourceRename", "Rename function/variable"), Gtk::AccelKey(menu->key_map["source_rename"]), [this]() {
entry_box.clear();
if(notebook.CurrentPage()!=-1) {
if(notebook.CurrentSourceView()->get_token && notebook.CurrentSourceView()->get_token_name) {
auto token=std::make_shared<std::string>(notebook.CurrentSourceView()->get_token());
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++) {
if(notebook.source_views.at(c)->view->tag_similar_tokens) {
notebook.source_views.at(c)->view->tag_similar_tokens(*token);
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();
@ -83,13 +83,13 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
};
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(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.source_views.at(c)->view->file_path+"\n");
notebook.source_views.at(c)->view->save();
Singleton::terminal()->print("Replaced "+std::to_string(number)+" occurrences in file "+notebook.get_view(c)->file_path+"\n");
notebook.save(c);
}
}
}
@ -107,12 +107,12 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
});
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;
notebook.CurrentSourceView()->save();
notebook.save_current();
if (running.try_lock()) {
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("/\\");
if(pos != std::string::npos) {
path.erase(path.begin()+pos,path.end());
@ -128,12 +128,12 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
});
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;
notebook.CurrentSourceView()->save();
notebook.save_current();
if (running.try_lock()) {
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("/\\");
if(pos != std::string::npos){
path.erase(path.begin()+pos,path.end());
@ -170,27 +170,27 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
box.unset_focus_chain();
});
entry_box.signal_hide().connect([this]() {
if(notebook.CurrentPage()!=-1) {
notebook.CurrentSourceView()->grab_focus();
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.CurrentPage()!=-1) {
notebook.CurrentSourceView()->update_search_occurrences=[this](int number){
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.CurrentSourceView()->search_highlight(last_search, case_sensitive_search, regex_search);
notebook.get_current_view()->search_highlight(last_search, case_sensitive_search, regex_search);
}
if(notebook.CurrentPage()!=-1) {
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.CurrentSourceView()->get_declaration_location);
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.CurrentSourceView()->goto_method);
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.CurrentSourceView()->rename_similar_tokens);
menu_item->set_sensitive((bool)notebook.get_current_view()->rename_similar_tokens);
}
});
@ -244,8 +244,8 @@ bool Window::on_delete_event (GdkEventAny *event) {
}
void Window::hide() {
auto size=notebook.source_views.size();
for(size_t c=0;c<size;c++) {
auto size=notebook.size();
for(int c=0;c<size;c++) {
if(!notebook.close_current_page())
return;
}
@ -266,7 +266,7 @@ void Window::new_file_entry() {
else {
std::ofstream f(p.string().c_str());
if(f) {
notebook.open_file(boost::filesystem::canonical(p).string());
notebook.open(boost::filesystem::canonical(p).string());
Singleton::terminal()->print("New file "+p.string()+" created.\n");
if(notebook.project_path!="")
directories.open_folder(notebook.project_path); //TODO: Do refresh instead
@ -308,7 +308,6 @@ void Window::open_file_dialog() {
Gtk::FileChooserDialog dialog("Please choose a file", Gtk::FILE_CHOOSER_ACTION_OPEN);
if(notebook.project_path.size()>0)
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_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS);
@ -338,14 +337,16 @@ void Window::open_file_dialog() {
if(result==Gtk::RESPONSE_OK) {
std::string path = dialog.get_filename();
notebook.open_file(path);
notebook.open(path);
}
}
void Window::save_file_dialog() {
if(notebook.get_current_page()==-1)
return;
INFO("Save file dialog");
Gtk::FileChooserDialog dialog(*this, "Please choose a file", Gtk::FILE_CHOOSER_ACTION_SAVE);
gtk_file_chooser_set_filename((GtkFileChooser*)dialog.gobj(), notebook.CurrentSourceView()->file_path.c_str());
gtk_file_chooser_set_filename((GtkFileChooser*)dialog.gobj(), notebook.get_current_view()->file_path.c_str());
dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS);
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Save", Gtk::RESPONSE_OK);
@ -356,10 +357,10 @@ void Window::save_file_dialog() {
if(path.size()>0) {
std::ofstream file(path);
if(file) {
file << notebook.CurrentSourceView()->get_buffer()->get_text();
file << notebook.get_current_view()->get_buffer()->get_text();
file.close();
notebook.open_file(path);
Singleton::terminal()->print("File saved to: " + notebook.CurrentSourceView()->file_path+"\n");
notebook.open(path);
Singleton::terminal()->print("File saved to: " + notebook.get_current_view()->file_path+"\n");
if(notebook.project_path!="")
directories.open_folder(notebook.project_path); //TODO: Do refresh instead
}
@ -385,7 +386,7 @@ void Window::on_directory_navigation(const Gtk::TreeModel::Path& path, Gtk::Tree
std::stringstream sstm;
sstm << row[directories.view().m_col_path];
std::string file = sstm.str();
notebook.open_file(file);
notebook.open(file);
}
}
}
@ -406,41 +407,41 @@ void Window::search_and_replace_entry() {
}
};
entry_box.entries.emplace_back(last_search, [this](const std::string& content){
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->search_forward();
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->search_forward();
});
auto search_entry_it=entry_box.entries.begin();
search_entry_it->set_placeholder_text("Find");
if(notebook.CurrentPage()!=-1) {
notebook.CurrentSourceView()->update_search_occurrences=[label_it](int number){
if(notebook.get_current_page()!=-1) {
notebook.get_current_view()->update_search_occurrences=[label_it](int number){
label_it->update(0, std::to_string(number));
};
notebook.CurrentSourceView()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
notebook.get_current_view()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
}
search_entry_it->signal_key_press_event().connect([this](GdkEventKey* event){
if(event->keyval==GDK_KEY_Return && event->state==GDK_SHIFT_MASK) {
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->search_backward();
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->search_backward();
}
return false;
});
search_entry_it->signal_changed().connect([this, search_entry_it](){
last_search=search_entry_it->get_text();
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
});
entry_box.entries.emplace_back(last_replace, [this](const std::string &content){
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->replace_forward(content);
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->replace_forward(content);
});
auto replace_entry_it=entry_box.entries.begin();
replace_entry_it++;
replace_entry_it->set_placeholder_text("Replace");
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(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->replace_backward(replace_entry_it->get_text());
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->replace_backward(replace_entry_it->get_text());
}
return false;
});
@ -449,35 +450,35 @@ void Window::search_and_replace_entry() {
});
entry_box.buttons.emplace_back("Find", [this](){
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->search_forward();
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->search_forward();
});
entry_box.buttons.emplace_back("Replace", [this, replace_entry_it](){
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->replace_forward(replace_entry_it->get_text());
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->replace_forward(replace_entry_it->get_text());
});
entry_box.buttons.emplace_back("Replace all", [this, replace_entry_it](){
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->replace_all(replace_entry_it->get_text());
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->replace_all(replace_entry_it->get_text());
});
entry_box.toggle_buttons.emplace_back("Match case");
entry_box.toggle_buttons.back().set_active(case_sensitive_search);
entry_box.toggle_buttons.back().on_activate=[this, search_entry_it](){
case_sensitive_search=!case_sensitive_search;
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
};
entry_box.toggle_buttons.emplace_back("Use regex");
entry_box.toggle_buttons.back().set_active(regex_search);
entry_box.toggle_buttons.back().on_activate=[this, search_entry_it](){
regex_search=!regex_search;
if(notebook.CurrentPage()!=-1)
notebook.CurrentSourceView()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
if(notebook.get_current_page()!=-1)
notebook.get_current_view()->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
};
entry_box.signal_hide().connect([this]() {
for(int c=0;c<notebook.Pages();c++) {
notebook.source_views.at(c)->view->update_search_occurrences=nullptr;
notebook.source_views.at(c)->view->search_highlight("", case_sensitive_search, regex_search);
for(int c=0;c<notebook.size();c++) {
notebook.get_view(c)->update_search_occurrences=nullptr;
notebook.get_view(c)->search_highlight("", case_sensitive_search, regex_search);
}
search_entry_shown=false;
});

3
juci/window.h

@ -3,7 +3,8 @@
#include "api.h"
#include <cstddef>
#include "directories.h"
#include "entrybox.h"
class Window : public Gtk::Window {
public:

Loading…
Cancel
Save