]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
fix crash with "save as"
[lyx.git] / src / frontends / xforms / FormToc.C
1 /**
2  * \file xforms/FormToc.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "FormToc.h"
18 #include "xformsBC.h"
19 #include "xforms_helpers.h"
20 #include "ControlToc.h"
21 #include "forms/form_toc.h"
22 #include "support/lstrings.h" // frontStrip, strip
23 #include "debug.h"
24 #include "gettext.h"
25 #include FORMS_H_LOCATION
26
27 #include <vector>
28
29 using std::vector;
30 using std::endl;
31
32
33 typedef FormCB<ControlToc, FormDB<FD_toc> > base_class;
34
35 FormToc::FormToc()
36         : base_class(_("Table of Contents"))
37 {}
38
39
40 void FormToc::build()
41 {
42         dialog_.reset(build_toc(this));
43
44         vector<string> types = controller().getTypes();
45         
46         
47         string const choice =
48                 " " + getStringFromVector(controller().getTypes(), " | ") + " ";
49         fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
50
51         // Manage the cancel/close button
52         bc().setCancel(dialog_->button_close);
53 }
54
55
56 void FormToc::update()
57 {
58         updateType();
59         updateContents();
60 }
61
62
63 ButtonPolicy::SMInput FormToc::input(FL_OBJECT * ob, long)
64 {
65         if (ob == dialog_->browser_toc) {
66                 unsigned int const choice = fl_get_browser(dialog_->browser_toc);
67
68                 if (choice - 1 < toc_.size() && choice >= 1) {
69                         controller().goTo(toc_[choice - 1]);
70                 }
71                 return ButtonPolicy::SMI_VALID;
72         }
73
74         if (ob != dialog_->choice_toc_type) {
75                 updateType();
76         }
77
78         updateContents();
79
80         return ButtonPolicy::SMI_VALID;
81 }
82
83
84 void FormToc::updateType()
85 {
86         // Update the choice list from scratch
87         fl_clear_choice(dialog_->choice_toc_type);
88         string const choice = getStringFromVector(controller().getTypes(), "|");
89         fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
90
91         // And select the correct one
92         string const type = toc::getType(controller().params().getCmdName());
93         fl_set_choice_text(dialog_->choice_toc_type, type.c_str());
94 }
95
96
97 void FormToc::updateContents()
98 {
99         string const type = getString(dialog_->choice_toc_type);
100         if (type.empty()) {
101                 fl_clear_browser(dialog_->browser_toc);
102                 fl_add_browser_line(dialog_->browser_toc,
103                                     _("*** No Lists ***"));
104                 setEnabled(dialog_->browser_toc, false);
105                 return;
106         }
107
108         toc::Toc const contents = controller().getContents(type);
109
110         // Check if all elements are the same.
111         if (toc_ == contents) {
112                 return;
113         }
114
115         // List has changed. Update browser
116         toc_ = contents;
117
118         if (contents.empty()) {
119                 fl_clear_browser(dialog_->browser_toc);
120                 fl_add_browser_line(dialog_->browser_toc,
121                                     _("*** No Lists ***"));
122                 setEnabled(dialog_->browser_toc, false);
123                 return;
124         }
125
126         unsigned int const topline =
127                 fl_get_browser_topline(dialog_->browser_toc);
128         unsigned int const line = fl_get_browser(dialog_->browser_toc);
129
130         fl_clear_browser(dialog_->browser_toc);
131         setEnabled(dialog_->browser_toc, true);
132
133         toc::Toc::const_iterator cit = contents.begin();
134         toc::Toc::const_iterator end = contents.end();
135         for (; cit != end; ++cit) {
136                 fl_add_browser_line(dialog_->browser_toc,
137                                     cit->asString().c_str());
138         }
139
140         fl_set_browser_topline(dialog_->browser_toc, topline);
141         fl_select_browser_line(dialog_->browser_toc, line);
142 }