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"
27 FormRef::FormRef(LyXView *v, Dialogs *d)
28 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
29 sort(0), gotowhere(GOTOREF), type(REF), refs(0)
31 // let the dialog be shown
32 // This is a permanent connection so we won't bother
33 // storing a copy because we won't be disconnecting.
34 d->showRef.connect(slot(this, &FormRef::showRef));
35 d->createRef.connect(slot(this, &FormRef::createRef));
43 void FormRef::showRef(InsetCommand * const inset)
45 // FIXME: when could inset be 0 here ?
50 readonly = lv_->buffer()->isReadonly();
51 ih_ = inset_->hide.connect(slot(this,&FormRef::hide));
52 params = inset->params();
57 void FormRef::createRef(string const & arg)
62 readonly = lv_->buffer()->isReadonly();
63 params.setFromString(arg);
67 void FormRef::select(const char *text)
73 void FormRef::highlight(const char *text)
75 if (gotowhere==GOTOBACK)
78 dialog_->buttonGoto->setEnabled(true);
80 dialog_->type->setEnabled(true);
81 dialog_->reference->setText(text);
82 dialog_->buttonOk->setEnabled(true);
86 void FormRef::set_sort(bool on)
90 dialog_->refs->clear();
95 void FormRef::goto_ref()
99 lv_->getLyXFunc()->Dispatch(LFUN_REF_GOTO, dialog_->reference->text());
101 dialog_->buttonGoto->setText(_("&Go back"));
104 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
106 dialog_->buttonGoto->setText(_("&Goto reference"));
111 void FormRef::updateRefs()
113 // list will be re-done, should go back if necessary
114 if (gotowhere==GOTOBACK) {
115 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
117 dialog_->buttonGoto->setText(_("&Goto reference"));
120 dialog_->refs->setAutoUpdate(false);
122 // need this because Qt will send a highlight() here for
123 // the first item inserted
124 string tmp(dialog_->reference->text());
126 for (vector< string >::const_iterator iter = refs.begin();
127 iter != refs.end(); ++iter) {
129 dialog_->refs->inSort(iter->c_str());
131 dialog_->refs->insertItem(iter->c_str());
134 dialog_->reference->setText(tmp.c_str());
136 for (unsigned int i = 0; i < dialog_->refs->count(); ++i) {
137 if (!strcmp(dialog_->reference->text(),dialog_->refs->text(i)))
138 dialog_->refs->setCurrentItem(i);
141 dialog_->refs->setAutoUpdate(true);
142 dialog_->refs->update();
145 void FormRef::do_ref_update()
148 dialog_->refs->clear();
149 refs = lv_->buffer()->getLabelList();
151 dialog_->sort->setEnabled(true);
155 void FormRef::update()
157 dialog_->reference->setText(params.getContents().c_str());
158 dialog_->refname->setText(params.getOptions().c_str());
160 if (params.getCmdName()=="pageref") {
162 dialog_->type->setCurrentItem(1);
163 } else if (params.getCmdName()=="vref") {
165 dialog_->type->setCurrentItem(2);
166 } else if (params.getCmdName()=="vpageref") {
168 dialog_->type->setCurrentItem(3);
169 } else if (params.getCmdName()=="prettyref") {
171 dialog_->type->setCurrentItem(4);
174 dialog_->type->setCurrentItem(0);
177 dialog_->buttonGoto->setText(_("&Goto reference"));
181 dialog_->sort->setChecked(sort);
185 dialog_->buttonGoto->setEnabled(params.getContents()!="");
186 dialog_->buttonOk->setEnabled(params.getContents()!="");
188 dialog_->type->setEnabled(!readonly);
189 dialog_->sort->setEnabled(!readonly);
190 dialog_->refs->setEnabled(!readonly);
191 dialog_->buttonOk->setEnabled(!readonly);
192 dialog_->buttonUpdate->setEnabled(!readonly);
194 dialog_->buttonCancel->setText(_("&Close"));
196 dialog_->buttonCancel->setText(_("&Cancel"));
199 void FormRef::apply()
204 if (!lv_->view()->available())
207 switch (dialog_->type->currentItem()) {
209 params.setCmdName("ref");
212 params.setCmdName("pageref");
215 params.setCmdName("vref");
218 params.setCmdName("vpageref");
221 params.setCmdName("prettyref");
224 lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
227 params.setContents(dialog_->reference->text());
228 params.setOptions(dialog_->refname->text());
231 if (params != inset_->params()) {
232 inset_->setParams(params);
233 lv_->view()->updateInset(inset_, true);
236 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
242 dialog_ = new FormRefDialog(this, 0, _("LyX: Cross Reference"), false);
244 if (!dialog_->isVisible()) {
245 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
246 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
250 dialog_->setActiveWindow();
256 void FormRef::close()