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