]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
implement getLabelList
[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
28
29 typedef FormCB<ControlToc, FormDB<FD_form_toc> > base_class;
30
31 FormToc::FormToc(ControlToc & c)
32         : base_class(c, _("Table of Contents"))
33 {}
34
35
36 void FormToc::build()
37 {
38         dialog_.reset(build_toc());
39
40         string const choice =
41                 " " + getStringFromVector(controller().getTypes(), " | ") + " ";
42         fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
43
44         // Manage the cancel/close button
45         bc().setCancel(dialog_->button_cancel);
46 }
47
48
49 void FormToc::update()
50 {
51         updateType();
52         updateContents();
53 }
54
55
56 ButtonPolicy::SMInput FormToc::input(FL_OBJECT *, long)
57 {
58         updateContents();
59
60         unsigned int const choice = fl_get_browser( dialog_->browser_toc );
61         if (0 < choice && choice - 1 < toclist_.size()) {
62                 controller().Goto(toclist_[choice-1].par->id());
63         }
64
65         return ButtonPolicy::SMI_VALID;
66 }
67
68
69 void FormToc::updateType()
70 {
71         string const type = toc::getType(controller().params().getCmdName());
72         
73         fl_set_choice(dialog_->choice_toc_type, 1);
74         for (int i = 1;
75              i <= fl_get_choice_maxitems(dialog_->choice_toc_type); ++i) {
76                 string const choice =
77                         fl_get_choice_item_text(dialog_->choice_toc_type, i);
78
79                 if (choice == type) {
80                         fl_set_choice(dialog_->choice_toc_type, i);
81                         break;
82                 }
83         }
84 }
85
86
87 void FormToc::updateContents()
88 {
89         string const type =
90                 frontStrip(strip(fl_get_choice_text(dialog_->choice_toc_type)));
91
92         Buffer::SingleList const contents = controller().getContents(type);
93
94         // Check if all elements are the same.
95         if (toclist_ == contents) {
96                 return;
97         }
98         
99         // List has changed. Update browser
100         toclist_ = contents;
101
102         unsigned int const topline =
103                 fl_get_browser_topline(dialog_->browser_toc);
104         unsigned int const line = fl_get_browser(dialog_->browser_toc);
105
106         fl_clear_browser( dialog_->browser_toc );
107
108         Buffer::SingleList::const_iterator cit = toclist_.begin();
109         Buffer::SingleList::const_iterator end = toclist_.end();
110         
111         for (; cit != end; ++cit) {
112                 string const line = string(4 * cit->depth, ' ') + cit->str;
113                 fl_add_browser_line(dialog_->browser_toc, line.c_str());
114         }
115         
116         fl_set_browser_topline(dialog_->browser_toc, topline);
117         fl_select_browser_line(dialog_->browser_toc, line);
118 }