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