]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
fix Angus's forgotten initializations
[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 "buffer.h"
27 #include "form_toc.h"
28 #include "lyxtext.h"
29
30 static vector<Buffer::TocItem> toclist;
31
32 FormToc::FormToc(LyXView * lv, Dialogs * d)
33   : FormCommand(lv, d, _("Table of Contents")), dialog_(0)
34 {
35         // let the dialog be shown
36         // These are permanent connections so we won't bother
37         // storing a copy because we won't be disconnecting.
38         d->showTOC.connect(slot(this, &FormToc::showInset));
39         d->createTOC.connect(slot(this, &FormToc::createInset));
40 }
41
42
43 FormToc::~FormToc()
44 {
45         free();
46         delete dialog_;
47 }
48
49
50 void FormToc::build()
51 {
52         dialog_ = build_toc();
53         fl_addto_choice(dialog_->type,
54                         _(" TOC | LOF | LOT | LOA "));
55 }
56
57
58 FL_FORM * const FormToc::form() const
59 {
60         if( dialog_ && dialog_->form_toc )
61                 return dialog_->form_toc;
62         else
63                 return 0;
64 }
65
66
67 void FormToc::update()
68 {
69         static int ow = -1, oh;
70                 
71         if (ow < 0) {
72                 ow = form()->w;
73                 oh = form()->h;
74
75                 fl_set_form_minsize(form(), ow, oh);
76                 fl_set_form_maxsize(form(), 2*ow, oh);
77         }
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, _("*** No Document ***"));
105                 return;
106         }
107
108         vector<vector<Buffer::TocItem> > tmp =
109                 lv_->view()->buffer()->getTocList();
110         int type = fl_get_choice( dialog_->type ) - 1;
111
112         // Check if all elements are the same.
113         if (toclist.size() == tmp[type].size()) {
114                 unsigned int i = 0;
115                 for (; i < toclist.size(); ++i) {
116                         if (toclist[i] !=  tmp[type][i])
117                                 break;
118                 }
119                 if (i >= toclist.size()) return;
120         }
121
122         // List has changed. Update browser
123         toclist = tmp[type];
124
125         static Buffer * buffer = 0;
126         int topline = 0;
127         int line = 0;
128         if (buffer == lv_->view()->buffer()) {
129                 topline = fl_get_browser_topline( dialog_->browser );
130                 line = fl_get_browser( dialog_->browser );
131         } else
132                 buffer = lv_->view()->buffer();
133
134         fl_clear_browser( dialog_->browser );
135
136         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
137              it != toclist.end(); ++it)
138                 fl_add_browser_line( dialog_->browser,
139                                      (string(4*(*it).depth,' ')+(*it).str).c_str());
140
141         fl_set_browser_topline( dialog_->browser, topline );
142         fl_select_browser_line( dialog_->browser, line );
143 }
144
145  
146 void FormToc::apply()
147 {
148         if (!lv_->view()->available())
149                 return;
150
151         updateToc();
152
153         unsigned int choice = fl_get_browser( dialog_->browser );
154         if (0 < choice && choice - 1 < toclist.size()) {
155                 lv_->view()->beforeChange();
156                 lv_->view()->text->SetCursor( lv_->view(), toclist[choice-1].par, 0 );
157                 lv_->view()->text->sel_cursor = 
158                         lv_->view()->text->cursor;
159                 lv_->view()->update(BufferView::SELECT|BufferView::FITCUR);
160         }
161 }