]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Removed // -*- C++ -*- from all .C files!
[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         string const type = toc::getType(controller().params().getCmdName());
75         
76         fl_set_choice(dialog_->choice_toc_type, 1);
77         for (int i = 1;
78              i <= fl_get_choice_maxitems(dialog_->choice_toc_type); ++i) {
79                 string const choice =
80                         fl_get_choice_item_text(dialog_->choice_toc_type, i);
81
82                 if (choice == type) {
83                         fl_set_choice(dialog_->choice_toc_type, i);
84                         break;
85                 }
86         }
87 }
88
89
90 void FormToc::updateContents()
91 {
92         string const type =
93                 frontStrip(strip(fl_get_choice_text(dialog_->choice_toc_type)));
94
95         Buffer::SingleList const contents = controller().getContents(type);
96
97         if (contents.empty()) {
98                 fl_clear_browser(dialog_->browser_toc);
99                 fl_add_browser_line(dialog_->browser_toc,
100                                     _("*** No Lists ***"));
101         }
102         
103         // Check if all elements are the same.
104         if (toclist_ == contents) {
105                 return;
106         }
107         
108         // List has changed. Update browser
109         toclist_ = contents;
110
111         unsigned int const topline =
112                 fl_get_browser_topline(dialog_->browser_toc);
113         unsigned int const line = fl_get_browser(dialog_->browser_toc);
114
115         fl_clear_browser( dialog_->browser_toc );
116
117         Buffer::SingleList::const_iterator cit = toclist_.begin();
118         Buffer::SingleList::const_iterator end = toclist_.end();
119         
120         for (; cit != end; ++cit) {
121                 string const line = string(4 * cit->depth, ' ') + cit->str;
122                 fl_add_browser_line(dialog_->browser_toc, line.c_str());
123         }
124         
125         fl_set_browser_topline(dialog_->browser_toc, topline);
126         fl_select_browser_line(dialog_->browser_toc, line);
127 }