-// -*- C++ -*-
/* This file is part of
* ======================================================
*
* LyX, The Document Processor
*
- * Copyright 2000 The LyX Team.
+ * Copyright 2000-2001 The LyX Team.
*
* ======================================================
+ *
+ * \file FormToc.C
+ * \author Angus Leeming, a.leeming@ic.ac.uk
*/
#include <config.h>
-#include <vector>
-
-#include FORMS_H_LOCATION
#ifdef __GNUG__
#pragma implementation
#endif
-
-#include "gettext.h"
-#include "Dialogs.h"
#include "FormToc.h"
-#include "LyXView.h"
+#include "xformsBC.h"
+#include "ControlToc.h"
#include "form_toc.h"
-#include "lyxtext.h"
-#include "lyxfunc.h"
-#include "support/lstrings.h"
+#include "helper_funcs.h" // getStringFromVector
+#include "support/lstrings.h" // frontStrip, strip
+#include "debug.h"
+#include "gettext.h"
-FormToc::FormToc(LyXView * lv, Dialogs * d)
- : FormCommand(lv, d, _("Table of Contents")), 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 <vector>
+using std::endl;
-FormToc::~FormToc()
-{
- delete dialog_;
-}
+typedef FormCB<ControlToc, FormDB<FD_form_toc> > base_class;
-void FormToc::clearStore()
-{
- toclist.clear();
-}
+FormToc::FormToc(ControlToc & c)
+ : base_class(c, _("Table of Contents"))
+{}
void FormToc::build()
{
- dialog_ = build_toc();
- fl_addto_choice(dialog_->type,
- _(" TOC | LOF | LOT | LOA "));
+ dialog_.reset(build_toc());
- // Don't need to limit size of this dialog
+ string const choice =
+ " " + getStringFromVector(controller().getTypes(), " | ") + " ";
+ fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
+
+ // Manage the cancel/close button
+ bc().setCancel(dialog_->button_cancel);
}
-FL_FORM * const FormToc::form() const
+void FormToc::update()
{
- if ( dialog_ ) // no need to test for dialog_->form
- return dialog_->form;
- else
- return 0;
+ updateType();
+ updateContents();
}
-void FormToc::update()
+ButtonPolicy::SMInput FormToc::input(FL_OBJECT * ob, long)
{
- Buffer::TocType type;
+ if (ob != dialog_->choice_toc_type) {
+ updateType();
+ }
+
+ updateContents();
- if( params.getCmdName() == "tableofcontents" )
- type = Buffer::TOC_TOC;
+ unsigned int const choice = fl_get_browser( dialog_->browser_toc );
- else if( params.getCmdName() == "listofalgorithms" )
- type = Buffer::TOC_LOA;
+ if (choice - 1 < toclist_.size() && choice >= 1) {
+ controller().Goto(toclist_[choice - 1].par->id());
+ }
- else if( params.getCmdName() == "listoffigures" )
- type = Buffer::TOC_LOF;
+ return ButtonPolicy::SMI_VALID;
+}
- else
- type = Buffer::TOC_LOT;
-
- fl_set_choice( dialog_->type, type+1 );
- updateToc();
+void FormToc::updateType()
+{
+ // Update the choice list from scratch
+ fl_clear_choice(dialog_->choice_toc_type);
+ string const choice = getStringFromVector(controller().getTypes(), "|");
+ lyxerr << "choice " << choice << endl;
+ fl_addto_choice(dialog_->choice_toc_type, choice.c_str());
+
+ // And select the correct one
+ string const type = toc::getType(controller().params().getCmdName());
+
+ fl_set_choice(dialog_->choice_toc_type, 1);
+ for (int i = 1;
+ i <= fl_get_choice_maxitems(dialog_->choice_toc_type); ++i) {
+ string const choice =
+ fl_get_choice_item_text(dialog_->choice_toc_type, i);
+
+ if (choice == type) {
+ fl_set_choice(dialog_->choice_toc_type, i);
+ break;
+ }
+ }
}
-void FormToc::updateToc()
+void FormToc::updateContents()
{
- if (!lv_->view()->available()) {
- toclist.clear();
- fl_clear_browser( dialog_->browser );
- fl_add_browser_line( dialog_->browser, _("*** No Document ***"));
+ char const * tmp = fl_get_choice_text(dialog_->choice_toc_type);
+
+ if (!tmp) {
+ fl_clear_browser(dialog_->browser_toc);
+ fl_add_browser_line(dialog_->browser_toc,
+ _("*** No Lists ***"));
return;
}
+
+ string const type = frontStrip(strip(tmp));
- vector<vector<Buffer::TocItem> > tmp =
- lv_->view()->buffer()->getTocList();
- int type = fl_get_choice( dialog_->type ) - 1;
+ Buffer::SingleList const contents = controller().getContents(type);
+ if (contents.empty()) {
+ fl_clear_browser(dialog_->browser_toc);
+ fl_add_browser_line(dialog_->browser_toc,
+ _("*** No Lists ***"));
+ }
+
// 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;
+ if (toclist_ == contents) {
+ 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 );
- line = fl_get_browser( dialog_->browser );
- } else
- buffer = lv_->view()->buffer();
-
- fl_clear_browser( dialog_->browser );
-
- for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
- it != toclist.end(); ++it)
- fl_add_browser_line( dialog_->browser,
- (string(4*(*it).depth,' ')+(*it).str).c_str());
-
- fl_set_browser_topline( dialog_->browser, topline );
- fl_select_browser_line( dialog_->browser, line );
-}
+ toclist_ = contents;
-
-void FormToc::apply()
-{
- if (!lv_->view()->available())
- return;
+ unsigned int const topline =
+ fl_get_browser_topline(dialog_->browser_toc);
+ unsigned int const line = fl_get_browser(dialog_->browser_toc);
- updateToc();
+ fl_clear_browser( dialog_->browser_toc );
- unsigned int const choice = fl_get_browser( dialog_->browser );
- if (0 < choice && choice - 1 < toclist.size()) {
- string const tmp = tostr(toclist[choice-1].par->id());
- lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tmp);
+ Buffer::SingleList::const_iterator cit = toclist_.begin();
+ Buffer::SingleList::const_iterator end = toclist_.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_set_browser_topline(dialog_->browser_toc, topline);
+ fl_select_browser_line(dialog_->browser_toc, line);
}