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