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