]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
some further work on the float 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         Buffer::Lists const tmp = lv_->view()->buffer()->getLists();
75         Buffer::Lists::const_iterator cit = tmp.begin();
76         Buffer::Lists::const_iterator end = tmp.end();
77         for (; cit != end; ++cit) {
78                 fl_addto_choice(dialog_->choice_toc_type, cit->first.c_str());
79         }
80 #endif
81         // Don't need to limit size of this dialog
82         // (but fixing min size is a GOOD thing).
83         // Workaround dumb xforms sizing bug
84         minw_ = form()->w;
85         minh_ = form()->h;
86
87         // Manage the cancel/close button
88         bc_.setCancel(dialog_->button_cancel);
89         bc_.refresh();
90 }
91
92
93 void FormToc::update()
94 {
95 #if 0
96         Buffer::TocType type;
97
98         if (params.getCmdName() == "tableofcontents" )
99                 type = Buffer::TOC_TOC;
100
101         else if (params.getCmdName() == "listofalgorithms" )
102                 type = Buffer::TOC_LOA;
103
104         else if (params.getCmdName() == "listoffigures" )
105                 type = Buffer::TOC_LOF;
106
107         else
108                 type = Buffer::TOC_LOT;
109         
110         fl_set_choice( dialog_->choice_toc_type, type+1 );
111 #else
112 #warning Reimplement (Lgb)
113 #endif
114         updateToc();
115 }
116
117
118 void FormToc::updateToc()
119 {
120 #if 0
121         if (!lv_->view()->available()) {
122                 toclist.clear();
123                 fl_clear_browser( dialog_->browser_toc );
124                 fl_add_browser_line( dialog_->browser_toc,
125                                      _("*** No Document ***"));
126                 return;
127         }
128
129         vector<vector<Buffer::TocItem> > tmp =
130                 lv_->view()->buffer()->getTocList();
131         int type = fl_get_choice( dialog_->choice_toc_type ) - 1;
132
133         // Check if all elements are the same.
134         if (toclist.size() == tmp[type].size()) {
135                 unsigned int i = 0;
136                 for (; i < toclist.size(); ++i) {
137                         if (toclist[i] !=  tmp[type][i])
138                                 break;
139                 }
140                 if (i >= toclist.size()) return;
141         }
142
143         // List has changed. Update browser
144         toclist = tmp[type];
145
146         static Buffer * buffer = 0;
147         int topline = 0;
148         int line = 0;
149         if (buffer == lv_->view()->buffer()) {
150                 topline = fl_get_browser_topline( dialog_->browser_toc );
151                 line = fl_get_browser( dialog_->browser_toc );
152         } else
153                 buffer = lv_->view()->buffer();
154
155         fl_clear_browser( dialog_->browser_toc );
156
157         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
158              it != toclist.end(); ++it)
159                 fl_add_browser_line( dialog_->browser_toc,
160                                      (string(4 * (*it).depth, ' ')
161                                       + (*it).str).c_str());
162
163         fl_set_browser_topline( dialog_->browser_toc, topline );
164         fl_select_browser_line( dialog_->browser_toc, line );
165 #else
166 #warning Fix Me! (Lgb)
167         if (!lv_->view()->available()) {
168                 toclist.clear();
169                 fl_clear_browser( dialog_->browser_toc );
170                 fl_add_browser_line( dialog_->browser_toc,
171                                      _("*** No Document ***"));
172                 return;
173         }
174
175         Buffer::Lists tmp = lv_->view()->buffer()->getLists();
176         string const type =
177                 fl_get_choice_item_text(dialog_->choice_toc_type,
178                                         fl_get_choice(dialog_->choice_toc_type));
179
180         Buffer::Lists::iterator it = tmp.find(type);
181
182         if (it != tmp.end()) {
183                 // Check if all elements are the same.
184                 if (toclist == it->second) {
185                         return;
186                 }
187         } else if (it == tmp.end()) {
188                 toclist.clear();
189                 fl_clear_browser(dialog_->browser_toc);
190                 fl_add_browser_line(dialog_->browser_toc,
191                                     _("*** No Lists ***"));
192                 return;
193         }
194         
195         // List has changed. Update browser
196         toclist = it->second;
197
198         static Buffer * buffer = 0;
199         int topline = 0;
200         int line = 0;
201         if (buffer == lv_->view()->buffer()) {
202                 topline = fl_get_browser_topline(dialog_->browser_toc);
203                 line = fl_get_browser( dialog_->browser_toc );
204         } else
205                 buffer = lv_->view()->buffer();
206
207         fl_clear_browser(dialog_->browser_toc);
208
209         Buffer::SingleList::const_iterator cit = toclist.begin();
210         Buffer::SingleList::const_iterator end = toclist.end();
211         
212         for (; cit != end; ++cit) {
213                 string const line = string(4 * cit->depth, ' ') + cit->str;
214                 fl_add_browser_line(dialog_->browser_toc, line.c_str());
215         }
216         
217         fl_set_browser_topline(dialog_->browser_toc, topline);
218         fl_select_browser_line(dialog_->browser_toc, line);
219 #endif
220 }
221
222  
223 bool FormToc::input(FL_OBJECT *, long)
224 {
225         updateToc();
226
227         unsigned int const choice = fl_get_browser( dialog_->browser_toc );
228         if (0 < choice && choice - 1 < toclist.size()) {
229                 string const tmp = tostr(toclist[choice-1].par->id());
230                 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp);
231         }
232
233         return true;
234 }