]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Rewrote the maths panel so that ALL the popups now derive from FormBaseBD,
[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 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13 #include <vector>
14
15 #include FORMS_H_LOCATION
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21
22 #include "Dialogs.h"
23 #include "FormToc.h"
24 #include "LyXView.h"
25 #include "form_toc.h"
26 #include "lyxtext.h"
27 #include "lyxfunc.h"
28 #include "support/lstrings.h"
29
30 using std::vector;
31 using SigC::slot;
32
33 // The current code uses the apply() for handling the Update button and the
34 // type-of-table selection and cancel() for the close button.  This is a little
35 // confusing to the button controller so I've made an IgnorantPolicy to cover
36 // this situation since the dialog doesn't care about buttons. ARRae 20001013
37 FormToc::FormToc(LyXView * lv, Dialogs * d)
38         : FormCommand(lv, d, _("Table of Contents")),
39           dialog_(0)
40 {
41         // let the dialog be shown
42         // These are permanent connections so we won't bother
43         // storing a copy because we won't be disconnecting.
44         d->showTOC.connect(slot(this, &FormToc::showInset));
45         d->createTOC.connect(slot(this, &FormToc::createInset));
46 }
47
48
49 FL_FORM * FormToc::form() const
50 {
51         if (dialog_.get())
52                 return dialog_->form;
53         return 0;
54 }
55
56
57 void FormToc::disconnect()
58 {
59         toclist.clear();
60         FormCommand::disconnect();
61 }
62
63
64 void FormToc::build()
65 {
66         dialog_.reset(build_toc());
67
68 #if 0
69         fl_addto_choice(dialog_->choice_toc_type,
70                         _(" TOC | LOF | LOT | LOA "));
71 #else
72         Buffer::Lists const tmp = lv_->view()->buffer()->getLists();
73         Buffer::Lists::const_iterator cit = tmp.begin();
74         Buffer::Lists::const_iterator end = tmp.end();
75         for (; cit != end; ++cit) {
76                 fl_addto_choice(dialog_->choice_toc_type, cit->first.c_str());
77         }
78 #endif
79
80         // Manage the cancel/close button
81         bc().setCancel(dialog_->button_cancel);
82         bc().refresh();
83 }
84
85
86 void FormToc::update()
87 {
88 #if 0
89         Buffer::TocType type;
90
91         if (params.getCmdName() == "tableofcontents" )
92                 type = Buffer::TOC_TOC;
93
94         else if (params.getCmdName() == "listofalgorithms" )
95                 type = Buffer::TOC_LOA;
96
97         else if (params.getCmdName() == "listoffigures" )
98                 type = Buffer::TOC_LOF;
99
100         else
101                 type = Buffer::TOC_LOT;
102         
103         fl_set_choice( dialog_->choice_toc_type, type+1 );
104 #else
105 #warning Reimplement (Lgb)
106 #endif
107         updateToc();
108 }
109
110
111 void FormToc::updateToc()
112 {
113 #if 0
114         if (!lv_->view()->available()) {
115                 toclist.clear();
116                 fl_clear_browser( dialog_->browser_toc );
117                 fl_add_browser_line( dialog_->browser_toc,
118                                      _("*** No Document ***"));
119                 return;
120         }
121
122         vector<vector<Buffer::TocItem> > tmp =
123                 lv_->view()->buffer()->getTocList();
124         int type = fl_get_choice( dialog_->choice_toc_type ) - 1;
125
126         // Check if all elements are the same.
127         if (toclist.size() == tmp[type].size()) {
128                 unsigned int i = 0;
129                 for (; i < toclist.size(); ++i) {
130                         if (toclist[i] !=  tmp[type][i])
131                                 break;
132                 }
133                 if (i >= toclist.size()) return;
134         }
135
136         // List has changed. Update browser
137         toclist = tmp[type];
138
139         static Buffer * buffer = 0;
140         int topline = 0;
141         int line = 0;
142         if (buffer == lv_->view()->buffer()) {
143                 topline = fl_get_browser_topline( dialog_->browser_toc );
144                 line = fl_get_browser( dialog_->browser_toc );
145         } else
146                 buffer = lv_->view()->buffer();
147
148         fl_clear_browser( dialog_->browser_toc );
149
150         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
151              it != toclist.end(); ++it)
152                 fl_add_browser_line( dialog_->browser_toc,
153                                      (string(4 * (*it).depth, ' ')
154                                       + (*it).str).c_str());
155
156         fl_set_browser_topline( dialog_->browser_toc, topline );
157         fl_select_browser_line( dialog_->browser_toc, line );
158 #else
159 #warning Fix Me! (Lgb)
160         if (!lv_->view()->available()) {
161                 toclist.clear();
162                 fl_clear_browser( dialog_->browser_toc );
163                 fl_add_browser_line( dialog_->browser_toc,
164                                      _("*** No Document ***"));
165                 return;
166         }
167
168         Buffer::Lists tmp = lv_->view()->buffer()->getLists();
169         string const type =
170                 fl_get_choice_item_text(dialog_->choice_toc_type,
171                                         fl_get_choice(dialog_->choice_toc_type));
172
173         Buffer::Lists::iterator it = tmp.find(type);
174
175         if (it != tmp.end()) {
176                 // Check if all elements are the same.
177                 if (toclist == it->second) {
178                         return;
179                 }
180         } else if (it == tmp.end()) {
181                 toclist.clear();
182                 fl_clear_browser(dialog_->browser_toc);
183                 fl_add_browser_line(dialog_->browser_toc,
184                                     _("*** No Lists ***"));
185                 return;
186         }
187         
188         // List has changed. Update browser
189         toclist = it->second;
190
191         static Buffer * buffer = 0;
192         int topline = 0;
193         int line = 0;
194         if (buffer == lv_->view()->buffer()) {
195                 topline = fl_get_browser_topline(dialog_->browser_toc);
196                 line = fl_get_browser( dialog_->browser_toc );
197         } else
198                 buffer = lv_->view()->buffer();
199
200         fl_clear_browser(dialog_->browser_toc);
201
202         Buffer::SingleList::const_iterator cit = toclist.begin();
203         Buffer::SingleList::const_iterator end = toclist.end();
204         
205         for (; cit != end; ++cit) {
206                 string const line = string(4 * cit->depth, ' ') + cit->str;
207                 fl_add_browser_line(dialog_->browser_toc, line.c_str());
208         }
209         
210         fl_set_browser_topline(dialog_->browser_toc, topline);
211         fl_select_browser_line(dialog_->browser_toc, line);
212 #endif
213 }
214
215  
216 bool FormToc::input(FL_OBJECT *, long)
217 {
218         updateToc();
219
220         unsigned int const choice = fl_get_browser( dialog_->browser_toc );
221         if (0 < choice && choice - 1 < toclist.size()) {
222                 string const tmp = tostr(toclist[choice-1].par->id());
223                 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp);
224         }
225
226         return true;
227 }