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