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