/**
* \file QRef.C
- * Copyright 2001 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
* \author John Levon
+ *
+ * Full author contact details are available in file CREDITS
*/
#include <config.h>
-#include "QRefDialog.h"
+#ifdef __GNUG__
+#pragma implementation
+#endif
-#include "QtLyXView.h"
-#include "BufferView.h"
-#include "Dialogs.h"
+#include "ControlRef.h"
#include "QRef.h"
-#include "gettext.h"
-#include "buffer.h"
-#include "lyxfunc.h"
-#include "debug.h"
+#include "QRefDialog.h"
+#include "Qt2BC.h"
-#include <qpushbutton.h>
#include <qlineedit.h>
+#include <qcheckbox.h>
#include <qlistbox.h>
#include <qcombobox.h>
-#include <qcheckbox.h>
+#include <qpushbutton.h>
+#include <qtooltip.h>
+
+#include "helper_funcs.h" // getStringFromVector
+#include "support/lstrings.h" // frontStrip, strip
+#include "gettext.h"
+#include "insets/insetref.h"
+using std::find;
+using std::max;
+using std::sort;
+using std::vector;
using std::endl;
-QRef::QRef(LyXView *v, Dialogs *d)
- : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
- sort(0), gotowhere(GOTOREF), type(REF), refs(0)
+
+typedef Qt2CB<ControlRef, Qt2DB<QRefDialog> > base_class;
+
+
+QRef::QRef()
+ : base_class(_("Cross Reference")),
+ sort_(false), at_ref_(false)
{
- // let the dialog be shown
- // This is a permanent connection so we won't bother
- // storing a copy because we won't be disconnecting.
- d->showRef.connect(slot(this, &QRef::showRef));
- d->createRef.connect(slot(this, &QRef::createRef));
}
-QRef::~QRef()
+void QRef::build_dialog()
{
- delete dialog_;
+ dialog_.reset(new QRefDialog(this));
+
+ bc().setOK(dialog_->okPB);
+ bc().setCancel(dialog_->closePB);
+ bc().addReadOnly(dialog_->refsLB);
+ bc().addReadOnly(dialog_->sortCB);
+ bc().addReadOnly(dialog_->nameED);
+ bc().addReadOnly(dialog_->referenceED);
+ bc().addReadOnly(dialog_->typeCO);
+ bc().addReadOnly(dialog_->bufferCO);
}
-void QRef::showRef(InsetCommand * const inset)
+void QRef::update_contents()
{
- // FIXME: when could inset be 0 here ?
- if (inset==0)
- return;
-
- inset_ = inset;
- readonly = lv_->buffer()->isReadonly();
- //ih_ = inset_->hide.connect(slot(this,&QRef::hide));
- params = inset->params();
-
- show();
+ InsetCommandParams const & params = controller().params();
+
+ dialog_->referenceED->setText(params.getContents().c_str());
+
+ dialog_->nameED->setText(params.getOptions().c_str());
+ dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
+
+ dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
+ dialog_->typeCO->setEnabled(!typeAllowed() && !readOnly());
+ if (!typeAllowed())
+ dialog_->typeCO->setCurrentItem(0);
+
+ dialog_->sortCB->setChecked(sort_);
+
+ // insert buffer list
+ dialog_->bufferCO->clear();
+ vector<string> const buffers = controller().getBufferList();
+ for (vector<string>::const_iterator it = buffers.begin();
+ it != buffers.end(); ++it) {
+ dialog_->bufferCO->insertItem(it->c_str());
+ }
+ dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
+
+ updateRefs();
}
-void QRef::createRef(string const & arg)
+void QRef::apply()
{
- if (inset_)
- close();
+ InsetCommandParams & params = controller().params();
- readonly = lv_->buffer()->isReadonly();
- params.setFromString(arg);
- show();
+ params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
+ params.setContents(dialog_->referenceED->text().latin1());
+ params.setOptions(dialog_->nameED->text().latin1());
}
-void QRef::select(const char *text)
+bool QRef::nameAllowed()
{
- highlight(text);
- goto_ref();
+ return controller().docType() != ControlRef::LATEX &&
+ controller().docType() != ControlRef::LITERATE;
}
-
-void QRef::highlight(const char *text)
+
+bool QRef::typeAllowed()
{
- if (gotowhere==GOTOBACK)
- goto_ref();
-
- dialog_->gotoPB->setEnabled(true);
- if (!readonly) {
- dialog_->typeCO->setEnabled(true);
- dialog_->referenceED->setText(text);
- dialog_->okPB->setEnabled(true);
- }
+ return controller().docType() == ControlRef::LINUXDOC ||
+ controller().docType() == ControlRef::DOCBOOK;
}
-
-void QRef::set_sort(bool on)
+
+void QRef::setGoBack()
{
- if (on!=sort) {
- sort=on;
- dialog_->refsLB->clear();
- updateRefs();
- }
+ dialog_->gotoPB->setText(_("&Go back"));
+ QToolTip::remove(dialog_->gotoPB);
+ QToolTip::add(dialog_->gotoPB, _("Go back"));
}
-
-void QRef::goto_ref()
+
+void QRef::setGotoRef()
{
- switch (gotowhere) {
- case GOTOREF:
- lv_->getLyXFunc()->dispatch(LFUN_REF_GOTO, dialog_->referenceED->text().latin1());
- gotowhere=GOTOBACK;
- dialog_->gotoPB->setText(_("&Go back"));
- break;
- case GOTOBACK:
- //FIXME lv_->getLyXFunc()->dispatch(LFUN_REF_BACK);
- gotowhere=GOTOREF;
- dialog_->gotoPB->setText(_("&Goto reference"));
- break;
- }
+ dialog_->gotoPB->setText(_("&Goto"));
+ QToolTip::remove(dialog_->gotoPB);
+ QToolTip::add(dialog_->gotoPB, _("Go to reference"));
}
-
-void QRef::updateRefs()
+
+void QRef::gotoRef()
{
- // list will be re-done, should go back if necessary
- if (gotowhere==GOTOBACK) {
- //FIXME lv_->getLyXFunc()->dispatch(LFUN_REF_BACK);
- gotowhere = GOTOREF;
- dialog_->gotoPB->setText(_("&Goto reference"));
+ string ref(dialog_->referenceED->text().latin1());
+
+ if (at_ref_) {
+ // go back
+ setGotoRef();
+ controller().gotoBookmark();
+ } else {
+ // go to the ref
+ setGoBack();
+ controller().gotoRef(ref);
}
+ at_ref_ = !at_ref_;
+}
+
+void QRef::redoRefs()
+{
dialog_->refsLB->setAutoUpdate(false);
// need this because Qt will send a highlight() here for
// the first item inserted
- string tmp(dialog_->referenceED->text());
+ string const tmp(dialog_->referenceED->text().latin1());
- for (std::vector< string >::const_iterator iter = refs.begin();
- iter != refs.end(); ++iter) {
- if (sort)
+ for (std::vector<string>::const_iterator iter = refs_.begin();
+ iter != refs_.end(); ++iter) {
+ if (sort_)
dialog_->refsLB->inSort(iter->c_str());
else
dialog_->refsLB->insertItem(iter->c_str());
dialog_->referenceED->setText(tmp.c_str());
for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
- if (!strcmp(dialog_->referenceED->text(),dialog_->refsLB->text(i)))
+ if (!compare(tmp.c_str(), dialog_->refsLB->text(i).latin1()))
dialog_->refsLB->setCurrentItem(i);
}
dialog_->refsLB->update();
}
-
-void QRef::do_ref_update()
-{
- refs.clear();
- dialog_->refsLB->clear();
- refs = lv_->buffer()->getLabelList();
- if (!refs.empty())
- dialog_->sortCB->setEnabled(true);
- updateRefs();
-}
-
-
-void QRef::update()
-{
- // FIXME: needs updating
-
- dialog_->referenceED->setText(params.getContents().c_str());
- dialog_->nameED->setText(params.getOptions().c_str());
-
- if (params.getCmdName()=="pageref") {
- type = PAGEREF;
- dialog_->typeCO->setCurrentItem(1);
- } else if (params.getCmdName()=="vref") {
- type = VREF;
- dialog_->typeCO->setCurrentItem(2);
- } else if (params.getCmdName()=="vpageref") {
- type = VPAGEREF;
- dialog_->typeCO->setCurrentItem(3);
- } else if (params.getCmdName()=="prettyref") {
- type = PRETTYREF;
- dialog_->typeCO->setCurrentItem(4);
- } else {
- type = REF;
- dialog_->typeCO->setCurrentItem(0);
- }
-
- dialog_->gotoPB->setText(_("&Goto reference"));
-
- gotowhere = GOTOREF;
-
- dialog_->sortCB->setChecked(sort);
-
- do_ref_update();
- dialog_->gotoPB->setEnabled(params.getContents()!="");
- dialog_->okPB->setEnabled(params.getContents()!="");
-
- dialog_->typeCO->setEnabled(!readonly);
- dialog_->sortCB->setEnabled(!readonly);
- dialog_->refsLB->setEnabled(!readonly);
- dialog_->okPB->setEnabled(!readonly);
- dialog_->updatePB->setEnabled(!readonly);
- if (readonly)
- dialog_->cancelPB->setText(_("&Close"));
- else
- dialog_->cancelPB->setText(_("&Cancel"));
-}
-
-
-void QRef::apply()
-{
- if (readonly)
- return;
-
- if (!lv_->view()->available())
- return;
-
- switch (dialog_->typeCO->currentItem()) {
- case 0:
- params.setCmdName("ref");
- break;
- case 1:
- params.setCmdName("pageref");
- break;
- case 2:
- params.setCmdName("vref");
- break;
- case 3:
- params.setCmdName("vpageref");
- break;
- case 4:
- params.setCmdName("prettyref");
- break;
- default:
- lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
- }
-
- params.setContents(dialog_->referenceED->text().latin1());
- params.setOptions(dialog_->nameED->text().latin1());
-
- if (inset_ != 0) {
- if (params != inset_->params()) {
- inset_->setParams(params);
- lv_->view()->updateInset(inset_, true);
- }
- } else
- lv_->getLyXFunc()->dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
-}
-
-
-void QRef::show()
-{
- if (!dialog_)
- dialog_ = new QRefDialog(this, 0, _("LyX: Cross Reference"), false);
-
- if (!dialog_->isVisible()) {
- h_ = d_->hideBufferDependent.connect(slot(this, &QRef::hide));
- //u_ = d_->updateBufferDependent.connect(slot(this, &QRef::update));
- }
-
- dialog_->raise();
- dialog_->setActiveWindow();
-
- update();
- dialog_->show();
-}
-
-
-void QRef::close()
-{
- h_.disconnect();
- u_.disconnect();
- ih_.disconnect();
- inset_ = 0;
-}
-
-
-void QRef::hide()
+void QRef::updateRefs()
{
- dialog_->hide();
- close();
+ refs_.clear();
+ if (at_ref_)
+ gotoRef();
+ dialog_->refsLB->clear();
+ string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
+ refs_ = controller().getLabelList(name);
+ dialog_->sortCB->setEnabled(!refs_.empty());
+ dialog_->refsLB->setEnabled(!refs_.empty());
+ redoRefs();
}