]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Bugfixes: checkboxes to radiobuttons (from J�rgen S) and remove a little
[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_->choice_toc_type) {
64                 updateType();
65         }
66  
67         updateContents();
68
69         unsigned int const choice = fl_get_browser( dialog_->browser_toc );
70
71         if (choice - 1 < toclist_.size() && choice >= 1) {
72                 controller().Goto(toclist_[choice - 1].par->id());
73         }
74
75         return ButtonPolicy::SMI_VALID;
76 }
77
78
79 void FormToc::updateType()
80 {
81         // Update the choice list from scratch
82         fl_clear_choice(dialog_->choice_toc_type);
83         string const choice = getStringFromVector(controller().getTypes(), "|");
84         lyxerr << "choice " << choice << endl; 
85         fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
86
87         // And select the correct one
88         string const type = toc::getType(controller().params().getCmdName());
89         
90         fl_set_choice(dialog_->choice_toc_type, 1);
91         for (int i = 1;
92              i <= fl_get_choice_maxitems(dialog_->choice_toc_type); ++i) {
93                 string const choice =
94                         fl_get_choice_item_text(dialog_->choice_toc_type, i);
95
96                 if (choice == type) {
97                         fl_set_choice(dialog_->choice_toc_type, i);
98                         break;
99                 }
100         }
101 }
102
103
104 void FormToc::updateContents()
105 {
106         char const * tmp = fl_get_choice_text(dialog_->choice_toc_type);
107  
108         if (!tmp) {
109                 fl_clear_browser(dialog_->browser_toc);
110                 fl_add_browser_line(dialog_->browser_toc,
111                                     _("*** No Lists ***"));
112                 return;
113         }
114  
115         string const type = frontStrip(strip(tmp));
116
117         Buffer::SingleList const contents = controller().getContents(type);
118
119         if (contents.empty()) {
120                 fl_clear_browser(dialog_->browser_toc);
121                 fl_add_browser_line(dialog_->browser_toc,
122                                     _("*** No Lists ***"));
123         }
124         
125         // Check if all elements are the same.
126         if (toclist_ == contents) {
127                 return;
128         }
129         
130         // List has changed. Update browser
131         toclist_ = contents;
132
133         unsigned int const topline =
134                 fl_get_browser_topline(dialog_->browser_toc);
135         unsigned int const line = fl_get_browser(dialog_->browser_toc);
136
137         fl_clear_browser( dialog_->browser_toc );
138
139         Buffer::SingleList::const_iterator cit = toclist_.begin();
140         Buffer::SingleList::const_iterator end = toclist_.end();
141         
142         for (; cit != end; ++cit) {
143                 string const line = string(4 * cit->depth, ' ') + cit->str;
144                 fl_add_browser_line(dialog_->browser_toc, line.c_str());
145         }
146         
147         fl_set_browser_topline(dialog_->browser_toc, topline);
148         fl_select_browser_line(dialog_->browser_toc, line);
149 }