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