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