]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiRef.cpp
Remove debugging code.
[lyx.git] / src / frontends / qt4 / GuiRef.cpp
index b268a3f478c71d1573bf5893a0609bc0bb3fa428..5d46a471c710bb980c0c062085c8f3a480d405b5 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author John Levon
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 #include <config.h>
 
 #include "GuiRef.h"
-#include "Qt2BC.h"
-#include "qt_helpers.h"
 
-#include "ButtonController.h"
+#include "GuiApplication.h"
+
+#include "Buffer.h"
+#include "BufferParams.h"
+#include "BufferList.h"
+#include "BufferView.h"
+#include "Cursor.h"
+#include "FuncRequest.h"
+
+#include "qt_helpers.h"
 
 #include "insets/InsetRef.h"
 
+#include "support/FileName.h"
+#include "support/FileNameList.h"
+#include "support/filetools.h" // makeAbsPath, makeDisplayPath
+
 #include <QLineEdit>
 #include <QCheckBox>
-#include <QListWidget>
-#include <QListWidgetItem>
+#include <QTreeWidget>
+#include <QTreeWidgetItem>
 #include <QPushButton>
 #include <QToolTip>
 #include <QCloseEvent>
+#include <QHeaderView>
 
-
-using std::vector;
-using std::string;
-
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
-/////////////////////////////////////////////////////////////////////
-//
-// GuiRefDialog
-//
-/////////////////////////////////////////////////////////////////////
-
-GuiRefDialog::GuiRefDialog(GuiRef * form)
-       : form_(form)
+GuiRef::GuiRef(GuiView & lv)
+       : GuiDialog(lv, "ref", qt_("Cross-reference")),
+         params_(insetCode("ref"))
 {
        setupUi(this);
 
-       connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
-       connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
-       connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
-       connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
-       connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
+       at_ref_ = false;
+
+       // The filter bar
+       filter_ = new FancyLineEdit(this);
+       filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
+       filter_->setButtonVisible(FancyLineEdit::Right, true);
+       filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
+       filter_->setAutoHideButton(FancyLineEdit::Right, true);
+       filter_->setPlaceholderText(qt_("All available labels"));
+       filter_->setToolTip(qt_("Enter string to filter the list of available labels"));
+#if (QT_VERSION < 0x050000)
+       connect(filter_, SIGNAL(downPressed()),
+               refsTW, SLOT(setFocus()));
+#else
+       connect(filter_, &FancyLineEdit::downPressed,
+               refsTW, [=](){ focusAndHighlight(refsTW); });
+#endif
+
+       filterBarL->addWidget(filter_, 0);
+       findKeysLA->setBuddy(filter_);
+
+       sortingCO->addItem(qt_("By Occurrence"), "unsorted");
+       sortingCO->addItem(qt_("Alphabetically (Case-Insensitive)"), "nocase");
+       sortingCO->addItem(qt_("Alphabetically (Case-Sensitive)"), "case");
+
+       buttonBox->button(QDialogButtonBox::Reset)->setText(qt_("&Update"));
+       buttonBox->button(QDialogButtonBox::Reset)->setToolTip(qt_("Update the label list"));
+
+       refsTW->setColumnCount(1);
+       refsTW->header()->setVisible(false);
+
+       connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
 
        connect(typeCO, SIGNAL(activated(int)),
                this, SLOT(changed_adaptor()));
-       connect(referenceED, SIGNAL(textChanged(const QString &)),
+       connect(referenceED, SIGNAL(textChanged(QString)),
+               this, SLOT(refTextChanged(QString)));
+       connect(referenceED, SIGNAL(textChanged(QString)),
                this, SLOT(changed_adaptor()));
-       connect(nameED, SIGNAL(textChanged(const QString &)),
+       connect(filter_, SIGNAL(textEdited(QString)),
+               this, SLOT(filterLabels()));
+       connect(filter_, SIGNAL(rightButtonClicked()),
+               this, SLOT(resetFilter()));
+       connect(csFindCB, SIGNAL(clicked()),
+               this, SLOT(filterLabels()));
+       connect(nameED, SIGNAL(textChanged(QString)),
                this, SLOT(changed_adaptor()));
-       connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
-               this, SLOT(refHighlighted(QListWidgetItem *)));
-       connect(refsLW, SIGNAL(itemSelectionChanged()),
+       connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
+               this, SLOT(refHighlighted(QTreeWidgetItem *)));
+       connect(refsTW, SIGNAL(itemSelectionChanged()),
                this, SLOT(selectionChanged()));
-       connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
-               this, SLOT(refSelected(QListWidgetItem *)));
-       connect(sortCB, SIGNAL(clicked(bool)),
-               this, SLOT(sortToggled(bool)));
+       connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
+               this, SLOT(refSelected(QTreeWidgetItem *)));
+       connect(sortingCO, SIGNAL(activated(int)),
+               this, SLOT(sortToggled()));
+       connect(groupCB, SIGNAL(clicked()),
+               this, SLOT(groupToggled()));
        connect(gotoPB, SIGNAL(clicked()),
                this, SLOT(gotoClicked()));
-       connect(updatePB, SIGNAL(clicked()),
-               this, SLOT(updateClicked()));
        connect(bufferCO, SIGNAL(activated(int)),
                this, SLOT(updateClicked()));
+       connect(pluralCB, SIGNAL(clicked()),
+               this, SLOT(changed_adaptor()));
+       connect(capsCB, SIGNAL(clicked()),
+               this, SLOT(changed_adaptor()));
+       connect(noprefixCB, SIGNAL(clicked()),
+               this, SLOT(changed_adaptor()));
+
+       enableBoxes();
+
+       bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
+       bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
+       bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
+       bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
+       bc().addReadOnly(typeCO);
 
-       setFocusProxy(refsLW);
+       restored_buffer_ = -1;
+       active_buffer_ = -1;
+
+       setFocusProxy(filter_);
+}
+
+
+void GuiRef::enableView(bool enable)
+{
+       if (!enable)
+               // In the opposite case, updateContents() will be called anyway.
+               updateContents();
+       GuiDialog::enableView(enable);
 }
 
-void GuiRefDialog::show()
+
+void GuiRef::enableBoxes()
 {
-       QDialog::show();
+       bool const isFormatted =
+           (InsetRef::getName(typeCO->currentIndex()) == "formatted");
+       bool const isLabelOnly =
+           (InsetRef::getName(typeCO->currentIndex()) == "labelonly");
+       bool const usingRefStyle = buffer().params().use_refstyle;
+       pluralCB->setEnabled(isFormatted && usingRefStyle);
+       capsCB->setEnabled(isFormatted && usingRefStyle);
+       noprefixCB->setEnabled(isLabelOnly);
 }
 
 
-void GuiRefDialog::changed_adaptor()
+void GuiRef::changed_adaptor()
 {
-       form_->changed();
+       changed();
+       enableBoxes();
 }
 
 
-void GuiRefDialog::gotoClicked()
+void GuiRef::gotoClicked()
 {
-       form_->gotoRef();
+       // By setting last_reference_, we ensure that the reference
+       // to which we are going (or from which we are returning) is
+       // restored in the dialog. It's a bit of a hack, but it works,
+       // and no-one seems to have any better idea.
+       bool const toggled =
+               last_reference_.isEmpty() || last_reference_.isNull();
+       if (toggled)
+               last_reference_ = referenceED->text();
+       gotoRef();
+       if (toggled)
+               last_reference_.clear();
 }
 
-void GuiRefDialog::selectionChanged()
+
+void GuiRef::selectionChanged()
 {
-       if (form_->readOnly())
+       if (isBufferReadonly())
                return;
 
-       QList<QListWidgetItem *> selections = refsLW->selectedItems();
+       QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
        if (selections.isEmpty())
                return;
-       QListWidgetItem * sel = selections.first();
+       QTreeWidgetItem * sel = selections.first();
        refHighlighted(sel);
        return;
 }
 
 
-void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
+void GuiRef::refHighlighted(QTreeWidgetItem * sel)
 {
-       if (form_->readOnly())
+       if (sel->childCount() > 0) {
+               sel->setExpanded(true);
                return;
+       }
 
-/*     int const cur_item = refsLW->currentRow();
+/*     int const cur_item = refsTW->currentRow();
        bool const cur_item_selected = cur_item >= 0 ?
                refsLB->isSelected(cur_item) : false;*/
-       bool const cur_item_selected = refsLW->isItemSelected(sel);
+       bool const cur_item_selected = refsTW->isItemSelected(sel);
 
        if (cur_item_selected)
-               referenceED->setText(sel->text());
+               referenceED->setText(sel->text(0));
 
-       if (form_->at_ref_)
-               form_->gotoRef();
+       if (at_ref_)
+               gotoRef();
        gotoPB->setEnabled(true);
-       if (form_->typeAllowed())
+       if (typeAllowed() && !isBufferReadonly())
                typeCO->setEnabled(true);
-       if (form_->nameAllowed())
-               nameED->setEnabled(true);
+       nameED->setHidden(!nameAllowed());
+       nameL->setHidden(!nameAllowed());
+}
+
+
+void GuiRef::refTextChanged(QString const & str)
+{
+       gotoPB->setEnabled(!str.isEmpty());
+       typeCO->setEnabled(!str.isEmpty());
+       typeLA->setEnabled(!str.isEmpty());
 }
 
 
-void GuiRefDialog::refSelected(QListWidgetItem * sel)
+void GuiRef::refSelected(QTreeWidgetItem * sel)
 {
-       if (form_->readOnly())
+       if (isBufferReadonly())
                return;
 
-/*     int const cur_item = refsLW->currentRow();
+       if (sel->childCount()) {
+               sel->setExpanded(false);
+               return;
+       }
+
+/*     int const cur_item = refsTW->currentRow();
        bool const cur_item_selected = cur_item >= 0 ?
                refsLB->isSelected(cur_item) : false;*/
-       bool const cur_item_selected = refsLW->isItemSelected(sel);
+       bool const cur_item_selected = refsTW->isItemSelected(sel);
 
        if (cur_item_selected)
-               referenceED->setText(sel->text());
+               referenceED->setText(sel->text(0));
        // <enter> or double click, inserts ref and closes dialog
-       form_->slotOK();
+       slotOK();
 }
 
 
-void GuiRefDialog::sortToggled(bool on)
+void GuiRef::sortToggled()
 {
-       form_->sort_ = on;
-       form_->redoRefs();
+       redoRefs();
 }
 
 
-void GuiRefDialog::updateClicked()
+void GuiRef::groupToggled()
 {
-       form_->updateRefs();
+       redoRefs();
 }
 
 
-void GuiRefDialog::reset_dialog() {
-       form_->at_ref_ = false;
-       form_->setGotoRef();
+void GuiRef::on_buttonBox_clicked(QAbstractButton * button)
+{
+       switch (buttonBox->standardButton(button)) {
+       case QDialogButtonBox::Ok:
+               slotOK();
+               break;
+       case QDialogButtonBox::Apply:
+               slotApply();
+               break;
+       case QDialogButtonBox::Cancel:
+               slotClose();
+               resetDialog();
+               break;
+       case QDialogButtonBox::Reset:
+               updateClicked();
+               break;
+       default:
+               break;
+       }
 }
 
 
-void GuiRefDialog::closeEvent(QCloseEvent * e)
+void GuiRef::updateClicked()
 {
-       form_->slotWMHide();
-       reset_dialog();
-       e->accept();
+       updateRefs();
 }
 
 
-/////////////////////////////////////////////////////////////////////
-//
-// GuiRef
-//
-/////////////////////////////////////////////////////////////////////
-
-
-GuiRef::GuiRef(Dialog & parent)
-       : GuiView<GuiRefDialog>(parent, _("Cross-reference")),
-       sort_(false), at_ref_(false)
+void GuiRef::dialogRejected()
 {
+       resetDialog();
+       // We have to do this manually, instead of calling slotClose(), because
+       // the dialog has already been made invisible before rejected() triggers.
+       Dialog::disconnect();
 }
 
 
-void GuiRef::build_dialog()
+void GuiRef::resetDialog()
 {
-       dialog_.reset(new GuiRefDialog(this));
-
-       bcview().setOK(dialog_->okPB);
-       bcview().setApply(dialog_->applyPB);
-       bcview().setCancel(dialog_->closePB);
-       bcview().addReadOnly(dialog_->refsLW);
-       bcview().addReadOnly(dialog_->sortCB);
-       bcview().addReadOnly(dialog_->nameED);
-       bcview().addReadOnly(dialog_->referenceED);
-       bcview().addReadOnly(dialog_->typeCO);
-       bcview().addReadOnly(dialog_->bufferCO);
-
-       restored_buffer_ = -1;
+       at_ref_ = false;
+       setGotoRef();
 }
 
 
-void GuiRef::update_contents()
+void GuiRef::closeEvent(QCloseEvent * e)
 {
-       InsetCommandParams const & params = controller().params();
-
-       int orig_type = dialog_->typeCO->currentIndex();
+       slotClose();
+       resetDialog();
+       e->accept();
+}
 
-       dialog_->referenceED->setText(toqstr(params["reference"]));
 
-       dialog_->nameED->setText(toqstr(params["name"]));
-       dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
+void GuiRef::updateContents()
+{
+       QString const orig_type =
+               typeCO->itemData(typeCO->currentIndex()).toString();
+
+       referenceED->clear();
+       nameED->clear();
+       typeCO->clear();
+
+       // FIXME Bring InsetMathRef on par with InsetRef
+       // (see #9798)
+       typeCO->addItem(qt_("<reference>"), "ref");
+       typeCO->addItem(qt_("(<reference>)"), "eqref");
+       typeCO->addItem(qt_("<page>"), "pageref");
+       typeCO->addItem(qt_("on page <page>"), "vpageref");
+       typeCO->addItem(qt_("<reference> on page <page>"), "vref");
+       typeCO->addItem(qt_("Textual reference"), "nameref");
+       if (bufferview()->cursor().inTexted()) {
+               typeCO->addItem(qt_("Formatted reference"), "formatted");
+               typeCO->addItem(qt_("Label only"), "labelonly");
+       } else
+               typeCO->addItem(qt_("Formatted reference"), "prettyref");
+
+       referenceED->setText(toqstr(params_["reference"]));
+       nameED->setText(toqstr(params_["name"]));
+       nameED->setHidden(!nameAllowed());
+       nameL->setHidden(!nameAllowed());
 
        // restore type settings for new insets
-       if (params["reference"].empty())
-               dialog_->typeCO->setCurrentIndex(orig_type);
+       bool const new_inset = params_["reference"].empty();
+       if (new_inset) {
+               int index = typeCO->findData(orig_type);
+               if (index == -1)
+                       index = 0;
+               typeCO->setCurrentIndex(index);
+       }
        else
-               dialog_->typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
-       dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
+               typeCO->setCurrentIndex(
+                       typeCO->findData(toqstr(params_.getCmdName())));
+       typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
        if (!typeAllowed())
-               dialog_->typeCO->setCurrentIndex(0);
+               typeCO->setCurrentIndex(0);
 
-       dialog_->sortCB->setChecked(sort_);
+       pluralCB->setChecked(params_["plural"] == "true");
+       capsCB->setChecked(params_["caps"] == "true");
+       noprefixCB->setChecked(params_["noprefix"] == "true");
 
        // 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->addItem(toqstr(*it));
+       bufferCO->clear();
+       FileNameList const buffers(theBufferList().fileNames());
+       for (FileNameList::const_iterator it = buffers.begin();
+            it != buffers.end(); ++it) {
+               bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
        }
+
+       int const thebuffer = theBufferList().bufferNum(buffer().fileName());
        // restore the buffer combo setting for new insets
-       if (params["reference"].empty() && restored_buffer_ != -1
-       && restored_buffer_ < dialog_->bufferCO->count())
-               dialog_->bufferCO->setCurrentIndex(restored_buffer_);
-       else
-               dialog_->bufferCO->setCurrentIndex(controller().getBufferNum());
+       if (new_inset && restored_buffer_ != -1
+           && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
+               bufferCO->setCurrentIndex(restored_buffer_);
+       else {
+               int const num = theBufferList().bufferNum(buffer().fileName());
+               bufferCO->setCurrentIndex(num);
+               if (thebuffer != active_buffer_)
+                       restored_buffer_ = num;
+       }
+       active_buffer_ = thebuffer;
 
        updateRefs();
-       bc().valid(false);
+       enableBoxes();
+       // Activate OK/Apply buttons if the users inserts a new ref
+       // and we have a valid pre-setting.
+       bc().setValid(isValid() && new_inset);
 }
 
 
-void GuiRef::apply()
+void GuiRef::applyView()
 {
-       InsetCommandParams & params = controller().params();
-
-       last_reference_ = dialog_->referenceED->text();
-
-       params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
-       params["reference"] = qstring_to_ucs4(last_reference_);
-       params["name"] = qstring_to_ucs4(dialog_->nameED->text());
-
-       restored_buffer_ = dialog_->bufferCO->currentIndex();
+       last_reference_ = referenceED->text();
+
+       params_.setCmdName(fromqstr(typeCO->itemData(typeCO->currentIndex()).toString()));
+       params_["reference"] = qstring_to_ucs4(last_reference_);
+       params_["name"] = qstring_to_ucs4(nameED->text());
+       params_["plural"] = pluralCB->isChecked() ?
+             from_ascii("true") : from_ascii("false");
+       params_["caps"] = capsCB->isChecked() ?
+             from_ascii("true") : from_ascii("false");
+       params_["noprefix"] = noprefixCB->isChecked() ?
+             from_ascii("true") : from_ascii("false");
+       restored_buffer_ = bufferCO->currentIndex();
 }
 
 
 bool GuiRef::nameAllowed()
 {
-       Kernel::DocType const doc_type = kernel().docType();
-       return doc_type != Kernel::LATEX &&
-               doc_type != Kernel::LITERATE;
+       KernelDocType const doc_type = docType();
+       return doc_type != LATEX && doc_type != LITERATE;
 }
 
 
 bool GuiRef::typeAllowed()
 {
-       Kernel::DocType const doc_type = kernel().docType();
-       return doc_type != Kernel::DOCBOOK;
+       return docType() != DOCBOOK;
 }
 
 
 void GuiRef::setGoBack()
 {
-       dialog_->gotoPB->setText(qt_("&Go Back"));
-       dialog_->gotoPB->setToolTip("");
-       dialog_->gotoPB->setToolTip(qt_("Jump back"));
+       gotoPB->setText(qt_("&Go Back"));
+       gotoPB->setToolTip(qt_("Jump back to the original cursor location"));
 }
 
 
 void GuiRef::setGotoRef()
 {
-       dialog_->gotoPB->setText(qt_("&Go to Label"));
-       dialog_->gotoPB->setToolTip("");
-       dialog_->gotoPB->setToolTip(qt_("Jump to label"));
+       gotoPB->setText(qt_("&Go to Label"));
+       gotoPB->setToolTip(qt_("Jump to the selected label"));
 }
 
 
 void GuiRef::gotoRef()
 {
-       string ref(fromqstr(dialog_->referenceED->text()));
+       string ref = fromqstr(referenceED->text());
 
        if (at_ref_) {
                // go back
                setGotoRef();
-               controller().gotoBookmark();
+               gotoBookmark();
        } else {
                // go to the ref
                setGoBack();
-               controller().gotoRef(ref);
+               gotoRef(ref);
        }
        at_ref_ = !at_ref_;
 }
 
+inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
+{
+       return s1.toLower() < s2.toLower();
+}
+
 
 void GuiRef::redoRefs()
 {
        // Prevent these widgets from emitting any signals whilst
        // we modify their state.
-       dialog_->refsLW->blockSignals(true);
-       dialog_->referenceED->blockSignals(true);
-       dialog_->refsLW->setUpdatesEnabled(false);
+       refsTW->blockSignals(true);
+       referenceED->blockSignals(true);
+       refsTW->setUpdatesEnabled(false);
 
-       dialog_->refsLW->clear();
+       refsTW->clear();
 
        // need this because Qt will send a highlight() here for
        // the first item inserted
-       QString const oldSelection(dialog_->referenceED->text());
-
-       for (std::vector<docstring>::const_iterator iter = refs_.begin();
-               iter != refs_.end(); ++iter) {
-               dialog_->refsLW->addItem(toqstr(*iter));
+       QString const oldSelection(referenceED->text());
+
+       QStringList refsStrings;
+       QStringList refsCategories;
+       vector<docstring>::const_iterator iter;
+       bool noprefix = false;
+       for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
+               QString const lab = toqstr(*iter);
+               refsStrings.append(lab);
+               if (groupCB->isChecked()) {
+                       if (lab.contains(":")) {
+                               QString const pref = lab.split(':')[0];
+                               if (!refsCategories.contains(pref)) {
+                                       if (!pref.isEmpty())
+                                               refsCategories.append(pref);
+                                       else
+                                               noprefix = true;
+                               }
+                       }
+                       else
+                               noprefix = true;
+               }
+       }
+       // sort categories case-intensively
+       qSort(refsCategories.begin(), refsCategories.end(),
+             caseInsensitiveLessThan /*defined above*/);
+       if (noprefix)
+               refsCategories.insert(0, qt_("<No prefix>"));
+
+       QString const sort = sortingCO->isEnabled() ?
+                               sortingCO->itemData(sortingCO->currentIndex()).toString()
+                               : QString();
+       if (sort == "nocase")
+               qSort(refsStrings.begin(), refsStrings.end(),
+                     caseInsensitiveLessThan /*defined above*/);
+       else if (sort == "case")
+               qSort(refsStrings.begin(), refsStrings.end());
+
+       if (groupCB->isChecked()) {
+               QList<QTreeWidgetItem *> refsCats;
+               for (int i = 0; i < refsCategories.size(); ++i) {
+                       QString const cat = refsCategories.at(i);
+                       QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
+                       item->setText(0, cat);
+                       for (int j = 0; j < refsStrings.size(); ++j) {
+                               QString const ref = refsStrings.at(j);
+                               if ((ref.startsWith(cat + QString(":")))
+                                   || (cat == qt_("<No prefix>")
+                                      && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) {
+                                               QTreeWidgetItem * child =
+                                                       new QTreeWidgetItem(item);
+                                               child->setText(0, ref);
+                                               item->addChild(child);
+                               }
+                       }
+                       refsCats.append(item);
+               }
+               refsTW->addTopLevelItems(refsCats);
+       } else {
+               QList<QTreeWidgetItem *> refsItems;
+               for (int i = 0; i < refsStrings.size(); ++i) {
+                       QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
+                       item->setText(0, refsStrings.at(i));
+                       refsItems.append(item);
+               }
+               refsTW->addTopLevelItems(refsItems);
        }
-
-       if (sort_)
-               dialog_->refsLW->sortItems();
-
-       dialog_->referenceED->setText(oldSelection);
 
        // restore the last selection or, for new insets, highlight
        // the previous selection
        if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
                bool const newInset = oldSelection.isEmpty();
                QString textToFind = newInset ? last_reference_ : oldSelection;
-               bool foundItem = false;
-               for (int i = 0; !foundItem && i < dialog_->refsLW->count(); ++i) {
-                       QListWidgetItem * item = dialog_->refsLW->item(i);
-                       if (textToFind == item->text()) {
-                               dialog_->refsLW->setCurrentItem(item);
-                               dialog_->refsLW->setItemSelected(item, !newInset);
+               referenceED->setText(textToFind);
+               last_reference_.clear();
+               QTreeWidgetItemIterator it(refsTW);
+               while (*it) {
+                       if ((*it)->text(0) == textToFind) {
+                               refsTW->setCurrentItem(*it);
+                               refsTW->setItemSelected(*it, true);
                                //Make sure selected item is visible
-                               dialog_->refsLW->scrollToItem(item);
-                               foundItem = true;
+                               refsTW->scrollToItem(*it);
+                               last_reference_ = textToFind;
+                               break;
                        }
+                       ++it;
                }
-               if (foundItem)
-                       last_reference_ = textToFind;
-               else last_reference_ = "";
        }
-       dialog_->refsLW->setUpdatesEnabled(true);
-       dialog_->refsLW->update();
+       refsTW->setUpdatesEnabled(true);
+       refsTW->update();
+
+       // redo filter
+       filterLabels();
 
        // Re-activate the emission of signals by these widgets.
-       dialog_->refsLW->blockSignals(false);
-       dialog_->referenceED->blockSignals(false);
+       refsTW->blockSignals(false);
+       referenceED->blockSignals(false);
+
+       gotoPB->setEnabled(!referenceED->text().isEmpty());
+       typeCO->setEnabled(!referenceED->text().isEmpty());
+       typeLA->setEnabled(!referenceED->text().isEmpty());
 }
 
 
 void GuiRef::updateRefs()
 {
        refs_.clear();
-       string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
-       refs_ = controller().getLabelList(name);
-       dialog_->sortCB->setEnabled(!refs_.empty());
-       dialog_->refsLW->setEnabled(!refs_.empty());
-       dialog_->gotoPB->setEnabled(!refs_.empty());
+       int const the_buffer = bufferCO->currentIndex();
+       if (the_buffer != -1) {
+               FileNameList const names(theBufferList().fileNames());
+               FileName const & name = names[the_buffer];
+               Buffer const * buf = theBufferList().getBuffer(name);
+               buf->getLabelList(refs_);
+       }
+       sortingCO->setEnabled(!refs_.empty());
+       refsTW->setEnabled(!refs_.empty());
+       groupCB->setEnabled(!refs_.empty());
        redoRefs();
 }
 
+
 bool GuiRef::isValid()
 {
-       return !dialog_->referenceED->text().isEmpty();
+       return !referenceED->text().isEmpty();
+}
+
+
+void GuiRef::gotoRef(string const & ref)
+{
+       dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
+       dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
+}
+
+
+void GuiRef::gotoBookmark()
+{
+       dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
+}
+
+
+void GuiRef::filterLabels()
+{
+       Qt::CaseSensitivity cs = csFindCB->isChecked() ?
+               Qt::CaseSensitive : Qt::CaseInsensitive;
+       QTreeWidgetItemIterator it(refsTW);
+       while (*it) {
+               (*it)->setHidden(
+                       (*it)->childCount() == 0
+                       && !(*it)->text(0).contains(filter_->text(), cs)
+               );
+               ++it;
+       }
+}
+
+
+void GuiRef::resetFilter()
+{
+       filter_->setText(QString());
+       filterLabels();
 }
 
+
+bool GuiRef::initialiseParams(std::string const & sdata)
+{
+       InsetCommand::string2params(sdata, params_);
+       return true;
+}
+
+
+void GuiRef::dispatchParams()
+{
+       std::string const lfun = InsetCommand::params2string(params_);
+       dispatch(FuncRequest(getLfun(), lfun));
+}
+
+
+
+Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
+
+
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiRef_moc.cpp"
+#include "moc_GuiRef.cpp"