]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Angus's xforms patch + Preferences redesign -- need help fixing BadDrawable + Abort...
[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 FL_FORM * FormToc::form() const
48 {
49         if ( dialog_ ) return dialog_->form;
50         return 0;
51 }
52
53
54 void FormToc::clearStore()
55 {
56         toclist.clear();
57 }
58
59
60 void FormToc::build()
61 {
62         dialog_ = build_toc();
63         fl_addto_choice(dialog_->type,
64                         _(" TOC | LOF | LOT | LOA "));
65
66         // Don't need to limit size of this dialog
67 }
68
69
70 void FormToc::update()
71 {
72         Buffer::TocType type;
73
74         if ( params.getCmdName() == "tableofcontents" )
75                 type = Buffer::TOC_TOC;
76
77         else if ( params.getCmdName() == "listofalgorithms" )
78                 type = Buffer::TOC_LOA;
79
80         else if ( params.getCmdName() == "listoffigures" )
81                 type = Buffer::TOC_LOF;
82
83         else
84                 type = Buffer::TOC_LOT;
85         
86         fl_set_choice( dialog_->type, type+1 );
87
88         updateToc();
89 }
90
91
92 void FormToc::updateToc()
93 {
94         if (!lv_->view()->available()) {
95                 toclist.clear();
96                 fl_clear_browser( dialog_->browser );
97                 fl_add_browser_line( dialog_->browser, _("*** No Document ***"));
98                 return;
99         }
100
101         vector<vector<Buffer::TocItem> > tmp =
102                 lv_->view()->buffer()->getTocList();
103         int type = fl_get_choice( dialog_->type ) - 1;
104
105         // Check if all elements are the same.
106         if (toclist.size() == tmp[type].size()) {
107                 unsigned int i = 0;
108                 for (; i < toclist.size(); ++i) {
109                         if (toclist[i] !=  tmp[type][i])
110                                 break;
111                 }
112                 if (i >= toclist.size()) return;
113         }
114
115         // List has changed. Update browser
116         toclist = tmp[type];
117
118         static Buffer * buffer = 0;
119         int topline = 0;
120         int line = 0;
121         if (buffer == lv_->view()->buffer()) {
122                 topline = fl_get_browser_topline( dialog_->browser );
123                 line = fl_get_browser( dialog_->browser );
124         } else
125                 buffer = lv_->view()->buffer();
126
127         fl_clear_browser( dialog_->browser );
128
129         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
130              it != toclist.end(); ++it)
131                 fl_add_browser_line( dialog_->browser,
132                                      (string(4*(*it).depth,' ')+(*it).str).c_str());
133
134         fl_set_browser_topline( dialog_->browser, topline );
135         fl_select_browser_line( dialog_->browser, line );
136 }
137
138  
139 void FormToc::apply()
140 {
141         if (!lv_->view()->available())
142                 return;
143
144         updateToc();
145
146         unsigned int const choice = fl_get_browser( dialog_->browser );
147         if (0 < choice && choice - 1 < toclist.size()) {
148                 string const tmp = tostr(toclist[choice-1].par->id());
149                 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp);
150         }
151 }