-// -*- C++ -*-
-/* This file is part of
- * ======================================================
+/**
+ * \file FormToc.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * LyX, The Document Processor
+ * \author Angus Leeming
*
- * Copyright 2000 The LyX Team.
- *
- * ======================================================
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-#include <vector>
-#include FORMS_H_LOCATION
+#include "FormToc.h"
+#include "ControlToc.h"
+#include "forms/form_toc.h"
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include "xforms_helpers.h"
+#include "xformsBC.h"
+#include "support/lstrings.h" // frontStrip, strip
-#include "Dialogs.h"
-#include "FormToc.h"
-#include "LyXView.h"
-#include "form_toc.h"
-#include "lyxtext.h"
-#include "lyxfunc.h"
-#include "support/lstrings.h"
-
-// The current code uses the apply() for handling the Update button and the
-// type-of-table selection and cancel() for the close button. This is a little
-// confusing to the button controller so I've made an IgnorantPolicy to cover
-// this situation since the dialog doesn't care about buttons. ARRae 20001013
-FormToc::FormToc(LyXView * lv, Dialogs * d)
- : FormCommand(lv, d, _("Table of Contents"), new OkCancelPolicy),
- dialog_(0)
-{
- // let the dialog be shown
- // These are permanent connections so we won't bother
- // storing a copy because we won't be disconnecting.
- d->showTOC.connect(slot(this, &FormToc::showInset));
- d->createTOC.connect(slot(this, &FormToc::createInset));
-}
+#include "lyx_forms.h"
+using lyx::support::getStringFromVector;
-FormToc::~FormToc()
-{
- delete dialog_;
-}
+using std::vector;
+using std::string;
+
+
+typedef FormController<ControlToc, FormView<FD_toc> > base_class;
+FormToc::FormToc(Dialog & parent)
+ : base_class(parent, _("Table of Contents"))
+{}
-FL_FORM * FormToc::form() const
+
+void FormToc::build()
{
- if (dialog_ ) return dialog_->form;
- return 0;
+ dialog_.reset(build_toc(this));
+
+ vector<string> types = controller().getTypes();
+
+
+ string const choice =
+ ' ' + getStringFromVector(controller().getTypes(), " | ") + ' ';
+ fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
+
+ // Manage the cancel/close button
+ bcview().setCancel(dialog_->button_close);
}
-void FormToc::disconnect()
+void FormToc::update()
{
- toclist.clear();
- FormCommand::disconnect();
+ updateType();
+ updateContents();
}
-void FormToc::build()
+ButtonPolicy::SMInput FormToc::input(FL_OBJECT * ob, long)
{
- dialog_ = build_toc();
-
-#if 0
- fl_addto_choice(dialog_->choice_toc_type,
- _(" TOC | LOF | LOT | LOA "));
-#else
- map<string, vector<Buffer::TocItem> > tmp =
- lv_->view()->buffer()->getTocList();
- string types;
- map<string, vector<Buffer::TocItem> >::const_iterator cit = tmp.begin();
- map<string, vector<Buffer::TocItem> >::const_iterator end = tmp.end();
- for (; cit != end; ++cit) {
- fl_addto_choice(dialog_->choice_toc_type, cit->first.c_str());
+ if (ob == dialog_->browser_toc) {
+ unsigned int const choice = fl_get_browser(dialog_->browser_toc);
+
+ if (choice - 1 < toc_.size() && choice >= 1) {
+ controller().goTo(toc_[choice - 1]);
+ }
+ return ButtonPolicy::SMI_VALID;
+ }
+
+ if (ob != dialog_->choice_toc_type) {
+ updateType();
}
-#endif
- // Don't need to limit size of this dialog
- // (but fixing min size is a GOOD thing).
- // Workaround dumb xforms sizing bug
- minw_ = form()->w;
- minh_ = form()->h;
-
- // Manage the cancel/close button
- bc_.setCancel(dialog_->button_cancel);
- bc_.refresh();
+
+ updateContents();
+
+ return ButtonPolicy::SMI_VALID;
}
-void FormToc::update()
+void FormToc::updateType()
{
-#if 0
- Buffer::TocType type;
-
- if (params.getCmdName() == "tableofcontents" )
- type = Buffer::TOC_TOC;
-
- else if (params.getCmdName() == "listofalgorithms" )
- type = Buffer::TOC_LOA;
-
- else if (params.getCmdName() == "listoffigures" )
- type = Buffer::TOC_LOF;
-
- else
- type = Buffer::TOC_LOT;
-
- fl_set_choice( dialog_->choice_toc_type, type+1 );
-#else
-#warning Reimplement (Lgb)
-#endif
- updateToc();
+ // Update the choice list from scratch
+ fl_clear_choice(dialog_->choice_toc_type);
+ string const choice = getStringFromVector(controller().getTypes(), "|");
+ fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
+
+ // And select the correct one
+ string const type = lyx::toc::getType(controller().params().getCmdName());
+ fl_set_choice_text(dialog_->choice_toc_type, type.c_str());
}
-void FormToc::updateToc()
+void FormToc::updateContents()
{
-#if 0
- if (!lv_->view()->available()) {
- toclist.clear();
- fl_clear_browser( dialog_->browser_toc );
- fl_add_browser_line( dialog_->browser_toc,
- _("*** No Document ***"));
+ string const type = getString(dialog_->choice_toc_type);
+ if (type.empty()) {
+ fl_clear_browser(dialog_->browser_toc);
+ fl_add_browser_line(dialog_->browser_toc,
+ _("*** No Lists ***").c_str());
+ setEnabled(dialog_->browser_toc, false);
return;
}
- vector<vector<Buffer::TocItem> > tmp =
- lv_->view()->buffer()->getTocList();
- int type = fl_get_choice( dialog_->choice_toc_type ) - 1;
+ lyx::toc::Toc const contents = controller().getContents(type);
// Check if all elements are the same.
- if (toclist.size() == tmp[type].size()) {
- unsigned int i = 0;
- for (; i < toclist.size(); ++i) {
- if (toclist[i] != tmp[type][i])
- break;
- }
- if (i >= toclist.size()) return;
- }
-
- // List has changed. Update browser
- toclist = tmp[type];
-
- static Buffer * buffer = 0;
- int topline = 0;
- int line = 0;
- if (buffer == lv_->view()->buffer()) {
- topline = fl_get_browser_topline( dialog_->browser_toc );
- line = fl_get_browser( dialog_->browser_toc );
- } else
- buffer = lv_->view()->buffer();
-
- fl_clear_browser( dialog_->browser_toc );
-
- for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
- it != toclist.end(); ++it)
- fl_add_browser_line( dialog_->browser_toc,
- (string(4 * (*it).depth, ' ')
- + (*it).str).c_str());
-
- fl_set_browser_topline( dialog_->browser_toc, topline );
- fl_select_browser_line( dialog_->browser_toc, line );
-#else
-#warning Fix Me! (Lgb)
- if (!lv_->view()->available()) {
- toclist.clear();
- fl_clear_browser( dialog_->browser_toc );
- fl_add_browser_line( dialog_->browser_toc,
- _("*** No Document ***"));
+ if (toc_ == contents) {
return;
}
- map<string, vector<Buffer::TocItem> > tmp =
- lv_->view()->buffer()->getTocList();
- //int type = fl_get_choice( dialog_->choice_toc_type ) - 1;
- string type = fl_get_choice_item_text(dialog_->choice_toc_type,
- fl_get_choice(dialog_->choice_toc_type));
-
- map<string, vector<Buffer::TocItem> >::iterator it = tmp.find(type);
+ // List has changed. Update browser
+ toc_ = contents;
- if (it != tmp.end()) {
- // Check if all elements are the same.
- if (toclist == it->second) {
- return;
- }
- } else if (it == tmp.end()) {
- toclist.clear();
+ if (contents.empty()) {
fl_clear_browser(dialog_->browser_toc);
fl_add_browser_line(dialog_->browser_toc,
- _("*** No Lists ***"));
+ _("*** No Lists ***").c_str());
+ setEnabled(dialog_->browser_toc, false);
return;
}
-
- // List has changed. Update browser
- toclist = it->second;
- static Buffer * buffer = 0;
- int topline = 0;
- int line = 0;
- if (buffer == lv_->view()->buffer()) {
- topline = fl_get_browser_topline(dialog_->browser_toc);
- line = fl_get_browser( dialog_->browser_toc );
- } else
- buffer = lv_->view()->buffer();
+ unsigned int const topline =
+ fl_get_browser_topline(dialog_->browser_toc);
+ unsigned int const line = fl_get_browser(dialog_->browser_toc);
fl_clear_browser(dialog_->browser_toc);
+ setEnabled(dialog_->browser_toc, true);
- vector<Buffer::TocItem>::const_iterator cit = toclist.begin();
- vector<Buffer::TocItem>::const_iterator end = toclist.end();
-
+ lyx::toc::Toc::const_iterator cit = contents.begin();
+ lyx::toc::Toc::const_iterator end = contents.end();
for (; cit != end; ++cit) {
- string const line = string(4 * cit->depth, ' ') + cit->str;
- fl_add_browser_line(dialog_->browser_toc, line.c_str());
+ fl_add_browser_line(dialog_->browser_toc,
+ cit->asString().c_str());
}
-
+
fl_set_browser_topline(dialog_->browser_toc, topline);
fl_select_browser_line(dialog_->browser_toc, line);
-#endif
-}
-
-
-bool FormToc::input(FL_OBJECT *, long)
-{
- updateToc();
-
- unsigned int const choice = fl_get_browser( dialog_->browser_toc );
- if (0 < choice && choice - 1 < toclist.size()) {
- string const tmp = tostr(toclist[choice-1].par->id());
- lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp);
- }
-
- return true;
}