]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormToc.C
try this for distinguishing inner and outer tabs
[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 // The current code uses the apply() for handling the Update button and the
31 // type-of-table selection and cancel() for the close button.  This is a little
32 // confusing to the button controller so I've made an IgnorantPolicy to cover
33 // this situation since the dialog doesn't care about buttons. ARRae 20001013
34 FormToc::FormToc(LyXView * lv, Dialogs * d)
35         : FormCommand(lv, d, _("Table of Contents"), new OkCancelPolicy),
36           dialog_(0)
37 {
38         // let the dialog be shown
39         // These are permanent connections so we won't bother
40         // storing a copy because we won't be disconnecting.
41         d->showTOC.connect(slot(this, &FormToc::showInset));
42         d->createTOC.connect(slot(this, &FormToc::createInset));
43 }
44
45
46 FormToc::~FormToc()
47 {
48         delete dialog_;
49 }
50
51
52 FL_FORM * FormToc::form() const
53 {
54         if (dialog_ ) return dialog_->form;
55         return 0;
56 }
57
58
59 void FormToc::disconnect()
60 {
61         toclist.clear();
62         FormCommand::disconnect();
63 }
64
65
66 void FormToc::build()
67 {
68         dialog_ = build_toc();
69
70         fl_addto_choice(dialog_->choice_toc_type,
71                         _(" TOC | LOF | LOT | LOA "));
72
73         // Don't need to limit size of this dialog
74         // (but fixing min size is a GOOD thing).
75         // Workaround dumb xforms sizing bug
76         minw_ = form()->w;
77         minh_ = form()->h;
78
79         // Manage the cancel/close button
80         bc_.setCancel(dialog_->button_cancel);
81         bc_.refresh();
82 }
83
84
85 void FormToc::update()
86 {
87         Buffer::TocType type;
88
89         if (params.getCmdName() == "tableofcontents" )
90                 type = Buffer::TOC_TOC;
91
92         else if (params.getCmdName() == "listofalgorithms" )
93                 type = Buffer::TOC_LOA;
94
95         else if (params.getCmdName() == "listoffigures" )
96                 type = Buffer::TOC_LOF;
97
98         else
99                 type = Buffer::TOC_LOT;
100         
101         fl_set_choice( dialog_->choice_toc_type, type+1 );
102
103         updateToc();
104 }
105
106
107 void FormToc::updateToc()
108 {
109         if (!lv_->view()->available()) {
110                 toclist.clear();
111                 fl_clear_browser( dialog_->browser_toc );
112                 fl_add_browser_line( dialog_->browser_toc,
113                                      _("*** No Document ***"));
114                 return;
115         }
116
117         vector<vector<Buffer::TocItem> > tmp =
118                 lv_->view()->buffer()->getTocList();
119         int type = fl_get_choice( dialog_->choice_toc_type ) - 1;
120
121         // Check if all elements are the same.
122         if (toclist.size() == tmp[type].size()) {
123                 unsigned int i = 0;
124                 for (; i < toclist.size(); ++i) {
125                         if (toclist[i] !=  tmp[type][i])
126                                 break;
127                 }
128                 if (i >= toclist.size()) return;
129         }
130
131         // List has changed. Update browser
132         toclist = tmp[type];
133
134         static Buffer * buffer = 0;
135         int topline = 0;
136         int line = 0;
137         if (buffer == lv_->view()->buffer()) {
138                 topline = fl_get_browser_topline( dialog_->browser_toc );
139                 line = fl_get_browser( dialog_->browser_toc );
140         } else
141                 buffer = lv_->view()->buffer();
142
143         fl_clear_browser( dialog_->browser_toc );
144
145         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
146              it != toclist.end(); ++it)
147                 fl_add_browser_line( dialog_->browser_toc,
148                                      (string(4 * (*it).depth, ' ')
149                                       + (*it).str).c_str());
150
151         fl_set_browser_topline( dialog_->browser_toc, topline );
152         fl_select_browser_line( dialog_->browser_toc, line );
153 }
154
155  
156 bool FormToc::input(FL_OBJECT *, long)
157 {
158         updateToc();
159
160         unsigned int const choice = fl_get_browser( dialog_->browser_toc );
161         if (0 < choice && choice - 1 < toclist.size()) {
162                 string const tmp = tostr(toclist[choice-1].par->id());
163                 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp);
164         }
165
166         return true;
167 }