]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Angus's FormInset work; Dekel's languages patch; my reworking of Angus's stuff +...
[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 IgnorantPolicy),
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         fl_addto_choice(dialog_->type,
70                         _(" TOC | LOF | LOT | LOA "));
71
72         // Don't need to limit size of this dialog
73 }
74
75
76 // we can safely ignore the parameter because we can always update
77 void FormToc::update(bool)
78 {
79         Buffer::TocType type;
80
81         if ( params.getCmdName() == "tableofcontents" )
82                 type = Buffer::TOC_TOC;
83
84         else if ( params.getCmdName() == "listofalgorithms" )
85                 type = Buffer::TOC_LOA;
86
87         else if ( params.getCmdName() == "listoffigures" )
88                 type = Buffer::TOC_LOF;
89
90         else
91                 type = Buffer::TOC_LOT;
92         
93         fl_set_choice( dialog_->type, type+1 );
94
95         updateToc();
96 }
97
98
99 void FormToc::updateToc()
100 {
101         if (!lv_->view()->available()) {
102                 toclist.clear();
103                 fl_clear_browser( dialog_->browser );
104                 fl_add_browser_line( dialog_->browser,
105                                      _("*** No Document ***"));
106                 return;
107         }
108
109         vector<vector<Buffer::TocItem> > tmp =
110                 lv_->view()->buffer()->getTocList();
111         int type = fl_get_choice( dialog_->type ) - 1;
112
113         // Check if all elements are the same.
114         if (toclist.size() == tmp[type].size()) {
115                 unsigned int i = 0;
116                 for (; i < toclist.size(); ++i) {
117                         if (toclist[i] !=  tmp[type][i])
118                                 break;
119                 }
120                 if (i >= toclist.size()) return;
121         }
122
123         // List has changed. Update browser
124         toclist = tmp[type];
125
126         static Buffer * buffer = 0;
127         int topline = 0;
128         int line = 0;
129         if (buffer == lv_->view()->buffer()) {
130                 topline = fl_get_browser_topline( dialog_->browser );
131                 line = fl_get_browser( dialog_->browser );
132         } else
133                 buffer = lv_->view()->buffer();
134
135         fl_clear_browser( dialog_->browser );
136
137         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
138              it != toclist.end(); ++it)
139                 fl_add_browser_line( dialog_->browser,
140                                      (string(4 * (*it).depth, ' ')
141                                       + (*it).str).c_str());
142
143         fl_set_browser_topline( dialog_->browser, topline );
144         fl_select_browser_line( dialog_->browser, line );
145 }
146
147  
148 void FormToc::apply()
149 {
150         if (!lv_->view()->available())
151                 return;
152
153         updateToc();
154
155         unsigned int const choice = fl_get_browser( dialog_->browser );
156         if (0 < choice && choice - 1 < toclist.size()) {
157                 string const tmp = tostr(toclist[choice-1].par->id());
158                 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp);
159         }
160 }