]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/FormRef.C
implement getLabelList
[lyx.git] / src / frontends / qt2 / FormRef.C
1 /*
2  * FormRef.C
3  * (C) 2000 LyX Team
4  * John Levon, moz@compsoc.man.ac.uk
5  */
6
7 /***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15
16 #include <config.h>
17
18 #include "FormRefDialog.h"
19 #undef emit
20
21 #include "Dialogs.h"
22 #include "FormRef.h"
23 #include "gettext.h"
24 #include "buffer.h"
25 #include "LyXView.h"
26 #include "lyxfunc.h"
27 #include "debug.h"
28
29 #include <qpushbutton.h>
30 #include <qlineedit.h>
31 #include <qlistbox.h>
32 #include <qcombobox.h>
33 #include <qcheckbox.h>
34
35 using std::endl;
36
37 FormRef::FormRef(LyXView *v, Dialogs *d)
38         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
39         sort(0), gotowhere(GOTOREF), type(REF), refs(0)
40 {
41         // let the dialog be shown
42         // This is a permanent connection so we won't bother
43         // storing a copy because we won't be disconnecting.
44         d->showRef.connect(slot(this, &FormRef::showRef));
45         d->createRef.connect(slot(this, &FormRef::createRef));
46 }
47
48 FormRef::~FormRef()
49 {
50         delete dialog_;
51 }
52
53 void FormRef::showRef(InsetCommand * const inset)
54 {
55         // FIXME: when could inset be 0 here ?
56         if (inset==0)
57                 return;
58
59         inset_ = inset;
60         readonly = lv_->buffer()->isReadonly();
61         ih_ = inset_->hide.connect(slot(this,&FormRef::hide));
62         params = inset->params();
63         
64         show();
65 }
66
67 void FormRef::createRef(string const & arg)
68 {
69         if (inset_)
70                 close();
71
72         readonly = lv_->buffer()->isReadonly();
73         params.setFromString(arg);
74         show();
75 }
76
77 void FormRef::select(const char *text)
78 {
79         highlight(text);
80         goto_ref();
81 }
82
83 void FormRef::highlight(const char *text)
84 {
85         if (gotowhere==GOTOBACK)
86                 goto_ref();
87
88         dialog_->gotoPB->setEnabled(true);
89         if (!readonly) {
90                 dialog_->typeCO->setEnabled(true);
91                 dialog_->referenceED->setText(text);
92                 dialog_->okPB->setEnabled(true);
93         }
94 }
95
96 void FormRef::set_sort(bool on)
97 {
98         if (on!=sort) {
99                 sort=on;
100                 dialog_->refsLB->clear();
101                 updateRefs();
102         }
103 }
104
105 void FormRef::goto_ref()
106 {
107         switch (gotowhere) {
108                 case GOTOREF:
109                         lv_->getLyXFunc()->Dispatch(LFUN_REF_GOTO, dialog_->referenceED->text().latin1());
110                         gotowhere=GOTOBACK;
111                         dialog_->gotoPB->setText(_("&Go back"));
112                         break;
113                 case GOTOBACK:
114                         lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
115                         gotowhere=GOTOREF;
116                         dialog_->gotoPB->setText(_("&Goto reference"));
117                         break;
118                 }
119 }
120
121 void FormRef::updateRefs()
122 {
123         // list will be re-done, should go back if necessary
124         if (gotowhere==GOTOBACK) {
125                 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
126                 gotowhere = GOTOREF;
127                 dialog_->gotoPB->setText(_("&Goto reference"));
128         }
129
130         dialog_->refsLB->setAutoUpdate(false);
131
132         // need this because Qt will send a highlight() here for
133         // the first item inserted
134         string tmp(dialog_->referenceED->text());
135
136         for (vector< string >::const_iterator iter = refs.begin();
137                 iter != refs.end(); ++iter) {
138                 if (sort)
139                         dialog_->refsLB->inSort(iter->c_str());
140                 else
141                         dialog_->refsLB->insertItem(iter->c_str());
142         }
143
144         dialog_->referenceED->setText(tmp.c_str());
145
146         for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
147                 if (!strcmp(dialog_->referenceED->text(),dialog_->refsLB->text(i)))
148                         dialog_->refsLB->setCurrentItem(i);
149         }
150
151         dialog_->refsLB->setAutoUpdate(true);
152         dialog_->refsLB->update();
153 }
154
155 void FormRef::do_ref_update()
156 {
157         refs.clear();
158         dialog_->refsLB->clear();
159         refs = lv_->buffer()->getLabelList();
160         if (!refs.empty())
161                 dialog_->sortCB->setEnabled(true);
162         updateRefs();
163 }
164
165 void FormRef::update()
166 {
167         dialog_->referenceED->setText(params.getContents().c_str());
168         dialog_->nameED->setText(params.getOptions().c_str());
169
170         if (params.getCmdName()=="pageref") {
171                 type = PAGEREF;
172                 dialog_->typeCO->setCurrentItem(1);
173         } else if (params.getCmdName()=="vref") {
174                 type = VREF;
175                 dialog_->typeCO->setCurrentItem(2);
176         } else if (params.getCmdName()=="vpageref") {
177                 type = VPAGEREF;
178                 dialog_->typeCO->setCurrentItem(3);
179         } else if (params.getCmdName()=="prettyref") {
180                 type = PRETTYREF;
181                 dialog_->typeCO->setCurrentItem(4);
182         } else {
183                 type = REF;
184                 dialog_->typeCO->setCurrentItem(0);
185         }
186
187         dialog_->gotoPB->setText(_("&Goto reference"));
188
189         gotowhere = GOTOREF;
190
191         dialog_->sortCB->setChecked(sort);
192
193         do_ref_update();
194
195         dialog_->gotoPB->setEnabled(params.getContents()!="");
196         dialog_->okPB->setEnabled(params.getContents()!="");
197
198         dialog_->typeCO->setEnabled(!readonly);
199         dialog_->sortCB->setEnabled(!readonly);
200         dialog_->refsLB->setEnabled(!readonly);
201         dialog_->okPB->setEnabled(!readonly);
202         dialog_->updatePB->setEnabled(!readonly);
203         if (readonly)
204                 dialog_->cancelPB->setText(_("&Close"));
205         else
206                 dialog_->cancelPB->setText(_("&Cancel"));
207 }
208
209 void FormRef::apply()
210 {
211         if (readonly)
212                 return;
213
214         if (!lv_->view()->available())
215                 return;
216
217         switch (dialog_->typeCO->currentItem()) {
218                 case 0:
219                         params.setCmdName("ref");
220                         break;
221                 case 1:
222                         params.setCmdName("pageref");
223                         break;
224                 case 2:
225                         params.setCmdName("vref");
226                         break;
227                 case 3:
228                         params.setCmdName("vpageref");
229                         break;
230                 case 4:
231                         params.setCmdName("prettyref");
232                         break;
233                 default:
234                         lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
235         }
236
237         params.setContents(dialog_->referenceED->text().latin1());
238         params.setOptions(dialog_->nameED->text().latin1());
239
240         if (inset_ != 0) {
241                 if (params != inset_->params()) {
242                         inset_->setParams(params);
243                         lv_->view()->updateInset(inset_, true);
244                 }
245         } else
246                 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
247 }
248
249 void FormRef::show()
250 {
251         if (!dialog_)
252                 dialog_ = new FormRefDialog(this, 0, _("LyX: Cross Reference"), false);
253
254         if (!dialog_->isVisible()) {
255                 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
256                 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
257         }
258
259         dialog_->raise();
260         dialog_->setActiveWindow();
261
262         update();
263         dialog_->show();
264 }
265
266 void FormRef::close()
267 {
268         h_.disconnect();
269         u_.disconnect();
270         ih_.disconnect();
271         inset_ = 0;
272 }
273
274 void FormRef::hide()
275 {
276         dialog_->hide();
277         close();
278 }