From 78990bd446dbd1351c7bc94123d0309bf73bc2cd Mon Sep 17 00:00:00 2001 From: oyvang Date: Tue, 3 Feb 2015 16:01:26 +0100 Subject: [PATCH] BAB-11 #time 10h #comment got a working menu with a working mvc --- juci/CMakeLists.txt | 13 ++++++++++--- juci/controller.h | 33 ++++++++++++++++++++++++++++----- juci/controllers.cc | 35 +++++++++++++++++++++++++++++++++++ juci/juci.cc | 12 ++++++------ juci/juci.h | 27 +++++++++++++++++++++++++++ juci/model.h | 17 ++++++++++++++--- juci/models.cc | 16 ++++++++++++++++ juci/view.cc | 19 ------------------- juci/view.h | 38 +++++++++++++++++++++++++++----------- juci/views.cc | 36 ++++++++++++++++++++++++++++++++++++ juci/window.cc | 33 +++++++++++++++++++++++++++++++++ 11 files changed, 232 insertions(+), 47 deletions(-) create mode 100644 juci/controllers.cc create mode 100644 juci/juci.h create mode 100644 juci/models.cc delete mode 100644 juci/view.cc create mode 100644 juci/views.cc create mode 100644 juci/window.cc diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index cca9014..2ac0a08 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -7,11 +7,18 @@ INCLUDE(FindPkgConfig) # name of the executable on Windows will be example.exe add_executable(juci # list of every needed file to create the executable - juci.cc - controller.h model.h + models.cc + view.h - view.cc) + views.cc + + controller.h + controllers.cc + + window.cc + juci.cc + ) # dependencies pkg_check_modules(GTKMM REQUIRED gtkmm-3.0) diff --git a/juci/controller.h b/juci/controller.h index 75b99af..f863adf 100644 --- a/juci/controller.h +++ b/juci/controller.h @@ -2,16 +2,39 @@ #ifndef CONTROLLER_H #define CONTROLLER_H +#include "view.h" #include "model.h" -/* -------------------------- HOW TO -------------------------- */ -/* Controll classes under Controller->public */ -/* Model object under Controller::class->private */ -/* ------------------ Remove these comments ------------------- */ +/* -------------------------- HOW TO ----------------------------- */ +/* Controll classes under Controller->public */ +/* Model object under Controller::class->private name:class_model */ +/* View object under Controller::class->private name:class_view */ +/* ------------------ Remove these comments ---------------------- */ -class Controller { +class Controller { public: + class Menu { + public: + Menu(); + virtual ~Menu(); + Gtk::Box &get_view(); + + Glib::RefPtr get_action_group() { + return menu_view.get_action_group(); + }; + Glib::RefPtr get_ui_manager() { + return menu_view.get_ui_manager(); + }; + + private: + View::Menu menu_view; + Model::Menu menu_model; + void onNewEmptyfile(); + void onNewCCFile(); + void onNewHeaderFile(); + }; }; + #endif //CONTROLLER_H \ No newline at end of file diff --git a/juci/controllers.cc b/juci/controllers.cc new file mode 100644 index 0000000..f4832b2 --- /dev/null +++ b/juci/controllers.cc @@ -0,0 +1,35 @@ +#include "controller.h" + +Controller::Menu::Menu() : + menu_view(Gtk::ORIENTATION_VERTICAL), + menu_model() { +/*Add action to menues*/ + menu_view.get_action_group()->add(Gtk::Action::create("FileMenu", "File")); + + menu_view.get_action_group()->add(Gtk::Action::create("FileNewStandard", + Gtk::Stock::NEW, "New empty file", "Create a new file"), + [this]() { + onNewEmptyfile(); + }); + + + + menu_view.set_ui_manager_action_group(menu_view.get_action_group()); + menu_view.set_ui_manger_string(menu_model.get_ui_string()); +} + +Controller::Menu::~Menu() { + +} + +Gtk::Box &Controller::Menu::get_view() { + return menu_view.get_view(); +} + +void Controller::Menu::onNewEmptyfile() { + std::cout << "New file clicked"<< std::endl; +} + + + + diff --git a/juci/juci.cc b/juci/juci.cc index de8df1b..6b86603 100644 --- a/juci/juci.cc +++ b/juci/juci.cc @@ -1,9 +1,9 @@ -#include "view.h" +#include "juci.h" -int main (int argc, char *argv[]) { - Glib::RefPtr app = Gtk::Application::create(argc, argv, "org.bachelor.juci"); +int main(int argc, char *argv[]) { - View view; + Glib::RefPtr app = Gtk::Application::create(argc, argv, "no.sout.juci"); + Window window; - return app->run(view); -} \ No newline at end of file + return app->run(window);; +} diff --git a/juci/juci.h b/juci/juci.h new file mode 100644 index 0000000..fa66dbe --- /dev/null +++ b/juci/juci.h @@ -0,0 +1,27 @@ +/*juCi++ main header file*/ +#ifndef JUCI_H +#define JUCI_H + +#include +#include "gtkmm.h" +#include "view.h" +#include "model.h" +#include "controller.h" + + +class Window : public Gtk::Window { +public: + Window(); + + virtual ~Window(); + + Gtk::Box window_box; + +private: + Controller::Menu menu; + /*signal handler*/ + void onSystemQuit(); + +}; + +#endif // JUCI_H diff --git a/juci/model.h b/juci/model.h index c550296..04ef11d 100644 --- a/juci/model.h +++ b/juci/model.h @@ -2,15 +2,26 @@ #ifndef MODEL_H #define MODEL_H -#include +#include "gtkmm.h" /* -------------------------- HOW TO -------------------------- */ -/* Model classes under Model->public if they are going to be */ -/* used from controllers */ +/* Model classes under Model if possible */ /* ------------------ Remove these comments ------------------- */ class Model { public: + class Menu { + public: + Menu(); + + virtual~Menu(); + + std::string get_ui_string() { + return ui_string; + }; + private: + std::string ui_string; + }; }; diff --git a/juci/models.cc b/juci/models.cc new file mode 100644 index 0000000..45bc49e --- /dev/null +++ b/juci/models.cc @@ -0,0 +1,16 @@ +#include "model.h" + +Model::Menu::Menu() { + ui_string = + " " + " " + " " + " " + " " + " " + " " + " "; +} + +Model::Menu::~Menu() { +} \ No newline at end of file diff --git a/juci/view.cc b/juci/view.cc deleted file mode 100644 index 3f15969..0000000 --- a/juci/view.cc +++ /dev/null @@ -1,19 +0,0 @@ -#include "view.h" - -View::View() : - window_box(Gtk::ORIENTATION_VERTICAL){ - - set_title("juCi++"); - set_default_size(800, 600); - maximize(); - - add(window_box); - - /*Add default views here */ - - show_all_children(); - -}; -View::~View() { - -} diff --git a/juci/view.h b/juci/view.h index b148e18..ce52932 100644 --- a/juci/view.h +++ b/juci/view.h @@ -2,25 +2,41 @@ #ifndef VIEW_H #define VIEW_H -#include #include "gtkmm.h" -#include "controller.h" +#include "iostream" /* -------------------------- HOW TO -------------------------- */ -/* All view shall be under View->private if possible */ -/* View objects under View->protected */ -/* View controller object under View::class->private */ +/* All view shall be under View if possible */ /* ------------------ Remove these comments ------------------- */ -class View : public Gtk::Window { +class View { public: - View(); - virtual ~View(); -protected: - Gtk::Box window_box; -private: + class Menu { + public: + Menu(Gtk::Orientation orient); + virtual ~Menu(); + + Gtk::Box &get_view(); + + Glib::RefPtr get_action_group() { + return action_group; + }; + + Glib::RefPtr get_ui_manager() { + return ui_manager; + }; + + void set_ui_manger_string(std::string ui_string); + + void set_ui_manager_action_group(Glib::RefPtr action_group); + + protected: + Gtk::Box view; + Glib::RefPtr ui_manager; + Glib::RefPtr action_group; + }; }; diff --git a/juci/views.cc b/juci/views.cc new file mode 100644 index 0000000..8e8c112 --- /dev/null +++ b/juci/views.cc @@ -0,0 +1,36 @@ +#include "view.h" + + +/***********************************/ +/* MENU */ +/***********************************/ +View::Menu::Menu(Gtk::Orientation orientation) : + view(orientation) { + + action_group = Gtk::ActionGroup::create(); + ui_manager = Gtk::UIManager::create(); + + +} + +void View::Menu::set_ui_manger_string(std::string ui_string) { + try { + ui_manager->add_ui_from_string(ui_string); + } + catch (const Glib::Error &ex) { + std::cerr << "building menus failed: " << ex.what(); + } +} + +void View::Menu::set_ui_manager_action_group(Glib::RefPtr action_group) { + ui_manager->insert_action_group(action_group); +} + +Gtk::Box &View::Menu::get_view() { + view.pack_start(*ui_manager->get_widget("/MenuBar"), Gtk::PACK_SHRINK); + return view; +} + + +View::Menu::~Menu() { +} \ No newline at end of file diff --git a/juci/window.cc b/juci/window.cc new file mode 100644 index 0000000..ddbfd76 --- /dev/null +++ b/juci/window.cc @@ -0,0 +1,33 @@ +#include "juci.h" + +Window::Window() : + window_box(Gtk::ORIENTATION_HORIZONTAL), + menu() { + + + set_title("example juCi++"); + set_default_size(600, 600); + + add(window_box); + menu.get_action_group()->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT), + [this]() { + onSystemQuit(); + }); + + add_accel_group(menu.get_ui_manager()->get_accel_group()); + + window_box.pack_start(menu.get_view()); + + + show_all_children(); +}; + +void Window::onSystemQuit() { + hide(); +} + +Window::~Window() { + +} + +