Browse Source

Cleanup: menu no longer singleton.

merge-requests/365/head
eidheim 11 years ago
parent
commit
720bf9b808
  1. 18
      src/api.cc
  2. 4
      src/api.h
  3. 7
      src/config.cc
  4. 4
      src/config.h
  5. 2
      src/menu.cc
  6. 6
      src/singletons.cc
  7. 2
      src/singletons.h
  8. 57
      src/window.cc
  9. 4
      src/window.h

18
src/api.cc

@ -2,15 +2,15 @@
#include "logging.h" #include "logging.h"
#include "singletons.h" #include "singletons.h"
Menu* PluginApi::menu_=nullptr; Menu* PluginApi::menu=nullptr;
Notebook* PluginApi::notebook=nullptr; Notebook* PluginApi::notebook=nullptr;
///////////////////////////// /////////////////////////////
//// API ServiceProvider //// //// API ServiceProvider ////
///////////////////////////// /////////////////////////////
PluginApi::PluginApi(Notebook* notebook) { PluginApi::PluginApi(Notebook* notebook, Menu* menu) {
DEBUG("Adding pointers for the API"); DEBUG("Adding pointers for the API");
this->notebook = notebook; this->notebook = notebook;
menu_ = Singleton::menu(); this->menu = menu;
DEBUG("Initiating plugins(from plugins.py).."); DEBUG("Initiating plugins(from plugins.py)..");
#ifndef __APPLE__ #ifndef __APPLE__
InitPlugins(); //TODO: fix this InitPlugins(); //TODO: fix this
@ -58,7 +58,7 @@ void PluginApi::AddMenuElement(std::string plugin_name) {
DEBUG("Adding menu element for "+plugin_name); DEBUG("Adding menu element for "+plugin_name);
AddMenuXml(plugin_name, "PluginMenu"); AddMenuXml(plugin_name, "PluginMenu");
std::string plugin_action_name = plugin_name+"Menu"; std::string plugin_action_name = plugin_name+"Menu";
Singleton::menu()->action_group->add(Gtk::Action::create(plugin_action_name, plugin_name)); menu->action_group->add(Gtk::Action::create(plugin_action_name, plugin_name));
} }
void PluginApi::AddSubMenuElement(std::string parent_menu, void PluginApi::AddSubMenuElement(std::string parent_menu,
@ -67,7 +67,7 @@ void PluginApi::AddSubMenuElement(std::string parent_menu,
std::string plugin_path, std::string plugin_path,
std::string menu_keybinding) { std::string menu_keybinding) {
AddSubMenuXml(menu_func_name, parent_menu); AddSubMenuXml(menu_func_name, parent_menu);
Singleton::menu()->action_group->add(Gtk::Action::create(menu_func_name, menu->action_group->add(Gtk::Action::create(menu_func_name,
menu_name), menu_name),
Gtk::AccelKey(menu_keybinding), Gtk::AccelKey(menu_keybinding),
[=]() { [=]() {
@ -76,7 +76,7 @@ void PluginApi::AddSubMenuElement(std::string parent_menu,
} }
void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) {
std::string temp_menu = Singleton::menu()->ui; std::string temp_menu = menu->ui;
std::size_t plugin_menu_pos = temp_menu.find(parent_menu); std::size_t plugin_menu_pos = temp_menu.find(parent_menu);
// +2 gets you outside of the tag:<'menu_name'> // +2 gets you outside of the tag:<'menu_name'>
plugin_menu_pos+=parent_menu.size() +2; plugin_menu_pos+=parent_menu.size() +2;
@ -86,12 +86,12 @@ void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) {
" <menu action='"+plugin_name+"Menu'> " " <menu action='"+plugin_name+"Menu'> "
" </menu> "; " </menu> ";
Singleton::menu()->ui = menu_prefix + menu_input + menu_suffix; menu->ui = menu_prefix + menu_input + menu_suffix;
} }
void PluginApi::AddSubMenuXml(std::string plugin_name, void PluginApi::AddSubMenuXml(std::string plugin_name,
std::string parent_menu) { std::string parent_menu) {
std::string temp_menu = Singleton::menu()->ui; std::string temp_menu = menu->ui;
std::size_t parent_menu_pos = temp_menu.find(parent_menu); std::size_t parent_menu_pos = temp_menu.find(parent_menu);
// +2 gets you outside of the tag:<'menu_name'> // +2 gets you outside of the tag:<'menu_name'>
@ -100,7 +100,7 @@ void PluginApi::AddSubMenuXml(std::string plugin_name,
std::string menu_suffix = temp_menu.substr(parent_menu_pos); std::string menu_suffix = temp_menu.substr(parent_menu_pos);
std::string menu_input ="<menuitem action='"+plugin_name+"'/>"; std::string menu_input ="<menuitem action='"+plugin_name+"'/>";
Singleton::menu()->ui = menu_prefix + menu_input + menu_suffix; menu->ui = menu_prefix + menu_input + menu_suffix;
} }
/////////////////////// ///////////////////////

4
src/api.h

@ -12,8 +12,8 @@
//////////////////// ////////////////////
class PluginApi { class PluginApi {
public: public:
PluginApi(Notebook* notebook); PluginApi(Notebook* notebook, Menu* menu);
static Menu* menu_; static Menu* menu;
static Notebook* notebook; static Notebook* notebook;
static void InitPlugins(); static void InitPlugins();
// for Python module: // for Python module:

7
src/config.cc

@ -5,7 +5,7 @@
#include "files.h" #include "files.h"
#include "sourcefile.h" #include "sourcefile.h"
MainConfig::MainConfig() { MainConfig::MainConfig(Menu &menu) : menu(menu) {
find_or_create_config_files(); find_or_create_config_files();
boost::property_tree::json_parser::read_json(Singleton::config_dir() + "config.json", cfg); boost::property_tree::json_parser::read_json(Singleton::config_dir() + "config.json", cfg);
GenerateSource(); GenerateSource();
@ -71,17 +71,16 @@ void MainConfig::GenerateTerminalCommands() {
} }
void MainConfig::GenerateKeybindings() { void MainConfig::GenerateKeybindings() {
auto menu = Singleton::menu();
boost::filesystem::path path(Singleton::config_dir() + "menu.xml"); boost::filesystem::path path(Singleton::config_dir() + "menu.xml");
if (!boost::filesystem::is_regular_file(path)) { if (!boost::filesystem::is_regular_file(path)) {
std::cerr << "menu.xml not found" << std::endl; std::cerr << "menu.xml not found" << std::endl;
throw; throw;
} }
menu->ui = juci::filesystem::open(path); menu.ui = juci::filesystem::open(path);
boost::property_tree::ptree keys_json = cfg.get_child("keybindings"); boost::property_tree::ptree keys_json = cfg.get_child("keybindings");
for (auto &i : keys_json) { for (auto &i : keys_json) {
auto key=i.second.get_value<std::string>(); auto key=i.second.get_value<std::string>();
menu->key_map[i.first] = key; menu.key_map[i.first] = key;
} }
DEBUG("Keybindings fetched"); DEBUG("Keybindings fetched");
} }

4
src/config.h

@ -2,10 +2,11 @@
#define JUCI_CONFIG_H_ #define JUCI_CONFIG_H_
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp> #include <boost/property_tree/xml_parser.hpp>
#include "menu.h"
class MainConfig { class MainConfig {
public: public:
MainConfig(); MainConfig(Menu &menu);
void find_or_create_config_files(); void find_or_create_config_files();
void PrintMenu(); void PrintMenu();
void GenerateSource(); void GenerateSource();
@ -15,5 +16,6 @@ public:
private: private:
boost::property_tree::ptree cfg; boost::property_tree::ptree cfg;
boost::property_tree::ptree key_tree; boost::property_tree::ptree key_tree;
Menu &menu;
}; };
#endif #endif

2
src/menu.cc

@ -1,6 +1,5 @@
#include "menu.h" #include "menu.h"
#include "logging.h" #include "logging.h"
#include <iostream>
Menu::Menu() : box(Gtk::ORIENTATION_VERTICAL) { Menu::Menu() : box(Gtk::ORIENTATION_VERTICAL) {
INFO("Creating menu"); INFO("Creating menu");
@ -34,3 +33,4 @@ void Menu::build() {
} }
ui_manager->insert_action_group(action_group); ui_manager->insert_action_group(action_group);
} }

6
src/singletons.cc

@ -5,14 +5,8 @@ std::unique_ptr<Terminal::Config> Singleton::Config::terminal_=std::unique_ptr<T
std::unique_ptr<Directories::Config> Singleton::Config::directories_=std::unique_ptr<Directories::Config>(new Directories::Config()); std::unique_ptr<Directories::Config> Singleton::Config::directories_=std::unique_ptr<Directories::Config>(new Directories::Config());
std::unique_ptr<Terminal> Singleton::terminal_=std::unique_ptr<Terminal>(); std::unique_ptr<Terminal> Singleton::terminal_=std::unique_ptr<Terminal>();
std::unique_ptr<Menu> Singleton::menu_=std::unique_ptr<Menu>();
Terminal *Singleton::terminal() { Terminal *Singleton::terminal() {
if(!terminal_) if(!terminal_)
terminal_=std::unique_ptr<Terminal>(new Terminal()); terminal_=std::unique_ptr<Terminal>(new Terminal());
return terminal_.get(); return terminal_.get();
} }
Menu *Singleton::menu() {
if(!menu_)
menu_=std::unique_ptr<Menu>(new Menu());
return menu_.get();
}

2
src/singletons.h

@ -23,10 +23,8 @@ public:
static std::string config_dir() { return std::string(getenv("HOME")) + "/.juci/config/"; } static std::string config_dir() { return std::string(getenv("HOME")) + "/.juci/config/"; }
static std::string log_dir() { return std::string(getenv("HOME")) + "/.juci/log/"; } static std::string log_dir() { return std::string(getenv("HOME")) + "/.juci/log/"; }
static Terminal *terminal(); static Terminal *terminal();
static Menu *menu();
private: private:
static std::unique_ptr<Terminal> terminal_; static std::unique_ptr<Terminal> terminal_;
static std::unique_ptr<Menu> menu_;
}; };
#endif // JUCI_SINGLETONS_H_ #endif // JUCI_SINGLETONS_H_

57
src/window.cc

@ -16,9 +16,10 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL) {
set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK); set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK);
add(box); add(box);
MainConfig(); //Read the configs here MainConfig(this->menu); //Read the configs here
PluginApi(&this->notebook); //Initialise plugins PluginApi(&this->notebook, &this->menu); //Initialise plugins
add_menu(); create_menu();
box.pack_start(menu.get_widget(), Gtk::PACK_SHRINK);
box.pack_start(entry_box, Gtk::PACK_SHRINK); box.pack_start(entry_box, Gtk::PACK_SHRINK);
@ -59,67 +60,66 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL) {
} }
if(notebook.get_current_page()!=-1) { if(notebook.get_current_page()!=-1) {
if(auto menu_item=dynamic_cast<Gtk::MenuItem*>(Singleton::menu()->ui_manager->get_widget("/MenuBar/SourceMenu/SourceGotoDeclaration"))) if(auto menu_item=dynamic_cast<Gtk::MenuItem*>(menu.ui_manager->get_widget("/MenuBar/SourceMenu/SourceGotoDeclaration")))
menu_item->set_sensitive((bool)notebook.get_current_view()->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"))) if(auto menu_item=dynamic_cast<Gtk::MenuItem*>(menu.ui_manager->get_widget("/MenuBar/SourceMenu/SourceGotoMethod")))
menu_item->set_sensitive((bool)notebook.get_current_view()->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"))) if(auto menu_item=dynamic_cast<Gtk::MenuItem*>(menu.ui_manager->get_widget("/MenuBar/SourceMenu/SourceRename")))
menu_item->set_sensitive((bool)notebook.get_current_view()->rename_similar_tokens); menu_item->set_sensitive((bool)notebook.get_current_view()->rename_similar_tokens);
} }
}); });
INFO("Window created"); INFO("Window created");
} // Window constructor } // Window constructor
void Window::add_menu() { void Window::create_menu() {
auto menu=Singleton::menu();
INFO("Adding actions to menu"); INFO("Adding actions to menu");
menu->action_group->add(Gtk::Action::create("FileQuit", "Quit juCi++"), Gtk::AccelKey(menu->key_map["quit"]), [this]() { menu.action_group->add(Gtk::Action::create("FileQuit", "Quit juCi++"), Gtk::AccelKey(menu.key_map["quit"]), [this]() {
hide(); hide();
}); });
menu->action_group->add(Gtk::Action::create("FileNewFile", "New file"), Gtk::AccelKey(menu->key_map["new_file"]), [this]() { menu.action_group->add(Gtk::Action::create("FileNewFile", "New file"), Gtk::AccelKey(menu.key_map["new_file"]), [this]() {
new_file_entry(); new_file_entry();
}); });
menu->action_group->add(Gtk::Action::create("FileOpenFile", "Open file"), Gtk::AccelKey(menu->key_map["open_file"]), [this]() { menu.action_group->add(Gtk::Action::create("FileOpenFile", "Open file"), Gtk::AccelKey(menu.key_map["open_file"]), [this]() {
open_file_dialog(); open_file_dialog();
}); });
menu->action_group->add(Gtk::Action::create("FileOpenFolder", "Open folder"), Gtk::AccelKey(menu->key_map["open_folder"]), [this]() { menu.action_group->add(Gtk::Action::create("FileOpenFolder", "Open folder"), Gtk::AccelKey(menu.key_map["open_folder"]), [this]() {
open_folder_dialog(); open_folder_dialog();
}); });
menu->action_group->add(Gtk::Action::create("FileSaveAs", "Save as"), Gtk::AccelKey(menu->key_map["save_as"]), [this]() { menu.action_group->add(Gtk::Action::create("FileSaveAs", "Save as"), Gtk::AccelKey(menu.key_map["save_as"]), [this]() {
save_file_dialog(); save_file_dialog();
}); });
menu->action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(menu->key_map["save"]), [this]() { menu.action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(menu.key_map["save"]), [this]() {
notebook.save_current(); notebook.save_current();
}); });
menu->action_group->add(Gtk::Action::create("EditCopy", "Copy"), Gtk::AccelKey(menu->key_map["edit_copy"]), [this]() { menu.action_group->add(Gtk::Action::create("EditCopy", "Copy"), Gtk::AccelKey(menu.key_map["edit_copy"]), [this]() {
auto widget=get_focus(); auto widget=get_focus();
if(auto entry=dynamic_cast<Gtk::Entry*>(widget)) if(auto entry=dynamic_cast<Gtk::Entry*>(widget))
entry->copy_clipboard(); entry->copy_clipboard();
else if(auto text_view=dynamic_cast<Gtk::TextView*>(widget)) else if(auto text_view=dynamic_cast<Gtk::TextView*>(widget))
text_view->get_buffer()->copy_clipboard(Gtk::Clipboard::get()); text_view->get_buffer()->copy_clipboard(Gtk::Clipboard::get());
}); });
menu->action_group->add(Gtk::Action::create("EditCut", "Cut"), Gtk::AccelKey(menu->key_map["edit_cut"]), [this]() { menu.action_group->add(Gtk::Action::create("EditCut", "Cut"), Gtk::AccelKey(menu.key_map["edit_cut"]), [this]() {
auto widget=get_focus(); auto widget=get_focus();
if(auto entry=dynamic_cast<Gtk::Entry*>(widget)) if(auto entry=dynamic_cast<Gtk::Entry*>(widget))
entry->cut_clipboard(); entry->cut_clipboard();
else if(notebook.get_current_page()!=-1) else if(notebook.get_current_page()!=-1)
notebook.get_current_view()->get_buffer()->cut_clipboard(Gtk::Clipboard::get()); 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]() { menu.action_group->add(Gtk::Action::create("EditPaste", "Paste"), Gtk::AccelKey(menu.key_map["edit_paste"]), [this]() {
auto widget=get_focus(); auto widget=get_focus();
if(auto entry=dynamic_cast<Gtk::Entry*>(widget)) if(auto entry=dynamic_cast<Gtk::Entry*>(widget))
entry->paste_clipboard(); entry->paste_clipboard();
else if(notebook.get_current_page()!=-1) else if(notebook.get_current_page()!=-1)
notebook.get_current_view()->get_buffer()->paste_clipboard(Gtk::Clipboard::get()); 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]() { menu.action_group->add(Gtk::Action::create("EditFind", "Find"), Gtk::AccelKey(menu.key_map["edit_find"]), [this]() {
search_and_replace_entry(); search_and_replace_entry();
}); });
menu->action_group->add(Gtk::Action::create("EditUndo", "Undo"), Gtk::AccelKey(menu->key_map["edit_undo"]), [this]() { menu.action_group->add(Gtk::Action::create("EditUndo", "Undo"), Gtk::AccelKey(menu.key_map["edit_undo"]), [this]() {
INFO("On undo"); INFO("On undo");
if(notebook.get_current_page()!=-1) { if(notebook.get_current_page()!=-1) {
auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager(); auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager();
@ -129,7 +129,7 @@ void Window::add_menu() {
} }
INFO("Done undo"); INFO("Done undo");
}); });
menu->action_group->add(Gtk::Action::create("EditRedo", "Redo"), Gtk::AccelKey(menu->key_map["edit_redo"]), [this]() { menu.action_group->add(Gtk::Action::create("EditRedo", "Redo"), Gtk::AccelKey(menu.key_map["edit_redo"]), [this]() {
INFO("On Redo"); INFO("On Redo");
if(notebook.get_current_page()!=-1) { if(notebook.get_current_page()!=-1) {
auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager(); auto undo_manager = notebook.get_current_view()->get_source_buffer()->get_undo_manager();
@ -140,7 +140,7 @@ void Window::add_menu() {
INFO("Done Redo"); INFO("Done Redo");
}); });
menu->action_group->add(Gtk::Action::create("SourceGotoDeclaration", "Go to declaration"), Gtk::AccelKey(menu->key_map["source_goto_declaration"]), [this]() { menu.action_group->add(Gtk::Action::create("SourceGotoDeclaration", "Go to declaration"), Gtk::AccelKey(menu.key_map["source_goto_declaration"]), [this]() {
if(notebook.get_current_page()!=-1) { if(notebook.get_current_page()!=-1) {
if(notebook.get_current_view()->get_declaration_location) { if(notebook.get_current_view()->get_declaration_location) {
auto location=notebook.get_current_view()->get_declaration_location(); auto location=notebook.get_current_view()->get_declaration_location();
@ -154,18 +154,18 @@ void Window::add_menu() {
} }
} }
}); });
menu->action_group->add(Gtk::Action::create("SourceGotoMethod", "Go to method"), Gtk::AccelKey(menu->key_map["source_goto_method"]), [this]() { menu.action_group->add(Gtk::Action::create("SourceGotoMethod", "Go to method"), Gtk::AccelKey(menu.key_map["source_goto_method"]), [this]() {
if(notebook.get_current_page()!=-1) { if(notebook.get_current_page()!=-1) {
if(notebook.get_current_view()->goto_method) { if(notebook.get_current_view()->goto_method) {
notebook.get_current_view()->goto_method(); notebook.get_current_view()->goto_method();
} }
} }
}); });
menu->action_group->add(Gtk::Action::create("SourceRename", "Rename"), Gtk::AccelKey(menu->key_map["source_rename"]), [this]() { menu.action_group->add(Gtk::Action::create("SourceRename", "Rename"), Gtk::AccelKey(menu.key_map["source_rename"]), [this]() {
rename_token_entry(); rename_token_entry();
}); });
menu->action_group->add(Gtk::Action::create("ProjectCompileAndRun", "Compile And Run"), Gtk::AccelKey(menu->key_map["compile_and_run"]), [this]() { menu.action_group->add(Gtk::Action::create("ProjectCompileAndRun", "Compile And Run"), Gtk::AccelKey(menu.key_map["compile_and_run"]), [this]() {
if(notebook.get_current_page()==-1) if(notebook.get_current_page()==-1)
return; return;
notebook.save_current(); notebook.save_current();
@ -185,7 +185,7 @@ void Window::add_menu() {
execute.detach(); execute.detach();
} }
}); });
menu->action_group->add(Gtk::Action::create("ProjectCompile", "Compile"), Gtk::AccelKey(menu->key_map["compile"]), [this]() { menu.action_group->add(Gtk::Action::create("ProjectCompile", "Compile"), Gtk::AccelKey(menu.key_map["compile"]), [this]() {
if(notebook.get_current_page()==-1) if(notebook.get_current_page()==-1)
return; return;
notebook.save_current(); notebook.save_current();
@ -204,13 +204,12 @@ void Window::add_menu() {
} }
}); });
menu->action_group->add(Gtk::Action::create("WindowCloseTab", "Close tab"), Gtk::AccelKey(menu->key_map["close_tab"]), [this]() { menu.action_group->add(Gtk::Action::create("WindowCloseTab", "Close tab"), Gtk::AccelKey(menu.key_map["close_tab"]), [this]() {
notebook.close_current_page(); notebook.close_current_page();
}); });
add_accel_group(menu->ui_manager->get_accel_group()); add_accel_group(menu.ui_manager->get_accel_group());
menu->build(); menu.build();
INFO("Menu build") INFO("Menu build")
box.pack_start(menu->get_widget(), Gtk::PACK_SHRINK);
} }
bool Window::on_key_press_event(GdkEventKey *event) { bool Window::on_key_press_event(GdkEventKey *event) {

4
src/window.h

@ -5,6 +5,7 @@
#include "directories.h" #include "directories.h"
#include "entrybox.h" #include "entrybox.h"
#include "notebook.h" #include "notebook.h"
#include "menu.h"
class Window : public Gtk::Window { class Window : public Gtk::Window {
public: public:
@ -20,8 +21,9 @@ private:
Gtk::Paned directory_and_notebook_panes; Gtk::Paned directory_and_notebook_panes;
EntryBox entry_box; EntryBox entry_box;
std::mutex running; std::mutex running;
Menu menu;
void add_menu(); void create_menu();
void hide(); void hide();
void new_file_entry(); void new_file_entry();
void open_folder_dialog(); void open_folder_dialog();

Loading…
Cancel
Save