Browse Source

BAB-11 #time 10h #comment got a working menu with a working mvc

merge-requests/365/head
oyvang 11 years ago
parent
commit
78990bd446
  1. 13
      juci/CMakeLists.txt
  2. 29
      juci/controller.h
  3. 35
      juci/controllers.cc
  4. 8
      juci/juci.cc
  5. 27
      juci/juci.h
  6. 17
      juci/model.h
  7. 16
      juci/models.cc
  8. 19
      juci/view.cc
  9. 38
      juci/view.h
  10. 36
      juci/views.cc
  11. 33
      juci/window.cc

13
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)

29
juci/controller.h

@ -2,16 +2,39 @@
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include "view.h"
#include "model.h"
/* -------------------------- HOW TO -------------------------- */
/* -------------------------- HOW TO ----------------------------- */
/* Controll classes under Controller->public */
/* Model object under Controller::class->private */
/* ------------------ Remove these comments ------------------- */
/* Model object under Controller::class->private name:class_model */
/* View object under Controller::class->private name:class_view */
/* ------------------ Remove these comments ---------------------- */
class Controller {
public:
class Menu {
public:
Menu();
virtual ~Menu();
Gtk::Box &get_view();
Glib::RefPtr<Gtk::ActionGroup> get_action_group() {
return menu_view.get_action_group();
};
Glib::RefPtr<Gtk::UIManager> 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

35
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;
}

8
juci/juci.cc

@ -1,9 +1,9 @@
#include "view.h"
#include "juci.h"
int main(int argc, char *argv[]) {
Glib::RefPtr <Gtk::Application> app = Gtk::Application::create(argc, argv, "org.bachelor.juci");
View view;
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "no.sout.juci");
Window window;
return app->run(view);
return app->run(window);;
}

27
juci/juci.h

@ -0,0 +1,27 @@
/*juCi++ main header file*/
#ifndef JUCI_H
#define JUCI_H
#include <iostream>
#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

17
juci/model.h

@ -2,15 +2,26 @@
#ifndef MODEL_H
#define MODEL_H
#include <iostream>
#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;
};
};

16
juci/models.cc

@ -0,0 +1,16 @@
#include "model.h"
Model::Menu::Menu() {
ui_string =
"<ui> "
" <menubar name='MenuBar'> "
" <menu action='FileMenu'> "
" <menuitem action='FileNewStandard'/> "
" <menuitem action='FileQuit'/> "
" </menu> "
" </menubar> "
"</ui> ";
}
Model::Menu::~Menu() {
}

19
juci/view.cc

@ -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() {
}

38
juci/view.h

@ -2,25 +2,41 @@
#ifndef VIEW_H
#define VIEW_H
#include <iostream>
#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<Gtk::ActionGroup> get_action_group() {
return action_group;
};
Glib::RefPtr<Gtk::UIManager> get_ui_manager() {
return ui_manager;
};
void set_ui_manger_string(std::string ui_string);
void set_ui_manager_action_group(Glib::RefPtr<Gtk::ActionGroup> action_group);
protected:
Gtk::Box view;
Glib::RefPtr<Gtk::UIManager> ui_manager;
Glib::RefPtr<Gtk::ActionGroup> action_group;
};
};

36
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<Gtk::ActionGroup> 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() {
}

33
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() {
}
Loading…
Cancel
Save