]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
controller-view split for TOC popup.
[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         bc().refresh();
47 }
48
49
50 void FormToc::update()
51 {
52         updateType();
53         updateContents();
54 }
55
56
57 ButtonPolicy::SMInput FormToc::input(FL_OBJECT *, long)
58 {
59         updateContents();
60
61         unsigned int const choice = fl_get_browser( dialog_->browser_toc );
62         if (0 < choice && choice - 1 < toclist_.size()) {
63                 controller().Goto(toclist_[choice-1].par->id());
64         }
65
66         return ButtonPolicy::SMI_VALID;
67 }
68
69
70 void FormToc::updateType()
71 {
72         string const type = toc::getType(controller().params().getCmdName());
73         
74         fl_set_choice(dialog_->choice_toc_type, 1);
75         for (int i = 1;
76              i <= fl_get_choice_maxitems(dialog_->choice_toc_type); ++i) {
77                 string const choice =
78                         fl_get_choice_item_text(dialog_->choice_toc_type, i);
79
80                 if (choice == type) {
81                         fl_set_choice(dialog_->choice_toc_type, i);
82                         break;
83                 }
84         }
85 }
86
87
88 void FormToc::updateContents()
89 {
90         string const type =
91                 frontStrip(strip(fl_get_choice_text(dialog_->choice_toc_type)));
92
93         Buffer::SingleList const contents = controller().getContents(type);
94
95         // Check if all elements are the same.
96         if (toclist_ == contents) {
97                 return;
98         }
99         
100         // List has changed. Update browser
101         toclist_ = contents;
102
103         unsigned int const topline =
104                 fl_get_browser_topline(dialog_->browser_toc);
105         unsigned int const line = fl_get_browser(dialog_->browser_toc);
106
107         fl_clear_browser( dialog_->browser_toc );
108
109         Buffer::SingleList::const_iterator cit = toclist_.begin();
110         Buffer::SingleList::const_iterator end = toclist_.end();
111         
112         for (; cit != end; ++cit) {
113                 string const line = string(4 * cit->depth, ' ') + cit->str;
114                 fl_add_browser_line(dialog_->browser_toc, line.c_str());
115         }
116         
117         fl_set_browser_topline(dialog_->browser_toc, topline);
118         fl_select_browser_line(dialog_->browser_toc, line);
119 }