Added SimpleTable example, moved from C::B to Makefile based template.
[fltk_mvc_template.git] / _template / src / View.cxx
1 ///////////////////////////////////////////////////////////////////////////
2 // Workfile: View.cxx
3 // Author: Daniel Giritzer <daniel@giritzer.eu>
4 // Date:
5 // Description: Diese Klasse stellt eine platformunabhängige Visualisierung
6 //              des Models zur verfügung.
7 // Remarks: -
8 ///////////////////////////////////////////////////////////////////////////
9 #include "View.h"
10 #include <iostream>
11 #include <FL/fl_ask.H>
12 #include <FL/Fl_PNG_Image.H>
13
14 #ifdef _WIN32
15 #include <FL/x.H>
16 #include <windows.h>
17 #endif /*_WIN32*/
18
19 View::View(ControllerIF::SPtr contr, ModelIF::SPtr model)
20 {
21     mController = contr;
22     mModel = model;
23     subscribeSubject(mModel.get());
24 }
25
26 void View::showErrorMsg(const std::string& message)
27 {
28     std::cerr << message << std::endl;
29     std::string printMsg = message;
30     fl_message_title("Error!");
31     printMsg.append("\n\nExit application?");
32     switch ( fl_choice(printMsg.c_str(), "Yes", "No", 0) ) {
33       case 0: exit(EXIT_FAILURE); break;
34     }
35 }
36
37 View::~View()
38 {
39     win_exmpl->hide();
40
41     // Ressourcen freigeben
42     Fl::delete_widget(win_exmpl);
43     unsubscribeSubject(mModel.get());
44 }
45
46 void View::updatedBySubject(Subject* s)
47 {
48     if(s == nullptr) {
49         throw std::string("Nullpointer given as Subject!");
50     }
51
52         try
53         {
54
55             // Lade Daten von Model hier implementieren
56
57             win_exmpl->redraw();
58         }
59         // ----------- Exception Handling ------------
60         catch(const std::string &e)
61         {
62             showErrorMsg(e);
63         }
64         catch(...)
65         {
66             showErrorMsg("Unknown error occured!");
67         }
68
69
70 void View::show()
71 {
72     // Setze fenster style global
73     Fl::scheme("gtk+");
74
75     //Setze Fenster Icon
76 #ifdef _WIN32
77     win_exmpl->icon((char*)LoadIcon(fl_display, MAKEINTRESOURCE(101)));
78 #else
79     Fl_PNG_Image win_icon("Icon.png");
80     win_exmpl->icon(&win_icon);
81 #endif /*_WIN32*/
82
83     //Tabelle mit Beispieldaten füllen
84     tbl_example->row_header(true);
85     tbl_example->col_header(true);
86     for(int i = 0; i <= 10; i++)
87             for(int ii = 0; ii <= 10; ii++)
88                 tbl_example->SetTableData(std::to_string(i) + " " + std::to_string(ii), i, ii, i + ii);
89
90     // definiere callback funktionen
91     btn_exception->callback((Fl_Callback*)btn_exception_cb, (void*)(this) );
92
93     win_exmpl->show();
94 }
95
96
97 void View::btn_exception_cb(Fl_Button* btn, void* view)
98 {
99     try
100     {
101         View* thisView = static_cast<View*>(view);
102         thisView->mController->throwException();
103     }
104     // ----------- Exception Handling ------------
105     catch(const std::string &e)
106     {
107         showErrorMsg(e);
108     }
109     catch(...)
110     {
111         showErrorMsg("Unknown error occured!");
112     }
113 }