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