4 * John Levon, moz@compsoc.man.ac.uk
7 /***************************************************************************
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. *
14 ***************************************************************************/
24 #include "formrefdialog.h"
29 FormRef::FormRef(LyXView *v, Dialogs *d)
30 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
31 sort(0), gotowhere(GOTOREF), type(REF), refs(0)
33 // let the dialog be shown
34 // This is a permanent connection so we won't bother
35 // storing a copy because we won't be disconnecting.
36 d->showRef.connect(slot(this, &FormRef::showRef));
37 d->createRef.connect(slot(this, &FormRef::createRef));
45 void FormRef::showRef(InsetCommand * const inset)
47 // FIXME: when could inset be 0 here ?
52 readonly = lv_->buffer()->isReadonly();
53 ih_ = inset_->hide.connect(slot(this,&FormRef::hide));
54 params = inset->params();
59 void FormRef::createRef(string const & arg)
64 readonly = lv_->buffer()->isReadonly();
65 params.setFromString(arg);
69 void FormRef::select(const char *text)
75 void FormRef::highlight(const char *text)
77 if (gotowhere==GOTOBACK)
80 dialog_->buttonGoto->setEnabled(true);
82 dialog_->type->setEnabled(true);
83 dialog_->reference->setText(text);
84 dialog_->buttonOk->setEnabled(true);
88 void FormRef::set_sort(bool on)
92 dialog_->refs->clear();
97 void FormRef::goto_ref()
101 lv_->getLyXFunc()->Dispatch(LFUN_REF_GOTO, dialog_->reference->text());
103 dialog_->buttonGoto->setText(_("&Go back"));
106 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
108 dialog_->buttonGoto->setText(_("&Goto reference"));
113 void FormRef::updateRefs()
115 // list will be re-done, should go back if necessary
116 if (gotowhere==GOTOBACK) {
117 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
119 dialog_->buttonGoto->setText(_("&Goto reference"));
122 dialog_->refs->setAutoUpdate(false);
124 // need this because Qt will send a highlight() here for
125 // the first item inserted
126 string tmp(dialog_->reference->text());
128 for (vector< string >::const_iterator iter = refs.begin();
129 iter != refs.end(); ++iter) {
131 dialog_->refs->inSort(iter->c_str());
133 dialog_->refs->insertItem(iter->c_str());
136 dialog_->reference->setText(tmp.c_str());
138 for (unsigned int i = 0; i < dialog_->refs->count(); ++i) {
139 if (!strcmp(dialog_->reference->text(),dialog_->refs->text(i)))
140 dialog_->refs->setCurrentItem(i);
143 dialog_->refs->setAutoUpdate(true);
144 dialog_->refs->update();
147 void FormRef::do_ref_update()
150 dialog_->refs->clear();
151 refs = lv_->buffer()->getLabelList();
153 dialog_->sort->setEnabled(true);
157 void FormRef::update()
159 dialog_->reference->setText(params.getContents().c_str());
160 dialog_->refname->setText(params.getOptions().c_str());
162 if (params.getCmdName()=="pageref") {
164 dialog_->type->setCurrentItem(1);
165 } else if (params.getCmdName()=="vref") {
167 dialog_->type->setCurrentItem(2);
168 } else if (params.getCmdName()=="vpageref") {
170 dialog_->type->setCurrentItem(3);
171 } else if (params.getCmdName()=="prettyref") {
173 dialog_->type->setCurrentItem(4);
176 dialog_->type->setCurrentItem(0);
179 dialog_->buttonGoto->setText(_("&Goto reference"));
183 dialog_->sort->setChecked(sort);
187 dialog_->buttonGoto->setEnabled(params.getContents()!="");
188 dialog_->buttonOk->setEnabled(params.getContents()!="");
190 dialog_->type->setEnabled(!readonly);
191 dialog_->sort->setEnabled(!readonly);
192 dialog_->refs->setEnabled(!readonly);
193 dialog_->buttonOk->setEnabled(!readonly);
194 dialog_->buttonUpdate->setEnabled(!readonly);
196 dialog_->buttonCancel->setText(_("&Close"));
198 dialog_->buttonCancel->setText(_("&Cancel"));
201 void FormRef::apply()
206 if (!lv_->view()->available())
209 switch (dialog_->type->currentItem()) {
211 params.setCmdName("ref");
214 params.setCmdName("pageref");
217 params.setCmdName("vref");
220 params.setCmdName("vpageref");
223 params.setCmdName("prettyref");
226 lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
229 params.setContents(dialog_->reference->text());
230 params.setOptions(dialog_->refname->text());
233 if (params != inset_->params()) {
234 inset_->setParams(params);
235 lv_->view()->updateInset(inset_, true);
238 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
244 dialog_ = new FormRefDialog(this, 0, _("LyX: Cross Reference"), false);
246 if (!dialog_->isVisible()) {
247 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
248 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
252 dialog_->setActiveWindow();
258 void FormRef::close()