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