]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Change glob() API to accept a dir parameter.
[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 std::vector;
25 using std::string;
26
27 namespace lyx {
28
29 using support::getStringFromVector;
30
31 namespace frontend {
32
33 typedef FormController<ControlToc, FormView<FD_toc> > base_class;
34
35 FormToc::FormToc(Dialog & parent)
36         : base_class(parent, _("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         bcview().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 ***").c_str());
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 ***").c_str());
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 }
143
144 } // namespace frontend
145 } // namespace lyx