5696e81f0152ed18af281de54aaa18a62dad06f3
[hsd_doku_tool.git] / src / View.cxx
1 ///////////////////////////////////////////////////////////////////////////
2 // Workfile: View.cxx
3 // Author: Daniel Giritzer <daniel@giritzer.eu>
4 // Date: 26.04.2018
5 // Description: Diese Klasse stellt eine platformunabhängige Visualisierung
6 //              des Models zur verfügung.
7 // Remarks: -
8 ///////////////////////////////////////////////////////////////////////////
9 #include "View.h"
10 #include "Author.h"
11 #include "ChapterIF.h"
12 #include "ChapterFactory.h"
13 #include "FSHelper.h"
14 #include <FL/Fl_File_Chooser.H>
15 #include <iostream>
16 #include <cstring>
17
18 View::View(ControllerIF::SPtr contr, ModelIF::SPtr model)
19 {
20     mController = contr;
21     mModel = model;
22     mEditorChapterLabel = nullptr;
23     mSelectSourceLabel = nullptr;
24     subscribeSubject(mModel.get());
25 }
26
27 View::~View()
28 {
29     win_doctool->hide();
30     win_chapt->hide();
31     win_settings->hide();
32     win_auth_coord->hide();
33     win_depart_coord->hide();
34     win_log_view->hide();
35
36     // Ressourcen freigeben
37     Fl::delete_widget(win_doctool);
38     Fl::delete_widget(win_chapt);
39     Fl::delete_widget(win_settings);
40     Fl::delete_widget(win_auth_coord);
41     Fl::delete_widget(win_depart_coord);
42     Fl::delete_widget(win_log_view);
43     unsubscribeSubject(mModel.get());
44     delete[] mEditorChapterLabel;
45     delete[] mSelectSourceLabel;
46 }
47
48 void View::activate()
49 {
50
51     // Author
52     cb_author->activate();
53     ti_name->activate();
54     ti_est_time->activate();
55     ti_needed_time->activate();
56     ti_ID->activate();
57     btn_save_author->activate();
58     br_chapters->activate();
59
60     // Kapitel schnellauswahl
61     // + status
62     chb_finished->activate();
63     btn_next->activate();
64     pb_progress->activate();
65
66     // Dokumentname
67     ti_doc_name->activate();
68
69     // Abteilung
70     ti_department->activate();
71
72     // Rechtsklick Menüs
73     menu_editor->activate();
74     menu_author->activate();
75     menu_chapter->activate();
76     menu_department->activate();
77
78     // Kaptielmanipulation
79     btn_up_chpt->activate();
80     btn_down_chpt->activate();
81
82     // Menüleiste
83     menu_settings->activate();
84     menu_gen_templ->activate();
85     menu_out_dir->activate();
86
87     // Deaktiviere Laden menüs
88     menu_new_templ->deactivate();
89     menu_open_template->deactivate();
90 }
91
92 void View::loadChapterList()
93 {
94     if(br_chapters->size() != 0)
95         br_chapters->clear();
96
97     for(auto curChapter : mModel->getChapterNames())
98         br_chapters->add(curChapter.c_str());
99 }
100
101 void View::loadAuthorList()
102 {
103     cb_author->clear();
104     for(auto curAuthor : mModel->getAuthorNames())
105         cb_author->add(curAuthor.c_str());
106     cb_author->value(mModel->getAuthorNum());
107 }
108
109 void View::loadChapterContent()
110 {
111     br_chapters->select(mModel->getChapterNum());
112     chb_finished->value(mModel->getSelChapter()->GetDone());
113
114     // Chapter Einstellungs Fenster
115     add_chapt_chaptname->value(mModel->getSelChapter()->GetPrettyName().c_str());
116     add_chapt_fname->value(mModel->getSelChapter()->GetOutFileName().c_str());
117     spin_hierachy->value(mModel->getSelChapter()->GetHierachyAsNum());
118
119     // Kopiere C String für Label
120     if(mEditorChapterLabel!=nullptr) delete[] mEditorChapterLabel;
121     mEditorChapterLabel = new char [mModel->getSelChapter()->GetPrettyName().length()+1];
122     std::strcpy (mEditorChapterLabel, mModel->getSelChapter()->GetPrettyName().c_str());
123     tb_editor->label(mEditorChapterLabel);
124     win_doctool->label(mModel->getSubjectName().c_str());
125
126     // Quellcode Einstellungen
127     if(mModel->getSelChapter()->GetType() == mModel->getChapterTypes().at(ChapterFactory::TypeSourceChapter) ||
128             mModel->getSelChapter()->GetType() == mModel->getChapterTypes().at(ChapterFactory::TypeDoxygenChapter)
129       )
130     {
131         // Editor Ausblenden
132         tb_editor->hide();
133         menu_editor->hide();
134
135         // Quellcode Einstellungen einblenden
136         ot_src_dir->show();
137         btn_open_src_dir->show();
138         border_src->show();
139
140         if(mSelectSourceLabel!=nullptr) delete[] mSelectSourceLabel;
141         mSelectSourceLabel = new char [mModel->getSelChapter()->GetType().length()+1];
142         std::strcpy (mSelectSourceLabel, mModel->getSelChapter()->GetType().c_str());
143         tb_editor->label(mEditorChapterLabel);
144         lb_type->label(mSelectSourceLabel);
145         lb_type->show();
146
147         // Aktiviere/Deaktiviere
148         // je nachdem ob kapitel als abgeschlossen
149         // markiert wurde
150         if(!mModel->getSelChapter()->GetDone())
151         {
152             ot_src_dir->activate();
153             btn_open_src_dir->activate();
154         }
155         else
156         {
157             ot_src_dir->deactivate();
158             btn_open_src_dir->deactivate();
159         }
160         ot_src_dir->value(mModel->getSelChapter()->GetContent().c_str());
161     }
162
163     // Editor
164     if(mModel->getSelChapter()->GetType() == mModel->getChapterTypes().at(ChapterFactory::TypeTextChapter))
165     {
166         // Quellcode Einstellungen ausblenden
167         ot_src_dir->hide();
168         btn_open_src_dir->hide();
169         lb_type->hide();
170         border_src->hide();
171
172         // Editor Einblenden
173         tb_editor->show();
174         menu_editor->show();
175
176         // Lade text in textbuffer von tb_editor
177         mTextBuf->text(mModel->getSelChapter()->GetContent().c_str());
178
179         // Aktiviere/Deaktiviere textfeld
180         // je nachdem ob kapitel als abgeschlossen
181         // markiert wurde
182         if(mModel->getSelChapter()->GetDone())
183         {
184             tb_editor->clear_active();
185             menu_editor->deactivate();
186         }
187         else
188         {
189             tb_editor->activate();
190             menu_editor->activate();
191         }
192     }
193 }
194
195 void View::loadAuthorContent()
196 {
197     ti_name->value(mModel->getSelAuthor()->GetName().c_str());
198     ti_est_time->value(mModel->getSelAuthor()->GetTimeEstimated().c_str());
199     ti_needed_time->value(mModel->getSelAuthor()->GetTimeSpent().c_str());
200     ti_ID->value(mModel->getSelAuthor()->GetID().c_str());
201
202     // Koordinaten für in titelblatt eingefügte texte
203     vi_spenttime_x->value(mModel->getSelAuthor()->GetXTimeSpentCoordinate());
204     vi_spenttime_y->value(mModel->getSelAuthor()->GetYTimeSpentCoordinate());
205     vi_authname_x->value(mModel->getSelAuthor()->GetXNameCoordinate());
206     vi_authname_y->value(mModel->getSelAuthor()->GetYNameCoordinate());
207     vi_esttime_x->value(mModel->getSelAuthor()->GetXTimeEstimatedCoordinate());
208     vi_esttime_y->value(mModel->getSelAuthor()->GetYTimeEstimatedCoordinate());
209     vi_id_x->value(mModel->getSelAuthor()->GetXid());
210     vi_id_y->value(mModel->getSelAuthor()->GetYid());
211 }
212
213 void View::loadSettings()
214 {
215     // Einstellungen Menü
216     chb_add_covpage->value(mModel->shouldAddCoverSheet());
217     chb_add_titlepage->value(mModel->shouldAddTitlePage());
218     chb_add_toc->value(mModel->shouldAddTableOfContent());
219     spin_toc_depth->value(mModel->getTableOfContentDepth());
220     ti_settings_subject_name->value(mModel->getSubjectName().c_str());
221     ti_location->value(mModel->getLocation().c_str());
222     ti_header_ext->value(mModel->getHeaderFileExtension().c_str());
223     ti_src_ext->value(mModel->getSourceFileExtension().c_str());
224 }
225
226 void View::loadGeneralGUIElements()
227 {
228     // progressbar
229     pb_progress->maximum(mModel->getNumChapters());
230     pb_progress->value(mModel->getNumFinishedChapters());
231
232
233     // Generate Button
234     if(mModel->getGenerateEnable())
235         btn_generate->activate();
236     else
237         btn_generate->clear_active();
238
239
240     // Titelblatt Einstellungen
241     if(mModel->shouldAddCoverSheet())
242     {
243         ot_cov_sheet->activate();
244         btn_open_cov_sheet->activate();
245         ot_cov_sheet->value(mModel->getCovSheet().c_str());
246     }
247     else
248     {
249         ot_cov_sheet->deactivate();
250         btn_open_cov_sheet->deactivate();
251         ot_cov_sheet->value("");
252     }
253
254     // Dokomenten Name
255     ti_doc_name->value(mModel->getDocName().c_str());
256     ti_department->value(mModel->getDepartment().c_str());
257
258     vi_depart_x->value(mModel->getDepartmentX());
259     vi_depart_y->value(mModel->getDepartmentY());
260 }
261
262 void View::updatedBySubject(Subject* s)
263 {
264     if(s == nullptr)
265         throw std::string("Nullpointer given as Subject!");
266
267
268     if(mModel->getChapterNames().size() != 0)
269     {
270         try
271         {
272
273             // Autor
274             loadAuthorList();
275             loadAuthorContent();
276
277             // Kapitel
278             loadChapterList();
279             loadChapterContent();
280
281             // Einstellungen
282             loadSettings();
283
284             // Andere GUI Elemente
285             loadGeneralGUIElements();
286
287             win_doctool->redraw();
288         }
289         // ----------- Exception Handling ------------
290         catch(std::string& e)
291         {
292             std::cerr << e << std::endl;
293             fl_alert(e.c_str()); //Textbox mit fehlermeldung
294         }
295     }
296 }
297
298 void View::show()
299 {
300     // Setze fenster style
301     Fl::scheme("gtk+");
302
303     // definiere callback funktionen
304     chb_finished->callback((Fl_Callback*)chb_finished_cb, (void*)(this));
305     btn_next->callback((Fl_Callback*)btn_next_cb, (void*)(this) );
306     btn_generate->callback((Fl_Callback*)btn_generate_cb, (void*)(this));
307     br_chapters->callback((Fl_Callback*)br_chapters_cb, (void*)(this));
308     menu_open_template->callback((Fl_Callback*)menu_open_template_cb, (void*)(this));
309     menu_editor->callback((Fl_Callback*)menu_editor_cb, (void*)(this));
310     menu_chapter->callback((Fl_Callback*)menu_chapter_cb, (void*)(this));
311     menu_author->callback((Fl_Callback*)menu_author_cb, (void*)(this));
312     menu_department->callback((Fl_Callback*)menu_department_cb, (void*)(this));
313     btn_up_chpt->callback((Fl_Callback*)btn_up_chpt_cb, (void*)(this) );
314     btn_down_chpt->callback((Fl_Callback*)btn_down_chpt_cb, (void*)(this) );
315     btn_save_author->callback((Fl_Callback*)btn_save_author_cb, (void*)(this));
316     cb_author->callback((Fl_Callback*)cb_author_cb, (void*)(this));
317     btn_chapt_save->callback((Fl_Callback*)btn_chapt_save_cb, (void*)(this));
318     menu_out_dir->callback((Fl_Callback*)menu_out_dir_cb, (void*)(this));
319     btn_open_src_dir->callback((Fl_Callback*)ot_src_dir_cb, (void*)(this));
320     btn_open_cov_sheet->callback((Fl_Callback*)ot_cov_sheet_cb, (void*)(this));
321     win_doctool->callback((Fl_Callback*)win_close_cb, (void*)(this));
322     menu_exit->callback((Fl_Callback*)win_close_cb, (void*)(this));
323     btn_ok_author->callback((Fl_Callback*)btn_ok_author_cb, (void*)(this));
324     menu_settings->callback((Fl_Callback*)menu_settings_cb, (void*)(this));
325     btn_save_settings->callback((Fl_Callback*)btn_save_settings_cb, (void*)(this));
326     menu_new_templ->callback((Fl_Callback*)menu_new_templ_cb, (void*)(this));
327     menu_gen_templ->callback((Fl_Callback*)menu_gen_templ_cb, (void*)(this));
328     btn_ok_depart->callback((Fl_Callback*)btn_ok_depart_cb, (void*)(this));
329     ti_department->callback((Fl_Callback*)ti_department_cb, (void*)(this));
330     ti_doc_name->callback((Fl_Callback*)ti_doc_name_cb, (void*)(this));
331
332
333     // Lade Kapiteltypen
334     for(auto curType : mModel->getChapterTypes())
335         choice_chapt_type->add(curType.c_str());
336
337     //Ersten auswählen
338     choice_chapt_type->value(0);
339
340     // Textfeld initialisieren
341     mTextBuf = new Fl_Text_Buffer();
342     tb_editor->buffer(mTextBuf);
343     tb_editor->clear_active();
344
345     mLogBuf = new Fl_Text_Buffer();
346     td_log_view->buffer(mLogBuf);
347
348     //widget einstellungen
349     br_chapters->type(FL_HOLD_BROWSER);
350     pb_progress->color(FL_WHITE, FL_DARK_GREEN);
351
352     //Rechtsklick menüs
353     menu_editor->type(Fl_Menu_Button::POPUP3);
354     menu_author->type(Fl_Menu_Button::POPUP3);
355     menu_chapter->type(Fl_Menu_Button::POPUP3);
356     menu_department->type(Fl_Menu_Button::POPUP3);
357
358     // Zeichne GUI
359     win_doctool->show();
360 }
361
362
363 void View::saveBuffer()
364 {
365     if(mModel->getSelChapter()->GetType() == mModel->getChapterTypes().at(ChapterFactory::TypeDoxygenChapter) ||
366             mModel->getSelChapter()->GetType() == mModel->getChapterTypes().at(ChapterFactory::TypeSourceChapter)
367       )
368     {
369         std::string text = ot_src_dir->value();
370         mController->saveContent(text);
371     }
372     if(mModel->getSelChapter()->GetType() == mModel->getChapterTypes().at(ChapterFactory::TypeTextChapter))
373     {
374         char* text = mTextBuf->text();
375         mController->saveContent(text);
376         free(text); // Hier muss free verwendet werde, da mTextBuf->text() speicher mit malloc allokiert
377     }
378 }
379
380 // -------------------------------------------------
381 // ----------- FLTK Callback Funktionen ------------
382 // -------------------------------------------------
383 void View::chb_finished_cb(Fl_Check_Button* btn, void* view)
384 {
385     try
386     {
387         View* thisView = static_cast<View*>(view);
388         thisView->saveBuffer();
389         thisView->mController->setCurrentChapterFinished(thisView->chb_finished->value());
390     }
391     // ----------- Exception Handling ------------
392     catch(std::string& e)
393     {
394         std::cerr << e << std::endl;
395         fl_alert(e.c_str()); //Textbox mit fehlermeldung
396     }
397     catch(...)
398     {
399         fl_alert("Unknown error occured!");
400         std::cerr << "Unknown error occured!" << std::endl;
401     }
402 }
403
404 void View::btn_next_cb(Fl_Button* btn, void* view)
405 {
406
407     try
408     {
409         View* thisView = static_cast<View*>(view);
410         thisView->saveBuffer();
411         int selectedNum=1;
412         for (selectedNum=1 ; selectedNum<=thisView->br_chapters->size(); selectedNum++ )
413             if (thisView->br_chapters->selected(selectedNum)) break;
414
415
416         if(selectedNum < thisView->br_chapters->size())
417             thisView->mController->selectedChapter(selectedNum+1);
418     }
419     // ----------- Exception Handling ------------
420     catch(std::string& e)
421     {
422         std::cerr << e << std::endl;
423         fl_alert(e.c_str()); //Textbox mit fehlermeldung
424     }
425     catch(...)
426     {
427         fl_alert("Unknown error occured!");
428         std::cerr << "Unknown error occured!" << std::endl;
429     }
430 }
431
432
433 void View::btn_generate_cb(Fl_Button* btn, void* view)
434 {
435     try
436     {
437         View* thisView = static_cast<View*>(view);
438         thisView->saveBuffer();
439         thisView->win_doctool->deactivate();
440         thisView->win_doctool->cursor(FL_CURSOR_WAIT);
441
442
443         Fl::check();
444         thisView->mController->generateDocument();
445
446         if(thisView->mLogBuf->loadfile(std::string(thisView->mModel->getOutPath() + thisView->mModel->getLogFile()).c_str()) != 0)
447             throw std::string("Error opening Logfile!");
448
449         thisView->win_log_view->show();
450         thisView->win_doctool->activate();
451         thisView->win_doctool->cursor(FL_CURSOR_DEFAULT);
452     }
453     // ----------- Exception Handling ------------
454     catch(std::string& e)
455     {
456         std::cerr << e << std::endl;
457         fl_alert(e.c_str()); //Textbox mit fehlermeldung
458         View* thisView = static_cast<View*>(view);
459         thisView->win_doctool->activate();
460         thisView->win_doctool->cursor(FL_CURSOR_DEFAULT);
461     }
462     catch(...)
463     {
464         fl_alert("Unknown error occured!");
465         std::cerr << "Unknown error occured!" << std::endl;
466         View* thisView = static_cast<View*>(view);
467         thisView->win_doctool->activate();
468         thisView->win_doctool->cursor(FL_CURSOR_DEFAULT);
469     }
470 }
471
472
473 void View::br_chapters_cb(Fl_Browser* br, void* view)
474 {
475     try
476     {
477         View* thisView = static_cast<View*>(view);
478
479         thisView->saveBuffer();
480
481         int selectedNum=1;
482         for (selectedNum =1 ; selectedNum<thisView->br_chapters->size(); selectedNum++ )
483             if (thisView->br_chapters->selected(selectedNum)) break;
484
485         thisView->mController->selectedChapter(selectedNum);
486     }
487     // ----------- Exception Handling ------------
488     catch(std::string& e)
489     {
490         std::cerr << e << std::endl;
491         fl_alert(e.c_str()); //Textbox mit fehlermeldung
492     }
493     catch(...)
494     {
495         fl_alert("Unknown error occured!");
496         std::cerr << "Unknown error occured!" << std::endl;
497     }
498 }
499
500 void View::menu_open_template_cb(Fl_Menu_* menu, void* view)
501 {
502     try
503     {
504         View* thisView = static_cast<View*>(view);
505
506         // File browser dialog
507         Fl_File_Chooser chooser(thisView->mModel->getConfDir().c_str(),
508                                 "Template Files (*.xml)",
509                                 Fl_File_Chooser::SINGLE,
510                                 "Select Template...");
511         chooser.show();
512
513         // Warte bis benutzer datei ausgewählt hat
514         while(chooser.shown())
515             Fl::wait();
516
517         if ( chooser.value() != nullptr )
518         {
519             thisView->mController->setTemplate(chooser.value());
520
521             while(thisView->mModel->getOutPath().empty())
522                 thisView->menu_out_dir->do_callback(nullptr);
523
524             thisView->activate();
525         }
526     }
527     // ----------- Exception Handling ------------
528     catch(std::string& e)
529     {
530         std::cerr << e << std::endl;
531         fl_alert(e.c_str()); //Textbox mit fehlermeldung
532     }
533     catch(...)
534     {
535         fl_alert("Illegal config file!");
536         std::cerr << "Illegal config file!" << std::endl;
537     }
538 }
539
540 void View::menu_editor_cb(Fl_Menu_* menu, void* view)
541 {
542     try
543     {
544         View* thisView = static_cast<View*>(view);
545
546         // Ermittle gewähltes menü
547         const char *text = menu->text();
548         if ( !text ) return;
549         if ( strcmp(text, "h1") == 0 )
550         {
551             thisView->tb_editor->insert("\\section{}");
552         }
553         if ( strcmp(text, "h2") == 0 )
554         {
555             thisView->tb_editor->insert("\\subsection{}");
556         }
557         if ( strcmp(text, "h3") == 0 )
558         {
559             thisView->tb_editor->insert("\\subsubsection{}");
560         }
561         if ( strcmp(text, "List") == 0 )
562         {
563             thisView->tb_editor->insert("\\begin{itemize}\n\\item One\n\\item Two\n\\end{itemize}");
564         }
565         if ( strcmp(text, "Page Break") == 0 )
566         {
567             thisView->tb_editor->insert("\\newpage");
568         }
569         if ( strcmp(text, "Line Break") == 0 )
570         {
571             thisView->tb_editor->insert("\\newline");
572         }
573         if ( strcmp(text, "Bold") == 0 )
574         {
575             thisView->tb_editor->insert("\\textbf{Bold Text}");
576         }
577         if ( strcmp(text, "Italic") == 0 )
578         {
579             thisView->tb_editor->insert("\\textit{Italic Text}");
580         }
581         if ( strcmp(text, "Underline") == 0 )
582         {
583             thisView->tb_editor->insert("\\underline{Underlined Text}");
584         }
585         if ( strcmp(text, "Center") == 0 )
586         {
587             thisView->tb_editor->insert("\\begin{center}\nCentered Text\n\\end{center}");
588         }
589         if ( strcmp(text, "Unit") == 0 )
590         {
591             thisView->tb_editor->insert("\\(mA\\)");
592         }
593         if ( strcmp(text, "Equal Sign") == 0 )
594         {
595             thisView->tb_editor->insert("&=");
596         }
597         if ( strcmp(text, "Fraction") == 0 )
598         {
599             thisView->tb_editor->insert("\\frac{numerator}{denominator}");
600         }
601         if ( strcmp(text, "Power") == 0 )
602         {
603             thisView->tb_editor->insert("num^{pow}");
604         }
605         if ( strcmp(text, "Intext Equation") == 0 )
606         {
607             thisView->tb_editor->insert("$ a = b$");
608         }
609         if ( strcmp(text, "Normal Equation") == 0 )
610         {
611         thisView->tb_editor->insert("\\begin{equation*}\n\\begin{split}\n1psi &= \\frac{1 lbf}{sq in}\\\\\n\\end{split}\n\\end{equation*}");
612         }
613         if ( strcmp(text, "Picture") == 0 )
614         {
615             // File browser dialog
616             Fl_File_Chooser chooser(".",
617                                     "Image Files (*.{bmp,gif,jpg,jpeg,png}",
618                                     Fl_File_Chooser::SINGLE,
619                                     "Select image file...");
620             chooser.show();
621
622             // Warte bis benutzer datei ausgewählt hat
623             while(chooser.shown())
624                 Fl::wait();
625
626             if ( chooser.value() != nullptr )
627             {
628                 std::string file = thisView->mFSHelp.getFileName(chooser.value());
629                 thisView->mFSHelp.makeDir(thisView->mModel->getOutPath() + "resources/");
630                 thisView->mFSHelp.copyFile(chooser.value(),thisView->mModel->getOutPath() + "resources/" + file);
631                 thisView->tb_editor->insert(std::string("\\begin{figure}\n\\centering\n\\includegraphics[width=0.6\\textwidth]{resources/"+file+"}" +
632                                                         "\n\\caption{Short description}\n\\label{fig:PictureLabel}\n\\end{figure}\n"+
633                                                         "% Use ~\\ref{fig:PictureLabel} to reference to this \n% picture within text.").c_str());
634             }
635         }
636         if ( strcmp(text, "Text") == 0 )
637         {
638             // File browser dialog
639             Fl_File_Chooser chooser(".",
640                                     "Text Files (*.{txt,tex,log,info,conf}",
641                                     Fl_File_Chooser::SINGLE,
642                                     "Select input file...");
643             chooser.show();
644
645             // Warte bis benutzer datei ausgewählt hat
646             while(chooser.shown())
647                 Fl::wait();
648
649             if ( chooser.value() != nullptr )
650             {
651                 std::string file = thisView->mFSHelp.getFileName(chooser.value());
652                 thisView->mFSHelp.makeDir(thisView->mModel->getOutPath() + "resources/");
653                 thisView->mFSHelp.copyFile(chooser.value(),thisView->mModel->getOutPath() + "resources/" + file);
654                 thisView->tb_editor->insert(std::string("\\input{resources/" + file + "}").c_str());
655             }
656         }
657
658
659     }
660     // ----------- Exception Handling ------------
661     catch(std::string& e)
662     {
663         std::cerr << e << std::endl;
664         fl_alert(e.c_str()); //Textbox mit fehlermeldung
665     }
666     catch(...)
667     {
668         fl_alert("Unknown error occured!");
669         std::cerr << "Unknown error occured!" << std::endl;
670     }
671 }
672
673 void View::menu_author_cb(Fl_Menu_* menu, void* view)
674 {
675     try
676     {
677         View* thisView = static_cast<View*>(view);
678
679         // Ermittle gewähltes menü
680         const char *text = menu->text();
681         if ( !text ) return;
682         if ( strcmp(text, "Add author") == 0 )
683         {
684             thisView->mController->addAuthor();
685         }
686         if ( strcmp(text, "Remove author") == 0 )
687         {
688             thisView->mController->remAuthor();
689         }
690         if ( strcmp(text, "Edit coordinates") == 0 )
691         {
692             if(!thisView->mModel->shouldAddCoverSheet())
693                 throw std::string("No need to set coordinates without cover page!");
694
695             thisView->win_auth_coord->show();
696         }
697     }
698     // ----------- Exception Handling ------------
699     catch(std::string& e)
700     {
701         std::cerr << e << std::endl;
702         fl_alert(e.c_str()); //Textbox mit fehlermeldung
703     }
704     catch(...)
705     {
706         fl_alert("Unknown error occured!");
707         std::cerr << "Unknown error occured!" << std::endl;
708     }
709 }
710
711 void View::menu_chapter_cb(Fl_Menu_* menu, void* view)
712 {
713     try
714     {
715         View* thisView = static_cast<View*>(view);
716
717         // Ermittle gewähltes menü
718         const char *text = menu->text();
719         if ( !text ) return;
720         if ( strcmp(text, "Add chapter") == 0 )
721         {
722             thisView->add_chapt_chaptname->value("");
723             thisView->add_chapt_fname->value(".tex");
724             thisView->spin_hierachy->value(0);
725             thisView->choice_chapt_type->activate();
726             thisView->choice_chapt_type->show();
727             thisView->win_chapt->show();
728         }
729         if ( strcmp(text, "Edit chapter") == 0 )
730         {
731             thisView->loadChapterContent();
732             thisView->choice_chapt_type->deactivate();
733             thisView->win_chapt->show();
734             thisView->choice_chapt_type->hide();
735         }
736         if ( strcmp(text, "Remove chapter") == 0 )
737         {
738             thisView->mController->remChapter();
739         }
740     }
741     // ----------- Exception Handling ------------
742     catch(std::string& e)
743     {
744         std::cerr << e << std::endl;
745         fl_alert(e.c_str()); //Textbox mit fehlermeldung
746     }
747     catch(...)
748     {
749         fl_alert("Unknown error occured!");
750         std::cerr << "Unknown error occured!" << std::endl;
751     }
752 }
753
754 void View::btn_up_chpt_cb(Fl_Button* btn, void* view)
755 {
756     try
757     {
758         View* thisView = static_cast<View*>(view);
759         thisView->saveBuffer();
760         thisView->mController->upChapter();
761     }
762     // ----------- Exception Handling ------------
763     catch(std::string& e)
764     {
765         std::cerr << e << std::endl;
766         fl_alert(e.c_str()); //Textbox mit fehlermeldung
767     }
768     catch(...)
769     {
770         fl_alert("Unknown error occured!");
771         std::cerr << "Unknown error occured!" << std::endl;
772     }
773 }
774
775 void View::btn_down_chpt_cb(Fl_Button* btn, void* view)
776 {
777     try
778     {
779         View* thisView = static_cast<View*>(view);
780         thisView->saveBuffer();
781         thisView->mController->downChapter();
782     }
783     // ----------- Exception Handling ------------
784     catch(std::string& e)
785     {
786         std::cerr << e << std::endl;
787         fl_alert(e.c_str()); //Textbox mit fehlermeldung
788     }
789     catch(...)
790     {
791         fl_alert("Unknown error occured!");
792         std::cerr << "Unknown error occured!" << std::endl;
793     }
794 }
795
796 void View::btn_save_author_cb(Fl_Button* btn, void* view)
797 {
798     try
799     {
800         View* thisView = static_cast<View*>(view);
801         thisView->mController->saveAuthor(thisView->ti_name->value(),
802                                           thisView->ti_ID->value(),
803                                           thisView->ti_est_time->value(),
804                                           thisView->ti_needed_time->value());
805     }
806     // ----------- Exception Handling ------------
807     catch(std::string& e)
808     {
809         std::cerr << e << std::endl;
810         fl_alert(e.c_str()); //Textbox mit fehlermeldung
811     }
812     catch(...)
813     {
814         fl_alert("Unknown error occured!");
815         std::cerr << "Unknown error occured!" << std::endl;
816     }
817 }
818
819 void View::cb_author_cb(Fl_Choice* cb, void* view)
820 {
821     try
822     {
823         View* thisView = static_cast<View*>(view);
824         thisView->mController->selectedAuthor(thisView->cb_author->value());
825     }
826     // ----------- Exception Handling ------------
827     catch(std::string& e)
828     {
829         std::cerr << e << std::endl;
830         fl_alert(e.c_str()); //Textbox mit fehlermeldung
831     }
832     catch(...)
833     {
834         fl_alert("Unknown error occured!");
835         std::cerr << "Unknown error occured!" << std::endl;
836     }
837 }
838
839 void View::btn_chapt_save_cb(Fl_Button* btn, void* view)
840 {
841     try
842     {
843         View* thisView = static_cast<View*>(view);
844
845         std::string chaptName = thisView->add_chapt_chaptname->value();
846         std::string fileName = thisView->add_chapt_fname->value();
847
848         if(!thisView->mFSHelp.validFileName(fileName))
849             throw std::string("Filename contains illegal characters.");
850
851         if(chaptName.empty())
852             throw std::string("Enter a chapter name.");
853
854         if(fileName.empty())
855             throw std::string("Enter a filename.");
856
857
858         if(thisView->choice_chapt_type->active())
859             thisView->mController->addChapter(chaptName, fileName, thisView->choice_chapt_type->value(), thisView->spin_hierachy->value());
860         else
861             thisView->mController->updateChapter(chaptName, fileName, thisView->spin_hierachy->value());
862
863         thisView->win_chapt->hide();
864     }
865     // ----------- Exception Handling ------------
866     catch(std::string& e)
867     {
868         std::cerr << e << std::endl;
869         fl_alert(e.c_str()); //Textbox mit fehlermeldung
870     }
871     catch(...)
872     {
873         fl_alert("Unknown error occured!");
874         std::cerr << "Unknown error occured!" << std::endl;
875     }
876 }
877
878 void View::menu_out_dir_cb(Fl_Menu_* menu, void* view)
879 {
880     try
881     {
882         View* thisView = static_cast<View*>(view);
883
884         // File browser dialog
885         Fl_File_Chooser chooser(".",
886                                 "*",
887                                 Fl_File_Chooser::DIRECTORY,
888                                 "Select output directory...");
889         chooser.newButton->activate();
890         chooser.show();
891
892         // Warte bis benutzer datei ausgewählt hat
893         while(chooser.shown())
894             Fl::wait();
895
896         // Abbruch gedrückt
897         if ( chooser.value() != nullptr )
898         {
899             thisView->mController->setOutDir(chooser.value());
900         }
901     }
902     // ----------- Exception Handling ------------
903     catch(std::string& e)
904     {
905         std::cerr << e << std::endl;
906         fl_alert(e.c_str()); //Textbox mit fehlermeldung
907     }
908     catch(...)
909     {
910         fl_alert("Unknown error occured!");
911         std::cerr << "Unknown error occured!" << std::endl;
912     }
913 }
914
915 void View::ot_src_dir_cb(Fl_Output *out, void* view)
916 {
917     try
918     {
919         View* thisView = static_cast<View*>(view);
920         // File browser dialog
921         Fl_File_Chooser chooser(".",
922                                 "*",
923                                 Fl_File_Chooser::DIRECTORY,
924                                 "Select source directory...");
925         chooser.show();
926
927         // Warte bis Benutzer ordner ausgewählt hat
928         while(chooser.shown())
929             Fl::wait();
930
931         // Überpüfe ob Abbruch gedrückt
932         if ( chooser.value() != nullptr )
933         {
934             thisView->ot_src_dir->value(chooser.value());
935             thisView->saveBuffer();
936         }
937     }
938     // ----------- Exception Handling ------------
939     catch(std::string& e)
940     {
941         std::cerr << e << std::endl;
942         fl_alert(e.c_str()); //Textbox mit fehlermeldung
943     }
944     catch(...)
945     {
946         fl_alert("Unknown error occured!");
947         std::cerr << "Unknown error occured!" << std::endl;
948     }
949 }
950
951
952 void View::ot_cov_sheet_cb(Fl_Output *out, void* view)
953 {
954     try
955     {
956         View* thisView = static_cast<View*>(view);
957
958         // File browser dialog
959         Fl_File_Chooser chooser(".",
960                                 "Cover Sheet (*.pdf)",
961                                 Fl_File_Chooser::SINGLE,
962                                 "Select cover sheet...");
963         chooser.show();
964
965         // Warte bis benutzer datei ausgewählt hat
966         while(chooser.shown())
967             Fl::wait();
968
969         // Abbruch gedrückt
970         if ( chooser.value() != nullptr )
971         {
972             thisView->mController->saveCoverSheet(chooser.value());
973         }
974     }
975     // ----------- Exception Handling ------------
976     catch(std::string& e)
977     {
978         std::cerr << e << std::endl;
979         fl_alert(e.c_str()); //Textbox mit fehlermeldung
980     }
981     catch(...)
982     {
983         fl_alert("Unknown error occured!");
984         std::cerr << "Unknown error occured!" << std::endl;
985     }
986 }
987
988 void View::win_close_cb(Fl_Window* win, void* view)
989 {
990     try
991     {
992         View* thisView = static_cast<View*>(view);
993
994         switch ( fl_choice("Do you really want to exit?", "No", "Yes", 0) )
995         {
996         case 0:
997             ;
998             break;
999         case 1:
1000             try
1001             {
1002                 thisView->mController->saveAllWrittenTextsToFiles();
1003             }
1004             catch(...)
1005             {
1006                 // Speichern fehlgeschlagen
1007                 std::cerr << "Sorry, could not save written texts to files..." << std::endl;
1008                 fl_alert("Sorry, could not save written texts to files...");
1009             }
1010             exit(0);
1011             break;
1012         default:
1013             break;
1014         }
1015     }
1016     // ----------- Exception Handling ------------
1017     catch(std::string& e)
1018     {
1019         std::cerr << e << std::endl;
1020         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1021     }
1022     catch(...)
1023     {
1024         fl_alert("Unknown error occured!");
1025         std::cerr << "Unknown error occured!" << std::endl;
1026     }
1027 }
1028
1029 void View::btn_ok_author_cb(Fl_Button* btn, void* view)
1030 {
1031     try
1032     {
1033         View* thisView = static_cast<View*>(view);
1034         thisView->mController->saveAuthorCoordinates(thisView->vi_authname_x->value(),thisView->vi_authname_y->value(),
1035                 thisView->vi_id_x->value(),thisView->vi_id_y->value(),
1036                 thisView->vi_esttime_x->value(),thisView->vi_esttime_y->value(),
1037                 thisView->vi_spenttime_x->value(),thisView->vi_spenttime_y->value()
1038                                                     );
1039         thisView->win_auth_coord->hide();
1040     }
1041     // ----------- Exception Handling ------------
1042     catch(std::string& e)
1043     {
1044         std::cerr << e << std::endl;
1045         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1046     }
1047     catch(...)
1048     {
1049         fl_alert("Unknown error occured!");
1050         std::cerr << "Unknown error occured!" << std::endl;
1051     }
1052 }
1053
1054 void View::menu_settings_cb(Fl_Menu_* menu, void* view)
1055 {
1056     try
1057     {
1058         View* thisView = static_cast<View*>(view);
1059         thisView->win_settings->show();
1060     }
1061     // ----------- Exception Handling ------------
1062     catch(std::string& e)
1063     {
1064         std::cerr << e << std::endl;
1065         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1066     }
1067     catch(...)
1068     {
1069         fl_alert("Unknown error occured!");
1070         std::cerr << "Unknown error occured!" << std::endl;
1071     }
1072 }
1073
1074 void View::btn_save_settings_cb(Fl_Button* btn, void* view)
1075 {
1076     try
1077     {
1078         View* thisView = static_cast<View*>(view);
1079
1080         std::string subjectName = thisView->ti_settings_subject_name->value();
1081         std::string location = thisView->ti_location->value();
1082         std::string header = thisView->ti_header_ext->value();
1083         std::string source = thisView->ti_src_ext->value();
1084
1085         if(header.empty())
1086             throw std::string("Please enter a header file extension.");
1087
1088         if(source.empty())
1089             throw std::string("Please enter a source file extension.");
1090
1091         if(subjectName.empty())
1092             throw std::string("Please enter a subject name.");
1093
1094         if(location.empty())
1095             throw std::string("Please enter a location.");
1096
1097         thisView->mController->saveSettings(thisView->chb_add_titlepage->value(), thisView->chb_add_covpage->value(),
1098                                             thisView->chb_add_toc->value(), thisView->spin_toc_depth->value(),
1099                                             header, source,
1100                                             subjectName, location
1101                                            );
1102
1103         thisView->win_settings->hide();
1104     }
1105     // ----------- Exception Handling ------------
1106     catch(std::string& e)
1107     {
1108         std::cerr << e << std::endl;
1109         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1110     }
1111     catch(...)
1112     {
1113         fl_alert("Unknown error occured!");
1114         std::cerr << "Unknown error occured!" << std::endl;
1115     }
1116 }
1117
1118 void View::menu_new_templ_cb(Fl_Menu_* menu, void* view)
1119 {
1120     try
1121     {
1122         View* thisView = static_cast<View*>(view);
1123         thisView->menu_out_dir->do_callback(nullptr);
1124
1125         if(thisView->mModel->getOutPath().empty())
1126             throw std::string("An output directory has to be set!");
1127
1128         menu_settings->do_callback(nullptr);
1129
1130         // Warte bis benutzer einstellungen eingegeben hat
1131         while(thisView->win_settings->shown())
1132             Fl::wait();
1133
1134         std::string subjName = thisView->ti_settings_subject_name->value();
1135         if(!subjName.empty())
1136         {
1137             thisView->activate();
1138             thisView->mController->startNewTemplate(subjName);
1139         }
1140     }
1141     // ----------- Exception Handling ------------
1142     catch(std::string& e)
1143     {
1144         std::cerr << e << std::endl;
1145         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1146     }
1147     catch(...)
1148     {
1149         fl_alert("Unknown error occured!");
1150         std::cerr << "Unknown error occured!" << std::endl;
1151     }
1152 }
1153
1154 void View::menu_gen_templ_cb(Fl_Menu_* menu, void* view)
1155 {
1156     try
1157     {
1158         View* thisView = static_cast<View*>(view);
1159         thisView->mController->saveAsTemplate();
1160         fl_alert(std::string("Successfully saved template to: " + thisView->mModel->getOutPath()).c_str());
1161     }
1162     // ----------- Exception Handling ------------
1163     catch(std::string& e)
1164     {
1165         std::cerr << e << std::endl;
1166         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1167     }
1168     catch(...)
1169     {
1170         fl_alert("Unknown error occured!");
1171         std::cerr << "Unknown error occured!" << std::endl;
1172     }
1173 }
1174
1175 void View::menu_department_cb(Fl_Menu_* menu, void* view)
1176 {
1177     try
1178     {
1179         View* thisView = static_cast<View*>(view);
1180
1181         // Ermittle gewähltes menü
1182         const char *text = menu->text();
1183         if ( !text ) return;
1184         if ( strcmp(text, "Edit coordinates") == 0 )
1185         {
1186             if(!thisView->mModel->shouldAddCoverSheet())
1187                 throw std::string("No need to set coordinates without cover page!");
1188
1189             thisView->win_depart_coord->show();
1190         }
1191     }
1192     // ----------- Exception Handling ------------
1193     catch(std::string& e)
1194     {
1195         std::cerr << e << std::endl;
1196         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1197     }
1198     catch(...)
1199     {
1200         fl_alert("Unknown error occured!");
1201         std::cerr << "Unknown error occured!" << std::endl;
1202     }
1203 }
1204
1205 void View::btn_ok_depart_cb(Fl_Button* cb, void* view)
1206 {
1207     try
1208     {
1209         View* thisView = static_cast<View*>(view);
1210         thisView->mController->setDepartmentCoordiantes(thisView->vi_depart_x->value(),thisView->vi_depart_y->value());
1211         thisView->win_depart_coord->hide();
1212     }
1213     // ----------- Exception Handling ------------
1214     catch(std::string& e)
1215     {
1216         std::cerr << e << std::endl;
1217         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1218     }
1219     catch(...)
1220     {
1221         fl_alert("Unknown error occured!");
1222         std::cerr << "Unknown error occured!" << std::endl;
1223     }
1224 }
1225
1226 void View::ti_doc_name_cb(Fl_Input * in, void* view)
1227 {
1228     try
1229     {
1230         View* thisView = static_cast<View*>(view);
1231         thisView->mController->setDocName(thisView->ti_doc_name->value());
1232     }
1233     // ----------- Exception Handling ------------
1234     catch(std::string& e)
1235     {
1236         std::cerr << e << std::endl;
1237         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1238     }
1239     catch(...)
1240     {
1241         fl_alert("Unknown error occured!");
1242         std::cerr << "Unknown error occured!" << std::endl;
1243     }
1244 }
1245
1246 void View::ti_department_cb(Fl_Input * in, void* view)
1247 {
1248     try
1249     {
1250         View* thisView = static_cast<View*>(view);
1251         thisView->mController->setDepartment(thisView->ti_department->value());
1252     }
1253     // ----------- Exception Handling ------------
1254     catch(std::string& e)
1255     {
1256         std::cerr << e << std::endl;
1257         fl_alert(e.c_str()); //Textbox mit fehlermeldung
1258     }
1259     catch(...)
1260     {
1261         fl_alert("Unknown error occured!");
1262         std::cerr << "Unknown error occured!" << std::endl;
1263     }
1264 }