]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Add fl_set_input_return to input_paperoption.
[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 #include <vector>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "xformsBC.h"
22 #include "ControlToc.h"
23 #include "FormToc.h"
24 #include "form_toc.h"
25 #include "helper_funcs.h" // getStringFromVector
26 #include "support/lstrings.h" // frontStrip, strip
27 #include "debug.h"
28 #include "gettext.h"
29
30
31 typedef FormCB<ControlToc, FormDB<FD_form_toc> > base_class;
32
33 FormToc::FormToc(ControlToc & c)
34         : base_class(c, _("Table of Contents"))
35 {}
36
37
38 void FormToc::build()
39 {
40         dialog_.reset(build_toc());
41
42         string const choice =
43                 " " + getStringFromVector(controller().getTypes(), " | ") + " ";
44         fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
45
46         // Manage the cancel/close button
47         bc().setCancel(dialog_->button_cancel);
48 }
49
50
51 void FormToc::update()
52 {
53         updateType();
54         updateContents();
55 }
56
57
58 ButtonPolicy::SMInput FormToc::input(FL_OBJECT *, long)
59 {
60         updateContents();
61
62         unsigned int const choice = fl_get_browser( dialog_->browser_toc );
63
64         if (choice - 1 < toclist_.size() && choice >= 1) {
65                 controller().Goto(toclist_[choice - 1].par->id());
66         }
67
68         return ButtonPolicy::SMI_VALID;
69 }
70
71
72 void FormToc::updateType()
73 {
74         // Update the choice list from scratch
75         fl_clear_choice(dialog_->choice_toc_type);
76         string const choice =
77                 " " + getStringFromVector(controller().getTypes(), " | ") + " ";
78         fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
79
80         // And select the correct one
81         string const type = toc::getType(controller().params().getCmdName());
82         
83         fl_set_choice(dialog_->choice_toc_type, 1);
84         for (int i = 1;
85              i <= fl_get_choice_maxitems(dialog_->choice_toc_type); ++i) {
86                 string const choice =
87                         fl_get_choice_item_text(dialog_->choice_toc_type, i);
88
89                 if (choice == type) {
90                         fl_set_choice(dialog_->choice_toc_type, i);
91                         break;
92                 }
93         }
94 }
95
96
97 void FormToc::updateContents()
98 {
99         string const type =
100                 frontStrip(strip(fl_get_choice_text(dialog_->choice_toc_type)));
101
102         Buffer::SingleList const contents = controller().getContents(type);
103
104         if (contents.empty()) {
105                 fl_clear_browser(dialog_->browser_toc);
106                 fl_add_browser_line(dialog_->browser_toc,
107                                     _("*** No Lists ***"));
108         }
109         
110         // Check if all elements are the same.
111         if (toclist_ == contents) {
112                 return;
113         }
114         
115         // List has changed. Update browser
116         toclist_ = contents;
117
118         unsigned int const topline =
119                 fl_get_browser_topline(dialog_->browser_toc);
120         unsigned int const line = fl_get_browser(dialog_->browser_toc);
121
122         fl_clear_browser( dialog_->browser_toc );
123
124         Buffer::SingleList::const_iterator cit = toclist_.begin();
125         Buffer::SingleList::const_iterator end = toclist_.end();
126         
127         for (; cit != end; ++cit) {
128                 string const line = string(4 * cit->depth, ' ') + cit->str;
129                 fl_add_browser_line(dialog_->browser_toc, line.c_str());
130         }
131         
132         fl_set_browser_topline(dialog_->browser_toc, topline);
133         fl_select_browser_line(dialog_->browser_toc, line);
134 }