]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
patch from dekel and patch from angus
[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 "gettext.h"
23 #include "Dialogs.h"
24 #include "FormToc.h"
25 #include "LyXView.h"
26 #include "form_toc.h"
27 #include "lyxtext.h"
28 #include "lyxfunc.h"
29 #include "support/lstrings.h"
30
31 FormToc::FormToc(LyXView * lv, Dialogs * d)
32         : FormCommand(lv, d, _("Table of Contents")), dialog_(0)
33 {
34         // let the dialog be shown
35         // These are permanent connections so we won't bother
36         // storing a copy because we won't be disconnecting.
37         d->showTOC.connect(slot(this, &FormToc::showInset));
38         d->createTOC.connect(slot(this, &FormToc::createInset));
39 }
40
41
42 FormToc::~FormToc()
43 {
44         delete dialog_;
45 }
46
47
48 void FormToc::clearStore()
49 {
50         toclist.clear();
51 }
52
53
54 void FormToc::build()
55 {
56         dialog_ = build_toc();
57         fl_addto_choice(dialog_->type,
58                         _(" TOC | LOF | LOT | LOA "));
59
60         // Don't need to limit size of this dialog
61 }
62
63
64 FL_FORM * const FormToc::form() const
65 {
66         if ( dialog_ ) // no need to test for dialog_->form
67                 return dialog_->form;
68         else
69                 return 0;
70 }
71
72
73 void FormToc::update()
74 {
75         Buffer::TocType type;
76
77         if( params.getCmdName() == "tableofcontents" )
78                 type = Buffer::TOC_TOC;
79
80         else if( params.getCmdName() == "listofalgorithms" )
81                 type = Buffer::TOC_LOA;
82
83         else if( params.getCmdName() == "listoffigures" )
84                 type = Buffer::TOC_LOF;
85
86         else
87                 type = Buffer::TOC_LOT;
88         
89         fl_set_choice( dialog_->type, type+1 );
90
91         updateToc();
92 }
93
94
95 void FormToc::updateToc()
96 {
97         if (!lv_->view()->available()) {
98                 toclist.clear();
99                 fl_clear_browser( dialog_->browser );
100                 fl_add_browser_line( dialog_->browser, _("*** No Document ***"));
101                 return;
102         }
103
104         vector<vector<Buffer::TocItem> > tmp =
105                 lv_->view()->buffer()->getTocList();
106         int type = fl_get_choice( dialog_->type ) - 1;
107
108         // Check if all elements are the same.
109         if (toclist.size() == tmp[type].size()) {
110                 unsigned int i = 0;
111                 for (; i < toclist.size(); ++i) {
112                         if (toclist[i] !=  tmp[type][i])
113                                 break;
114                 }
115                 if (i >= toclist.size()) return;
116         }
117
118         // List has changed. Update browser
119         toclist = tmp[type];
120
121         static Buffer * buffer = 0;
122         int topline = 0;
123         int line = 0;
124         if (buffer == lv_->view()->buffer()) {
125                 topline = fl_get_browser_topline( dialog_->browser );
126                 line = fl_get_browser( dialog_->browser );
127         } else
128                 buffer = lv_->view()->buffer();
129
130         fl_clear_browser( dialog_->browser );
131
132         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
133              it != toclist.end(); ++it)
134                 fl_add_browser_line( dialog_->browser,
135                                      (string(4*(*it).depth,' ')+(*it).str).c_str());
136
137         fl_set_browser_topline( dialog_->browser, topline );
138         fl_select_browser_line( dialog_->browser, line );
139 }
140
141  
142 void FormToc::apply()
143 {
144         if (!lv_->view()->available())
145                 return;
146
147         updateToc();
148
149         unsigned int choice = fl_get_browser( dialog_->browser );
150         if (0 < choice && choice - 1 < toclist.size()) {
151                 string tmp = tostr(toclist[choice-1].par->id());
152                 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp.c_str());
153         }
154 }