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