From 479e9054dbba9753a1a5e03e12ffab970caf4557 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Sat, 11 Nov 2006 00:35:14 +0000 Subject: [PATCH] * Buffer - new pimpled TocBackend member and associated accessors. * toc.[Ch]: delete all toc related methods except outline. * TocBackend: - goTo(): deleted, this gets rid of the LyXView dependency - made all accessors const. * ControlToc: - rework the controller to work exclusively with TocBackend. - goTo(): now call LyXView::dispatch() directly all other files: update with the TocBackend or ControlToc API changes. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15852 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/MenuBackend.C | 30 ++++----- src/TocBackend.C | 24 ++----- src/TocBackend.h | 16 ++--- src/buffer.C | 20 +++++- src/buffer.h | 6 ++ src/buffer_funcs.C | 4 +- src/frontends/controllers/ControlToc.C | 41 ++++++++---- src/frontends/controllers/ControlToc.h | 9 +-- src/frontends/qt4/QToc.C | 4 +- src/insets/insetfloat.C | 4 +- src/insets/insetfloat.h | 4 +- src/insets/insetfloatlist.C | 4 +- src/insets/insettoc.C | 5 +- src/insets/insetwrap.C | 4 +- src/insets/insetwrap.h | 4 +- src/toc.C | 89 -------------------------- src/toc.h | 34 ---------- 17 files changed, 102 insertions(+), 200 deletions(-) diff --git a/src/MenuBackend.C b/src/MenuBackend.C index 8fe76f1087..7397fcee34 100644 --- a/src/MenuBackend.C +++ b/src/MenuBackend.C @@ -35,7 +35,7 @@ #include "lyx_main.h" // for lastfiles #include "lyxfunc.h" #include "lyxlex.h" -#include "toc.h" +#include "TocBackend.h" #include "ToolbarBackend.h" #include "support/filetools.h" @@ -633,22 +633,22 @@ void expandCharStyleInsert(Menu & tomenu, Buffer const * buf) Menu::size_type const max_number_of_items = 25; void expandToc2(Menu & tomenu, - lyx::toc::Toc const & toc_list, - lyx::toc::Toc::size_type from, - lyx::toc::Toc::size_type to, int depth) + TocBackend::Toc const & toc_list, + TocBackend::Toc::size_type from, + TocBackend::Toc::size_type to, int depth) { int shortcut_count = 0; // check whether depth is smaller than the smallest depth in toc. int min_depth = 1000; - for (lyx::toc::Toc::size_type i = from; i < to; ++i) + for (TocBackend::Toc::size_type i = from; i < to; ++i) min_depth = std::min(min_depth, toc_list[i].depth()); if (min_depth > depth) depth = min_depth; if (to - from <= max_number_of_items) { - for (lyx::toc::Toc::size_type i = from; i < to; ++i) { + for (TocBackend::Toc::size_type i = from; i < to; ++i) { docstring label(4 * max(0, toc_list[i].depth() - depth), char_type(' ')); label += limit_string_length(toc_list[i].str()); if (toc_list[i].depth() == depth @@ -660,9 +660,9 @@ void expandToc2(Menu & tomenu, FuncRequest(toc_list[i].action()))); } } else { - lyx::toc::Toc::size_type pos = from; + TocBackend::Toc::size_type pos = from; while (pos < to) { - lyx::toc::Toc::size_type new_pos = pos + 1; + TocBackend::Toc::size_type new_pos = pos + 1; while (new_pos < to && toc_list[new_pos].depth() > depth) ++new_pos; @@ -705,18 +705,18 @@ void expandToc(Menu & tomenu, Buffer const * buf) } FloatList const & floatlist = buf->params().getLyXTextClass().floats(); - lyx::toc::TocList const & toc_list = lyx::toc::getTocList(*buf); - lyx::toc::TocList::const_iterator cit = toc_list.begin(); - lyx::toc::TocList::const_iterator end = toc_list.end(); + TocBackend::TocList const & toc_list = buf->tocBackend().tocs(); + TocBackend::TocList::const_iterator cit = toc_list.begin(); + TocBackend::TocList::const_iterator end = toc_list.end(); for (; cit != end; ++cit) { // Handle this later - if (cit->first == "TOC") + if (cit->first == "tableofcontents") continue; // All the rest is for floats auto_ptr menu(new Menu); - lyx::toc::Toc::const_iterator ccit = cit->second.begin(); - lyx::toc::Toc::const_iterator eend = cit->second.end(); + TocBackend::Toc::const_iterator ccit = cit->second.begin(); + TocBackend::Toc::const_iterator eend = cit->second.end(); for (; ccit != eend; ++ccit) { docstring const label = limit_string_length(ccit->str()); menu->add(MenuItem(MenuItem::Command, @@ -730,7 +730,7 @@ void expandToc(Menu & tomenu, Buffer const * buf) } // Handle normal TOC - cit = toc_list.find("TOC"); + cit = toc_list.find("tableofcontents"); if (cit == end) { tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, _("No Table of contents"), diff --git a/src/TocBackend.C b/src/TocBackend.C index 06e2e4db2e..0b4f69c40c 100644 --- a/src/TocBackend.C +++ b/src/TocBackend.C @@ -12,7 +12,7 @@ #include -#include "toc.h" +#include "TocBackend.h" #include "buffer.h" #include "bufferparams.h" @@ -20,26 +20,19 @@ #include "funcrequest.h" #include "LyXAction.h" #include "paragraph.h" -#include "cursor.h" #include "debug.h" -#include "frontends/LyXView.h" - #include "insets/insetfloat.h" #include "insets/insetoptarg.h" #include "insets/insetwrap.h" #include "support/convert.h" -#include namespace lyx { using std::vector; -using std::max; -using std::ostream; using std::string; -using std::endl; /////////////////////////////////////////////////////////////////////////// @@ -108,12 +101,6 @@ docstring const TocBackend::Item::asString() const } -void TocBackend::Item::goTo(LyXView & lv_) const -{ - string const tmp = convert(id()); - lv_.dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp)); -} - FuncRequest TocBackend::Item::action() const { return FuncRequest(LFUN_PARAGRAPH_GOTO, convert(id())); @@ -126,7 +113,7 @@ FuncRequest TocBackend::Item::action() const /////////////////////////////////////////////////////////////////////////// // TocBackend implementation -TocBackend::Toc const & TocBackend::toc(std::string const & type) +TocBackend::Toc const & TocBackend::toc(std::string const & type) const { // Is the type already supported? TocList::const_iterator it = tocs_.find(type); @@ -202,7 +189,7 @@ void TocBackend::update() if (tocstring.empty()) tocstring = pit->asString(*buffer_, true); Item const item(pit, toclevel - min_toclevel, tocstring); - tocs_["TOC"].push_back(item); + tocs_["tableofcontents"].push_back(item); } } @@ -212,9 +199,10 @@ void TocBackend::update() } -TocBackend::TocIterator const TocBackend::item(std::string const & type, ParConstIterator const & par_it) +TocBackend::TocIterator const TocBackend::item( + std::string const & type, ParConstIterator const & par_it) const { - TocList::iterator toclist_it = tocs_.find(type); + TocList::const_iterator toclist_it = tocs_.find(type); // Is the type supported? BOOST_ASSERT(toclist_it != tocs_.end()); diff --git a/src/TocBackend.h b/src/TocBackend.h index ca584e51e5..4a03493db8 100644 --- a/src/TocBackend.h +++ b/src/TocBackend.h @@ -17,16 +17,17 @@ #define TOC_BACKEND_H #include -#include #include #include #include "pariterator.h" +#include "support/docstream.h" + + namespace lyx { class Buffer; -class LyXView; class Paragraph; class FuncRequest; class LCursor; @@ -63,8 +64,7 @@ public: docstring const & str() const; /// docstring const asString() const; - /// set cursor in LyXView to this Item - void goTo(LyXView & lv_) const; + /// the action corresponding to the goTo above FuncRequest action() const; @@ -98,15 +98,15 @@ public: /// void update(); /// - TocList const & tocs() + TocList const & tocs() const { return tocs_; } /// - std::vector const & types() + std::vector const & types() const { return types_; } /// - Toc const & toc(std::string const & type); + Toc const & toc(std::string const & type) const; /// Return the first Toc Item before the cursor - TocIterator const item(std::string const & type, ParConstIterator const &); + TocIterator const item(std::string const & type, ParConstIterator const &) const; void asciiTocList(std::string const & type, odocstream & os) const; diff --git a/src/buffer.C b/src/buffer.C index 261bc0b144..5e359daab3 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -47,6 +47,7 @@ #include "pariterator.h" #include "sgml.h" #include "texrow.h" +#include "TocBackend.h" #include "undo.h" #include "version.h" @@ -192,13 +193,16 @@ public: /// MacroTable macros; + + /// + TocBackend toc_backend; }; Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_) : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_), - filename(file), file_fully_loaded(false), - inset(params) + filename(file), file_fully_loaded(false), inset(params), + toc_backend(&parent) { inset.setAutoBreakRows(true); lyxvc.buffer(&parent); @@ -326,6 +330,18 @@ TexRow const & Buffer::texrow() const } +TocBackend & Buffer::tocBackend() +{ + return pimpl_->toc_backend; +} + + +TocBackend const & Buffer::tocBackend() const +{ + return pimpl_->toc_backend; +} + + string const Buffer::getLatexName(bool const no_path) const { string const name = changeExtension(makeLatexName(fileName()), ".tex"); diff --git a/src/buffer.h b/src/buffer.h index 0707dacf5f..f30c83d8ab 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -54,6 +54,7 @@ class ParConstIterator; class ParIterator; class TeXErrors; class TexRow; +class TocBackend; class Undo; class StableDocIterator; @@ -355,6 +356,11 @@ public: ErrorList & errorList(std::string const & type); //@} + //@{ + TocBackend & tocBackend(); + TocBackend const & tocBackend() const; + //@} + private: /** Inserts a file into a document \return \c false if method fails. diff --git a/src/buffer_funcs.C b/src/buffer_funcs.C index b6944bf746..a203b62fee 100644 --- a/src/buffer_funcs.C +++ b/src/buffer_funcs.C @@ -32,8 +32,8 @@ #include "pariterator.h" #include "lyxvc.h" #include "texrow.h" +#include "TocBackend.h" #include "vc-backend.h" -#include "toc.h" #include "frontends/Alert.h" @@ -587,7 +587,7 @@ void updateLabels(Buffer const & buf) setLabel(buf, it); } - toc::updateToc(buf); + const_cast(buf).tocBackend().update(); } diff --git a/src/frontends/controllers/ControlToc.C b/src/frontends/controllers/ControlToc.C index b12d5ce89e..3e821965ab 100644 --- a/src/frontends/controllers/ControlToc.C +++ b/src/frontends/controllers/ControlToc.C @@ -13,10 +13,17 @@ #include #include "ControlToc.h" -#include "funcrequest.h" -#include "gettext.h" +#include "buffer.h" #include "BufferView.h" +#include "bufferparams.h" #include "debug.h" +#include "FloatList.h" +#include "funcrequest.h" +#include "gettext.h" + +#include "frontends/LyXView.h" + +#include "support/convert.h" using std::vector; using std::string; @@ -33,15 +40,16 @@ ControlToc::ControlToc(Dialog & d) {} -void ControlToc::goTo(toc::TocItem const & item) +void ControlToc::goTo(TocBackend::Item const & item) { - item.goTo(kernel().lyxview()); + string const tmp = convert(item.id()); + kernel().lyxview().dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp)); } bool ControlToc::canOutline(string const & type) { - return type == "TOC"; + return type == "tableofcontents"; } @@ -71,39 +79,44 @@ void ControlToc::outlineOut() vector const & ControlToc::getTypes() const { - return toc::getTypes(kernel().buffer()); + return kernel().buffer().tocBackend().types(); } -toc::TocIterator const ControlToc::getCurrentTocItem( +TocBackend::Toc::const_iterator const ControlToc::getCurrentTocItem( string const & type) const { BOOST_ASSERT(kernel().bufferview()); - return toc::getCurrentTocItem(kernel().buffer(), - kernel().bufferview()->cursor(), type); + ParConstIterator it(kernel().bufferview()->cursor()); + return kernel().buffer().tocBackend().item(type, it); } string const ControlToc::getGuiName(string const & type) const { - if (type == "TOC") + if (type == "tableofcontents") return lyx::to_utf8(_("Table of Contents")); + + FloatList const & floats = + kernel().buffer().params().getLyXTextClass().floats(); + if (floats.typeExist(type)) + return floats.getType(type).name(); else - return lyx::to_utf8(_(toc::getGuiName(type, kernel().buffer()))); + return lyx::to_utf8(_(type)); } -toc::Toc const empty_list; +TocBackend::Toc const empty_list; -toc::Toc const & ControlToc::getContents(string const & type) const +TocBackend::Toc const & ControlToc::getContents(string const & type) const { // This shouldn't be possible... if (!kernel().isBufferAvailable()) { return empty_list; } - return toc::getToc(kernel().buffer(), type); + return kernel().buffer().tocBackend().toc(type); } } // namespace frontend diff --git a/src/frontends/controllers/ControlToc.h b/src/frontends/controllers/ControlToc.h index 2a2dcce872..98adea8c1f 100644 --- a/src/frontends/controllers/ControlToc.h +++ b/src/frontends/controllers/ControlToc.h @@ -14,7 +14,8 @@ #include "ControlCommand.h" -#include "toc.h" +#include "TocBackend.h" + #include namespace lyx { @@ -28,7 +29,7 @@ public: ControlToc(Dialog &); /// Goto this paragraph id - void goTo(toc::TocItem const &); + void goTo(TocBackend::Item const &); /// Return the list of types available std::vector const & getTypes() const; @@ -37,11 +38,11 @@ public: std::string const getGuiName(std::string const & type) const; /// Return the first TocItem before the cursor - toc::TocIterator const getCurrentTocItem( + TocBackend::Toc::const_iterator const getCurrentTocItem( std::string const & type) const; /// Given a type, returns the contents - toc::Toc const & getContents(std::string const & type) const; + TocBackend::Toc const & getContents(std::string const & type) const; /// Apply the selected outlining operation void outlineUp(); diff --git a/src/frontends/qt4/QToc.C b/src/frontends/qt4/QToc.C index 8c43bc4a4b..22a3990c19 100644 --- a/src/frontends/qt4/QToc.C +++ b/src/frontends/qt4/QToc.C @@ -108,7 +108,7 @@ void QToc::goTo(QModelIndex const & index) << "QToc::goTo " << lyx::to_utf8(it->str()) << endl; - it->goTo(kernel().lyxview()); + ControlToc::goTo(*it); } @@ -127,7 +127,7 @@ void QToc::update() return; } - string const & selected_type = toc::getType(params().getCmdName()); + string const & selected_type = params().getCmdName(); lyxerr[Debug::GUI] << "selected_type " << selected_type << endl; QString gui_names_; diff --git a/src/insets/insetfloat.C b/src/insets/insetfloat.C index fb48320dd7..9d43997fd8 100644 --- a/src/insets/insetfloat.C +++ b/src/insets/insetfloat.C @@ -378,7 +378,7 @@ void InsetFloat::sideways(bool s, BufferParams const & bp) } -void InsetFloat::addToToc(toc::TocList & toclist, Buffer const & buf) const +void InsetFloat::addToToc(TocBackend::TocList & toclist, Buffer const & buf) const { ParConstIterator pit = par_const_iterator_begin(*this); ParConstIterator end = par_const_iterator_end(*this); @@ -390,7 +390,7 @@ void InsetFloat::addToToc(toc::TocList & toclist, Buffer const & buf) const docstring const str = convert(toclist[type].size() + 1) + ". " + pit->asString(buf, false); - toc::TocItem const item(pit, 0, str); + TocBackend::Item const item(pit, 0, str); toclist[type].push_back(item); } } diff --git a/src/insets/insetfloat.h b/src/insets/insetfloat.h index 08b9a574db..c89aff72ad 100644 --- a/src/insets/insetfloat.h +++ b/src/insets/insetfloat.h @@ -14,7 +14,7 @@ #define INSETFLOAT_H #include "insetcollapsable.h" -#include "toc.h" +#include "TocBackend.h" #include "mailinset.h" @@ -76,7 +76,7 @@ public: /// void sideways(bool s, BufferParams const &); /// - void addToToc(toc::TocList &, Buffer const &) const; + void addToToc(TocBackend::TocList &, Buffer const &) const; /// bool showInsetDialog(BufferView *) const; /// diff --git a/src/insets/insetfloatlist.C b/src/insets/insetfloatlist.C index 3ad0c27545..2e2aa13b42 100644 --- a/src/insets/insetfloatlist.C +++ b/src/insets/insetfloatlist.C @@ -23,7 +23,7 @@ #include "LaTeXFeatures.h" #include "lyxlex.h" #include "metricsinfo.h" -#include "toc.h" +#include "TocBackend.h" #include "support/lstrings.h" @@ -133,7 +133,7 @@ int InsetFloatList::plaintext(Buffer const & buffer, odocstream & os, { os << getScreenLabel(buffer) << "\n\n"; - toc::asciiTocList(to_ascii(getParam("type")), buffer, os); + buffer.tocBackend().asciiTocList(to_ascii(getParam("type")), os); os << "\n"; return 0; diff --git a/src/insets/insettoc.C b/src/insets/insettoc.C index 9cba6fa712..8f605b7910 100644 --- a/src/insets/insettoc.C +++ b/src/insets/insettoc.C @@ -12,11 +12,12 @@ #include "insettoc.h" +#include "buffer.h" #include "dispatchresult.h" #include "funcrequest.h" #include "gettext.h" #include "metricsinfo.h" -#include "toc.h" +#include "TocBackend.h" #include "support/std_ostream.h" @@ -59,7 +60,7 @@ int InsetTOC::plaintext(Buffer const & buffer, odocstream & os, { os << getScreenLabel(buffer) << "\n\n"; - toc::asciiTocList(lyx::toc::getType(getCmdName()), buffer, os); + buffer.tocBackend().asciiTocList(getCmdName(), os); os << "\n"; return 0; diff --git a/src/insets/insetwrap.C b/src/insets/insetwrap.C index c6ef786c2d..4c48a7c38d 100644 --- a/src/insets/insetwrap.C +++ b/src/insets/insetwrap.C @@ -224,7 +224,7 @@ bool InsetWrap::showInsetDialog(BufferView * bv) const } -void InsetWrap::addToToc(toc::TocList & toclist, Buffer const & buf) const +void InsetWrap::addToToc(TocBackend::TocList & toclist, Buffer const & buf) const { ParConstIterator pit = par_const_iterator_begin(*this); ParConstIterator end = par_const_iterator_end(*this); @@ -236,7 +236,7 @@ void InsetWrap::addToToc(toc::TocList & toclist, Buffer const & buf) const docstring const str = convert(toclist[type].size() + 1) + ". " + pit->asString(buf, false); - toc::TocItem const item(pit, 0, str); + TocBackend::Item const item(pit, 0, str); toclist[type].push_back(item); } } diff --git a/src/insets/insetwrap.h b/src/insets/insetwrap.h index ce61bdc5c6..7d9e68f098 100644 --- a/src/insets/insetwrap.h +++ b/src/insets/insetwrap.h @@ -13,9 +13,9 @@ #define INSETWRAP_H #include "insetcollapsable.h" -#include "toc.h" #include "lyxlength.h" #include "mailinset.h" +#include "TocBackend.h" namespace lyx { @@ -64,7 +64,7 @@ public: /// bool insetAllowed(InsetBase::Code) const; /// - void addToToc(toc::TocList &, Buffer const &) const; + void addToToc(TocBackend::TocList &, Buffer const &) const; /// bool showInsetDialog(BufferView *) const; /// diff --git a/src/toc.C b/src/toc.C index 85ec935dd7..3516b610d7 100644 --- a/src/toc.C +++ b/src/toc.C @@ -16,7 +16,6 @@ #include "buffer.h" #include "bufferparams.h" -#include "FloatList.h" #include "funcrequest.h" #include "lyxtext.h" #include "LyXAction.h" @@ -26,98 +25,10 @@ #include "debug.h" #include "undo.h" -#include "support/convert.h" - -#include - -using std::map; -using std::pair; -using std::make_pair; -using std::vector; -using std::max; -using std::ostream; -using std::string; -using std::endl; namespace lyx { namespace toc { -typedef map TocMap; -static TocMap toc_backend_; - -/////////////////////////////////////////////////////////////////////////// -// Interface to toc_backend_ - -void updateToc(Buffer const & buf) -{ - TocMap::iterator it = toc_backend_.find(&buf); - if (it == toc_backend_.end()) { - pair result - = toc_backend_.insert(make_pair(&buf, TocBackend(&buf))); - if (!result.second) - return; - - it = result.first; - } - - it->second.update(); -} - - -TocList const & getTocList(Buffer const & buf) -{ - return toc_backend_[&buf].tocs(); -} - - -Toc const & getToc(Buffer const & buf, std::string const & type) -{ - return toc_backend_[&buf].toc(type); -} - - -TocIterator const getCurrentTocItem(Buffer const & buf, LCursor const & cur, - std::string const & type) -{ - return toc_backend_[&buf].item(type, ParConstIterator(cur)); -} - - -vector const & getTypes(Buffer const & buf) -{ - return toc_backend_[&buf].types(); -} - - -void asciiTocList(string const & type, Buffer const & buf, odocstream & os) -{ - toc_backend_[&buf].asciiTocList(type, os); -} - -/////////////////////////////////////////////////////////////////////////// -// Other functions - -string const getType(string const & cmdName) -{ - // special case - if (cmdName == "tableofcontents") - return "TOC"; - else - return cmdName; -} - - -string const getGuiName(string const & type, Buffer const & buffer) -{ - FloatList const & floats = - buffer.params().getLyXTextClass().floats(); - if (floats.typeExist(type)) - return floats.getType(type).name(); - else - return type; -} - - void outline(OutlineOp mode, LCursor & cur) { recordUndo(cur); diff --git a/src/toc.h b/src/toc.h index cf8d029cce..1a5cf4e4c4 100644 --- a/src/toc.h +++ b/src/toc.h @@ -15,45 +15,11 @@ #ifndef TOC_H #define TOC_H -#include "TocBackend.h" - class LCursor; namespace lyx { namespace toc { -typedef TocBackend::Item TocItem; -typedef TocBackend::Toc::const_iterator TocIterator; -typedef TocBackend::Toc Toc; -typedef TocBackend::TocList TocList; - -/// -void updateToc(Buffer const &); - -/// -TocList const & getTocList(Buffer const &); - -/// -Toc const & getToc(Buffer const & buf, std::string const & type); - -/// -std::vector const & getTypes(Buffer const &); - -/// Return the first TocItem before the cursor -TocIterator const getCurrentTocItem(Buffer const &, LCursor const &, - std::string const & type); - -/// -void asciiTocList(std::string const &, Buffer const &, odocstream &); - -/** Given the cmdName of the TOC param, returns the type used - by ControlToc::getContents() */ -std::string const getType(std::string const & cmdName); - -/** Returns the guiname from a given @c type - The localization of the names will be done in the frontends */ -std::string const getGuiName(std::string const & type, Buffer const &); - /// the type of outline operation enum OutlineOp { Up, // Move this header with text down -- 2.39.5