+ setFocusProxy(tocTV);
+
+ // The toc types combo won't change its model.
+ typeCO->setModel(gui_view_.tocModels().nameModel());
+
+ // Make sure the buttons are disabled when first shown without a loaded
+ // Buffer.
+ enableControls(false);
+
+ // make us responsible for the context menu of the tabbar
+ setContextMenuPolicy(Qt::CustomContextMenu);
+ connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
+ this, SLOT(showContextMenu(const QPoint &)));
+ connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
+ this, SLOT(showContextMenu(const QPoint &)));
+ connect(filterLE, SIGNAL(textEdited(QString)),
+ this, SLOT(filterContents()));
+
+ init(QString());
+}
+
+
+void TocWidget::showContextMenu(const QPoint & pos)
+{
+ std::string name = "context-toc-" + fromqstr(current_type_);
+ QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
+ if (!menu)
+ return;
+ menu->exec(mapToGlobal(pos));
+}
+
+
+Inset * TocWidget::itemInset() const
+{
+ QModelIndex const & index = tocTV->currentIndex();
+ TocItem const & item =
+ gui_view_.tocModels().currentItem(current_type_, index);
+ DocIterator const & dit = item.dit();
+
+ Inset * inset = 0;
+ if (current_type_ == "label"
+ || current_type_ == "graphics"
+ || current_type_ == "citation"
+ || current_type_ == "child")
+ inset = dit.nextInset();
+
+ else if (current_type_ == "branch"
+ || current_type_ == "index"
+ || current_type_ == "change")
+ inset = &dit.inset();
+
+ else if (current_type_ == "table"
+ || current_type_ == "listing"
+ || current_type_ == "figure") {
+ DocIterator tmp_dit(dit);
+ tmp_dit.pop_back();
+ inset = &tmp_dit.inset();
+ }
+ return inset;
+}
+
+
+bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
+ FuncStatus & status) const
+{
+ Inset * inset = itemInset();
+ FuncRequest tmpcmd(cmd);
+
+ QModelIndex const & index = tocTV->currentIndex();
+ TocItem const & item =
+ gui_view_.tocModels().currentItem(current_type_, index);
+
+ switch (cmd.action())
+ {
+ case LFUN_CHANGE_ACCEPT:
+ case LFUN_CHANGE_REJECT:
+ case LFUN_OUTLINE_UP:
+ case LFUN_OUTLINE_DOWN:
+ case LFUN_OUTLINE_IN:
+ case LFUN_OUTLINE_OUT:
+ case LFUN_SECTION_SELECT:
+ status.setEnabled(true);
+ return true;
+
+ case LFUN_LABEL_COPY_AS_REF: {
+ // For labels in math, we need to supply the label as a string
+ FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
+ if (inset)
+ return inset->getStatus(cur, label_copy, status);
+ }
+
+ default:
+ if (inset)
+ return inset->getStatus(cur, tmpcmd, status);
+ }
+
+ return false;
+}
+
+
+void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
+{
+ Inset * inset = itemInset();
+ FuncRequest tmpcmd(cmd);
+
+ QModelIndex const & index = tocTV->currentIndex();
+ TocItem const & item =
+ gui_view_.tocModels().currentItem(current_type_, index);
+
+ // Start an undo group.
+ cur.beginUndoGroup();
+
+ switch (cmd.action())
+ {
+ case LFUN_CHANGE_ACCEPT:
+ case LFUN_CHANGE_REJECT:
+ case LFUN_SECTION_SELECT:
+ dispatch(item.action());
+ cur.dispatch(tmpcmd);
+ break;
+
+ case LFUN_LABEL_COPY_AS_REF: {
+ // For labels in math, we need to supply the label as a string
+ FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
+ if (inset)
+ inset->dispatch(cur, label_copy);
+ break;
+ }
+
+ case LFUN_OUTLINE_UP:
+ case LFUN_OUTLINE_DOWN:
+ case LFUN_OUTLINE_IN:
+ case LFUN_OUTLINE_OUT:
+ outline(cmd.action());
+ break;
+
+ default:
+ if (inset)
+ inset->dispatch(cur, tmpcmd);
+ }
+ cur.endUndoGroup();
+}
+
+
+void TocWidget::on_tocTV_activated(QModelIndex const & index)
+{
+ goTo(index);