Hello,

As discussed with Georg, this patch deletes most of the toc.[Ch] flesh (remains only outline which should be moved elsewhere). The different tocs were formerly defined and updated by a global map of TocBackends keyed with (Buffer*). Now TocBackend is a proper pimpled member of Buffer; this simplifies the code IMO.

I got rid also of most of the dependency of toc.[Ch], even LyXView.

This is just a reorganisation patch. That was necessary in any case in a multi-view context. I am going to commit this and try to adapt Georg multi-part document patch to this new structure.

I've noticed some bugs in the Toc dialog also as pointed out by Jurgen, I'll try to look at that.

Abdel.
Index: buffer.C
===================================================================
--- buffer.C    (revision 15850)
+++ buffer.C    (working copy)
@@ -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 @@
 
        ///
        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 @@
 }
 
 
+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");
Index: buffer.h
===================================================================
--- buffer.h    (revision 15850)
+++ buffer.h    (working copy)
@@ -54,6 +54,7 @@
 class ParIterator;
 class TeXErrors;
 class TexRow;
+class TocBackend;
 class Undo;
 class StableDocIterator;
 
@@ -355,6 +356,11 @@
        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.
Index: buffer_funcs.C
===================================================================
--- buffer_funcs.C      (revision 15850)
+++ buffer_funcs.C      (working copy)
@@ -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 @@
                setLabel(buf, it);
        }
 
-       toc::updateToc(buf);
+       const_cast<Buffer &>(buf).tocBackend().update();
 }
 
 
Index: frontends/controllers/ControlToc.C
===================================================================
--- frontends/controllers/ControlToc.C  (revision 15850)
+++ frontends/controllers/ControlToc.C  (working copy)
@@ -13,11 +13,18 @@
 #include <config.h>
 
 #include "ControlToc.h"
+#include "buffer.h"
+#include "BufferView.h"
+#include "bufferparams.h"
+#include "debug.h"
+#include "FloatList.h"
 #include "funcrequest.h"
 #include "gettext.h"
-#include "BufferView.h"
-#include "debug.h"
 
+#include "frontends/LyXView.h"
+
+#include "support/convert.h"
+
 using std::vector;
 using std::string;
 
@@ -33,15 +40,16 @@
 {}
 
 
-void ControlToc::goTo(toc::TocItem const & item)
+void ControlToc::goTo(TocBackend::Item const & item)
 {
-       item.goTo(kernel().lyxview());
+       string const tmp = convert<string>(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 @@
 
 vector<string> 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
Index: frontends/controllers/ControlToc.h
===================================================================
--- frontends/controllers/ControlToc.h  (revision 15850)
+++ frontends/controllers/ControlToc.h  (working copy)
@@ -14,7 +14,8 @@
 
 
 #include "ControlCommand.h"
-#include "toc.h"
+#include "TocBackend.h"
+
 #include <vector>
 
 namespace lyx {
@@ -28,7 +29,7 @@
        ControlToc(Dialog &);
 
        /// Goto this paragraph id
-       void goTo(toc::TocItem const &);
+       void goTo(TocBackend::Item const &);
 
        /// Return the list of types available
        std::vector<std::string> const & getTypes() const;
@@ -37,11 +38,11 @@
        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();
Index: frontends/qt4/QToc.C
===================================================================
--- frontends/qt4/QToc.C        (revision 15850)
+++ frontends/qt4/QToc.C        (working copy)
@@ -108,7 +108,7 @@
                << "QToc::goTo " << lyx::to_utf8(it->str())
                << endl;
 
-       it->goTo(kernel().lyxview());
+       ControlToc::goTo(*it);
 }
 
 
@@ -127,7 +127,7 @@
                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_;
Index: insets/insetfloat.C
===================================================================
--- insets/insetfloat.C (revision 15850)
+++ insets/insetfloat.C (working copy)
@@ -378,7 +378,7 @@
 }
 
 
-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 @@
                        docstring const str =
                                convert<docstring>(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);
                }
        }
Index: insets/insetfloat.h
===================================================================
--- insets/insetfloat.h (revision 15850)
+++ insets/insetfloat.h (working copy)
@@ -14,7 +14,7 @@
 #define INSETFLOAT_H
 
 #include "insetcollapsable.h"
-#include "toc.h"
+#include "TocBackend.h"
 #include "mailinset.h"
 
 
@@ -76,7 +76,7 @@
        ///
        void sideways(bool s, BufferParams const &);
        ///
-       void addToToc(toc::TocList &, Buffer const &) const;
+       void addToToc(TocBackend::TocList &, Buffer const &) const;
        ///
        bool  showInsetDialog(BufferView *) const;
        ///
Index: insets/insetfloatlist.C
===================================================================
--- insets/insetfloatlist.C     (revision 15850)
+++ insets/insetfloatlist.C     (working copy)
@@ -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 @@
 {
        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;
Index: insets/insettoc.C
===================================================================
--- insets/insettoc.C   (revision 15850)
+++ insets/insettoc.C   (working copy)
@@ -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 @@
 {
        os << getScreenLabel(buffer) << "\n\n";
 
-       toc::asciiTocList(lyx::toc::getType(getCmdName()), buffer, os);
+       buffer.tocBackend().asciiTocList(getCmdName(), os);
 
        os << "\n";
        return 0;
Index: insets/insetwrap.C
===================================================================
--- insets/insetwrap.C  (revision 15850)
+++ insets/insetwrap.C  (working copy)
@@ -224,7 +224,7 @@
 }
 
 
-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 @@
                        docstring const str =
                                convert<docstring>(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);
                }
        }
Index: insets/insetwrap.h
===================================================================
--- insets/insetwrap.h  (revision 15850)
+++ insets/insetwrap.h  (working copy)
@@ -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 @@
        ///
        bool insetAllowed(InsetBase::Code) const;
        ///
-       void addToToc(toc::TocList &, Buffer const &) const;
+       void addToToc(TocBackend::TocList &, Buffer const &) const;
        ///
        bool showInsetDialog(BufferView *) const;
        ///
Index: MenuBackend.C
===================================================================
--- MenuBackend.C       (revision 15850)
+++ MenuBackend.C       (working copy)
@@ -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 @@
 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 @@
                                            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 @@
        }
 
        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> 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 @@
        }
 
        // 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"),
Index: toc.C
===================================================================
--- toc.C       (revision 15850)
+++ toc.C       (working copy)
@@ -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 <map>
-
-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<Buffer const *, TocBackend> 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<TocMap::iterator, bool> 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<string> 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);
Index: toc.h
===================================================================
--- toc.h       (revision 15850)
+++ toc.h       (working copy)
@@ -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<std::string> 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
Index: TocBackend.C
===================================================================
--- TocBackend.C        (revision 15850)
+++ TocBackend.C        (working copy)
@@ -12,7 +12,7 @@
 
 #include <config.h>
 
-#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 <iostream>
 
 namespace lyx {
 
 using std::vector;
-using std::max;
-using std::ostream;
 using std::string;
-using std::endl;
 
 
 ///////////////////////////////////////////////////////////////////////////
@@ -108,12 +101,6 @@
 }
 
 
-void TocBackend::Item::goTo(LyXView & lv_) const
-{
-       string const tmp = convert<string>(id());
-       lv_.dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
-}
-
 FuncRequest TocBackend::Item::action() const
 {
        return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
@@ -126,7 +113,7 @@
 ///////////////////////////////////////////////////////////////////////////
 // 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 @@
                        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 @@
 }
 
 
-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());
 

Reply via email to