]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
Angus insetindex patch + protect patch from Dekel
[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         delete dialog_;
46 }
47
48
49 void FormToc::clearStore()
50 {
51         toclist.clear();
52 }
53
54
55 void FormToc::build()
56 {
57         dialog_ = build_toc();
58         fl_addto_choice(dialog_->type,
59                         _(" TOC | LOF | LOT | LOA "));
60 }
61
62
63 FL_FORM * const FormToc::form() const
64 {
65         if( dialog_ && dialog_->form_toc )
66                 return dialog_->form_toc;
67         else
68                 return 0;
69 }
70
71
72 void FormToc::update()
73 {
74         static int ow = -1, oh;
75                 
76         if (ow < 0) {
77                 ow = form()->w;
78                 oh = form()->h;
79
80                 fl_set_form_minsize(form(), ow, oh);
81                 fl_set_form_maxsize(form(), 2*ow, oh);
82         }
83
84         Buffer::TocType type;
85
86         if( params.getCmdName() == "tableofcontents" )
87                 type = Buffer::TOC_TOC;
88
89         else if( params.getCmdName() == "listofalgorithms" )
90                 type = Buffer::TOC_LOA;
91
92         else if( params.getCmdName() == "listoffigures" )
93                 type = Buffer::TOC_LOF;
94
95         else
96                 type = Buffer::TOC_LOT;
97         
98         fl_set_choice( dialog_->type, type+1 );
99
100         updateToc();
101 }
102
103
104 void FormToc::updateToc()
105 {
106         if (!lv_->view()->available()) {
107                 toclist.clear();
108                 fl_clear_browser( dialog_->browser );
109                 fl_add_browser_line( dialog_->browser, _("*** No Document ***"));
110                 return;
111         }
112
113         vector<vector<Buffer::TocItem> > tmp =
114                 lv_->view()->buffer()->getTocList();
115         int type = fl_get_choice( dialog_->type ) - 1;
116
117         // Check if all elements are the same.
118         if (toclist.size() == tmp[type].size()) {
119                 unsigned int i = 0;
120                 for (; i < toclist.size(); ++i) {
121                         if (toclist[i] !=  tmp[type][i])
122                                 break;
123                 }
124                 if (i >= toclist.size()) return;
125         }
126
127         // List has changed. Update browser
128         toclist = tmp[type];
129
130         static Buffer * buffer = 0;
131         int topline = 0;
132         int line = 0;
133         if (buffer == lv_->view()->buffer()) {
134                 topline = fl_get_browser_topline( dialog_->browser );
135                 line = fl_get_browser( dialog_->browser );
136         } else
137                 buffer = lv_->view()->buffer();
138
139         fl_clear_browser( dialog_->browser );
140
141         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
142              it != toclist.end(); ++it)
143                 fl_add_browser_line( dialog_->browser,
144                                      (string(4*(*it).depth,' ')+(*it).str).c_str());
145
146         fl_set_browser_topline( dialog_->browser, topline );
147         fl_select_browser_line( dialog_->browser, line );
148 }
149
150  
151 void FormToc::apply()
152 {
153         if (!lv_->view()->available())
154                 return;
155
156         updateToc();
157
158         unsigned int choice = fl_get_browser( dialog_->browser );
159         if (0 < choice && choice - 1 < toclist.size()) {
160                 lv_->view()->beforeChange();
161                 lv_->view()->text->SetCursor( lv_->view(), toclist[choice-1].par, 0 );
162                 lv_->view()->text->sel_cursor = 
163                         lv_->view()->text->cursor;
164                 lv_->view()->update(BufferView::SELECT|BufferView::FITCUR);
165         }
166 }