diff --git a/juci/api.cc b/juci/api.cc index 1057bf8..998e6b0 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -1,15 +1,11 @@ - #include "api.h" Menu::Controller* PluginApi::menu_; Notebook::Controller* PluginApi::notebook_; - ///////////////////////////// //// API ServiceProvider //// ///////////////////////////// - -std::string PluginApi::ProjectPath() -{ +std::string PluginApi::ProjectPath() { int MAXPATHLEN = 50; char temp[MAXPATHLEN]; return ( getcwd(temp, MAXPATHLEN) ? std::string( temp ) : std::string("") ); @@ -19,10 +15,9 @@ void PluginApi::ReplaceWord(std::string word) { Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); - libjuci::IterToWordStart(word_start); libjuci::IterToWordEnd(word_end); - if(word_start != word_end) { + if (word_start != word_end) { buffer->erase(word_start, word_end); Gtk::TextIter current = libjuci::IterFromNotebook(); buffer->insert(current, word); @@ -34,62 +29,53 @@ void PluginApi::ReplaceLine(std::string line) { } std::string PluginApi::GetWord() { - // TODO forgie: use notebook's functions - Glib::RefPtr buffer = libjuci::BufferFromNotebook(); + Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); libjuci::IterToWordStart(word_start); libjuci::IterToWordEnd(word_end); - if(word_start < word_end) { + if (word_start < word_end) { std::string word = buffer->get_text(word_start, word_end); return word; } return ""; } -//runs the pythonscript that initiates every plugin within plugins/ void PluginApi::InitPlugins() { - //libjuci::LoadPlugin(g_project_root+"plugins.py"); libjuci::LoadPlugin(ProjectPath()+"/plugins.py"); } -void PluginApi::AddMenuElement(std::string plugin_name){ +void PluginApi::AddMenuElement(std::string plugin_name) { AddMenuXml(plugin_name, "PluginMenu"); std::string plugin_action_name = plugin_name+"Menu"; - menu_->keybindings_.action_group_menu() ->add(Gtk::Action::create(plugin_action_name, plugin_name)); } void PluginApi::AddSubMenuElement(std::string parent_menu, - std::string menu_name, - std::string menu_func_name, - std::string plugin_path, - std::string menu_keybinding) { - + std::string menu_name, + std::string menu_func_name, + std::string plugin_path, + std::string menu_keybinding) { AddSubMenuXml(menu_func_name, parent_menu); - menu_->keybindings_.action_group_menu() ->add(Gtk::Action::create(menu_func_name, - menu_name), - Gtk::AccelKey(menu_keybinding), - [=]() { - libjuci::LoadPluginFunction(menu_func_name, plugin_path); - }); + menu_name), + Gtk::AccelKey(menu_keybinding), + [=]() { + libjuci::LoadPluginFunction(menu_func_name, plugin_path); + }); } void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { - std::string temp_menu = menu_->keybindings_.model_.menu_ui_string(); - std::size_t plugin_menu_pos = temp_menu.find(parent_menu); - // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc - plugin_menu_pos+=parent_menu.size() +2; + // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc + plugin_menu_pos+=parent_menu.size() +2; std::string menu_prefix = temp_menu.substr(0, plugin_menu_pos); std::string menu_suffix = temp_menu.substr(plugin_menu_pos); - std::string menu_input = " " " "; @@ -98,17 +84,17 @@ void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { menu_prefix + menu_input + menu_suffix; } -void PluginApi::AddSubMenuXml(std::string plugin_name, std::string parent_menu){ +void PluginApi::AddSubMenuXml(std::string plugin_name, + std::string parent_menu) { std::string temp_menu = menu_->keybindings_.model_.menu_ui_string(); std::size_t parent_menu_pos = temp_menu.find(parent_menu); - // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc - parent_menu_pos+=parent_menu.size() +2; + // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc + parent_menu_pos+=parent_menu.size() +2; std::string menu_prefix = temp_menu.substr(0, parent_menu_pos); std::string menu_suffix = temp_menu.substr(parent_menu_pos); - + std::string menu_input =""; - menu_->keybindings_.model_.menu_ui_string_ = menu_prefix + menu_input + menu_suffix; } @@ -122,27 +108,26 @@ void libjuci::ReplaceWord(const std::string word) { void libjuci::ReplaceLine(const std::string line) { std::cout << "unimplemented function: 'libjuci::ReplaceLine()' called" - << std::endl; + << std::endl; } std::string libjuci::GetWord() { return PluginApi::GetWord(); } -void libjuci::AddMenuElement(std::string plugin_name){ +void libjuci::AddMenuElement(std::string plugin_name) { PluginApi::AddMenuElement(plugin_name); } void libjuci::AddSubMenuElement(std::string parent_menu, - std::string menu_name, - std::string menu_func_name, - std::string plugin_path, - std::string menu_keybinding) { - + std::string menu_name, + std::string menu_func_name, + std::string plugin_path, + std::string menu_keybinding) { PluginApi::AddSubMenuElement(parent_menu, - menu_name, - menu_func_name, - plugin_path, - menu_keybinding); + menu_name, + menu_func_name, + plugin_path, + menu_keybinding); } ////////////////////////////// @@ -151,49 +136,47 @@ void libjuci::AddSubMenuElement(std::string parent_menu, namespace bp = boost::python; bp::api::object libjuci::OpenPythonScript(const std::string path, - bp::api::object python_name_space) { + bp::api::object python_name_space) { bp::str str_path(path); return bp::exec_file(str_path, python_name_space); } void libjuci::LoadPlugin(const std::string& plugin_name) { - try{ + try { Py_Initialize(); bp::api::object main_module = bp::import("__main__"); bp::api::object main_namespace = main_module.attr("__dict__"); bp::api::object ignored2 = OpenPythonScript(plugin_name, main_namespace); - - }catch(bp::error_already_set const&) { + }catch (bp::error_already_set const&) { PyErr_Print(); } } void libjuci::LoadPluginFunction(const std::string &function_name, - const std::string &plugin_path){ - try{ + const std::string &plugin_path) { + try { Py_Initialize(); bp::api::object main_module = bp::import("__main__"); bp::api::object main_namespace = main_module.attr("__dict__"); bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace); bp::str func_name(function_name); bp::api::object returnValue = bp::eval(func_name, main_namespace); - }catch(bp::error_already_set const&) { + }catch (bp::error_already_set const&) { PyErr_Print(); } } void libjuci::InitPlugin(const std::string& plugin_path) { - try{ + try { Py_Initialize(); bp::api::object main_module = bp::import("__main__"); bp::api::object main_namespace = main_module.attr("__dict__"); bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace); - bp::object returnValue = bp::eval("initPlugin()", main_namespace); - //std::string return_value = bp::extract(pySnippet); - //do something with return_value - }catch(bp::error_already_set const&) { + bp::object returnValue = bp::eval("initPlugin()", main_namespace); + // do something with return_value + }catch (bp::error_already_set const&) { PyErr_Print(); } } @@ -202,30 +185,30 @@ void libjuci::InitPlugin(const std::string& plugin_path) { //// Glib wrappers //// /////////////////////// void libjuci::IterToWordStart(Gtk::TextIter &iter) { - if(!iter.starts_line()) { - while(!iter.starts_word()) { + if (!iter.starts_line()) { + while (!iter.starts_word()) { iter.backward_char(); } } } void libjuci::IterToWordEnd(Gtk::TextIter &iter) { - if(!iter.ends_line()) { - while(!iter.ends_word()) { + if (!iter.ends_line()) { + while (!iter.ends_word()) { iter.forward_char(); } } } Glib::RefPtr libjuci::BufferFromNotebook() { - //finding focused view + // finding focused view int i = 0; - while(!PluginApi::notebook_->source_vec_.at(i)->view().has_focus()) { + while (!PluginApi::notebook_->source_vec_.at(i)->view().has_focus()) { i++; } return Glib::RefPtr(PluginApi::notebook_ - ->source_vec_.at(i) - ->view().get_buffer()); + ->source_vec_.at(i) + ->view().get_buffer()); } Gtk::TextIter libjuci::IterFromNotebook() { diff --git a/juci/api.h b/juci/api.h index 38f0008..eb17fc5 100644 --- a/juci/api.h +++ b/juci/api.h @@ -7,46 +7,39 @@ #include "notebook.h" #include "menu.h" - //////////////////// - //// Plugin Api //// - //////////////////// - class PluginApi { - public: - static Menu::Controller* menu_; - static Notebook::Controller* notebook_; - - static void InitPlugins(); - static std::string ProjectPath(); - - //for Python module: - static std::string GetWord(); - //menu management - static void AddMenuElement(const std::string plugin_name); - static void AddSubMenuElement(const std::string parent_menu, - const std::string menu_name, - const std::string menu_func_name, - const std::string plugin_path, - const std::string menu_keybinding); - //text-buffer functions - static void ReplaceWord(const std::string word); - static void ReplaceLine(const std::string line); - - protected: - static void AddMenuXml(std::string plugin_name, std::string parent_menu); - static void AddSubMenuXml(std::string plugin_name, std::string parent_menu); - }; - +//////////////////// +//// Plugin Api //// +//////////////////// +class PluginApi { +public: + static Menu::Controller* menu_; + static Notebook::Controller* notebook_; + static void InitPlugins(); + static std::string ProjectPath(); + // for Python module: + static std::string GetWord(); + // menu management + static void AddMenuElement(const std::string plugin_name); + static void AddSubMenuElement(const std::string parent_menu, + const std::string menu_name, + const std::string menu_func_name, + const std::string plugin_path, + const std::string menu_keybinding); + // text-buffer functions + static void ReplaceWord(const std::string word); + static void ReplaceLine(const std::string line); +protected: + static void AddMenuXml(std::string plugin_name, std::string parent_menu); + static void AddSubMenuXml(std::string plugin_name, std::string parent_menu); +}; // PluginApi namespace libjuci { - /////////////////////// //// Glib wrappers //// /////////////////////// void IterToWordStart(Gtk::TextIter &iter); void IterToWordEnd(Gtk::TextIter &iter); Gtk::TextIter IterFromNotebook(); - //TODO forgie: make sure it does not get the buffer to the last created textview. Glib::RefPtr BufferFromNotebook(); - /////////////////////// //// Api to python //// /////////////////////// @@ -57,27 +50,25 @@ namespace libjuci { void AddMenuElement(const std::string plugin_name); void AddSubMenuElement(const std::string parent_menu, - const std::string menu_name, - const std::string menu_func_name, - const std::string plugin_path, - const std::string menu_keybinding); - + const std::string menu_name, + const std::string menu_func_name, + const std::string plugin_path, + const std::string menu_keybinding); void AddMenuXml(const std::string plugin_name, - const string parent_menu); + const string parent_menu); void AddSubMenuXml(const std::string plugin_name, - const string parent_menu); - //TODO forgie: Make more functions targeting the python module - + const string parent_menu); ////////////////////////////// //// Boost.Python methods //// ////////////////////////////// - boost::python::api::object OpenPythonScript(const std::string path, - boost::python::api::object python_name_space); - void LoadPlugin(const std::string& plugin_name); + namespace bp = boost::python; + bp::api::object OpenPythonScript(const std::string path, + bp::api::object python_name_space); - void LoadPluginFunction(const std::string &function_name, const std::string &plugin_path); + void LoadPlugin(const std::string& plugin_name); + void LoadPluginFunction(const std::string &function_name, + const std::string &plugin_path); void InitPlugin(const std::string& plugin_path); - -}//libjuci -#endif // JUCI_API_H +} // libjuci +#endif // JUCI_API_H_ diff --git a/juci/config.h b/juci/config.h index 4b40506..4967f25 100644 --- a/juci/config.h +++ b/juci/config.h @@ -5,27 +5,17 @@ #include "keybindings.h" #include "source.h" - class MainConfig { - public: - MainConfig(); - const Source::Config& source_cfg(); - Keybindings::Config& keybindings_cfg(); - - void PrintMenu(); - //boost::property_tree::ptree& source_cfg(); - //boost::property_tree::ptree& keybindings_cfg(); - //boost::property_tree::ptree& notebookk_cfg(); - //boost::property_tree::ptree& menu_cfg(); - - boost::property_tree::ptree cfg_; - // boost::property_tree::ptree sourcecfg_; - boost::property_tree::ptree key_tree_; - // boost::property_tree::ptree notebook_cfg_; - // boost::property_tree::ptree menu_cfg_; - - Source::Config source_cfg_; - Keybindings::Config keybindings_cfg_; - - void GenerateSource(); - void GenerateKeybindings(); - }; +class MainConfig { +public: + MainConfig(); + const Source::Config& source_cfg(); + Keybindings::Config& keybindings_cfg(); + void PrintMenu(); + void GenerateSource(); + void GenerateKeybindings(); +private: + boost::property_tree::ptree cfg_; + boost::property_tree::ptree key_tree_; + Source::Config source_cfg_; + Keybindings::Config keybindings_cfg_; +}; diff --git a/juci/source.h b/juci/source.h index 315e2df..af94a1c 100644 --- a/juci/source.h +++ b/juci/source.h @@ -46,7 +46,7 @@ namespace Source { const string filepath(); void SetFilePath(const string &filepath); void SetSourceLocations( - const std::vector &locations); + const std::vector &locations); std::vector& getSourceLocations() { return locations_; } diff --git a/juci/window.cc b/juci/window.cc index efd34b1..8e96e9c 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -4,8 +4,8 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), keybindings_(main_config_.keybindings_cfg()), - notebook_( keybindings(), main_config_.source_cfg()), - menu_( keybindings() ) { + notebook_(keybindings(), main_config_.source_cfg()), + menu_(keybindings()) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); @@ -26,8 +26,6 @@ Window::Window() : add_accel_group(keybindings_.ui_manager_menu()->get_accel_group()); add_accel_group(keybindings_.ui_manager_hidden()->get_accel_group()); - - //moved here from menu.cc by forgie keybindings_.BuildMenu(); window_box_.pack_start(menu_.view(), Gtk::PACK_SHRINK); @@ -36,7 +34,7 @@ Window::Window() : show_all_children(); } // Window constructor -void Window::OnWindowHide(){ +void Window::OnWindowHide() { hide(); }