]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Fix bug 4441. GuiRef: Ok button must be default.
[lyx.git] / src / Buffer.cpp
index 080eee73ad70f319121004566958fb456bb2dea7..8105d5261ba8b57006528033b55105e4116f2080 100644 (file)
@@ -289,8 +289,12 @@ Buffer::~Buffer()
        // loop over children
        Impl::BufferPositionMap::iterator it = d->children_positions.begin();
        Impl::BufferPositionMap::iterator end = d->children_positions.end();
-       for (; it != end; ++it)
-               theBufferList().releaseChild(this, const_cast<Buffer *>(it->first));
+       for (; it != end; ++it) {
+               Buffer * child = const_cast<Buffer *>(it->first);
+               // The child buffer might have been closed already.
+               if (theBufferList().isLoaded(child))
+                       theBufferList().releaseChild(this, child);
+       }
 
        // clear references to children in macro tables
        d->children_positions.clear();
@@ -590,15 +594,22 @@ bool Buffer::readDocument(Lexer & lex)
                if (isLyXFilename(master_file.absFilename())) {
                        Buffer * master = 
                                checkAndLoadLyXFile(master_file, true);
-                       // set master as master buffer, but only if we are
-                       // a real child
-                       if (master && master->isChild(this))
-                               d->parent_buffer = master;
-                       else
-                               LYXERR0("The master '" 
-                                       << params().master 
-                                       << "' assigned to this document does not include "
-                                       "this document. Ignoring the master assignment.");
+                       if (master) {
+                               // set master as master buffer, but only
+                               // if we are a real child
+                               if (master->isChild(this))
+                                       setParent(master);
+                               // if the master is not fully loaded
+                               // it is probably just loading this
+                               // child. No warning needed then.
+                               else if (master->isFullyLoaded())
+                                       LYXERR0("The master '"
+                                               << params().master
+                                               << "' assigned to this document '"
+                                               << absFileName()
+                                               << "' does not include "
+                                               "this document. Ignoring the master assignment.");
+                       }
                }
        }
 
@@ -768,7 +779,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
                                     bformat(_("%1$s is from a different"
                                              " version of LyX, but a temporary"
                                              " file for converting it could"
-                                                           " not be created."),
+                                             " not be created."),
                                              from_utf8(filename.absFilename())));
                        return failure;
                }
@@ -778,7 +789,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
                                     bformat(_("%1$s is from a different"
                                               " version of LyX, but the"
                                               " conversion script lyx2lyx"
-                                                           " could not be found."),
+                                              " could not be found."),
                                               from_utf8(filename.absFilename())));
                        return failure;
                }
@@ -797,7 +808,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
                        Alert::error(_("Conversion script failed"),
                                     bformat(_("%1$s is from a different version"
                                              " of LyX, but the lyx2lyx script"
-                                                           " failed to convert it."),
+                                             " failed to convert it."),
                                              from_utf8(filename.absFilename())));
                        return failure;
                } else {
@@ -1357,10 +1368,10 @@ void Buffer::getLabelList(vector<docstring> & list) const
 }
 
 
-void Buffer::updateBibfilesCache() const
+void Buffer::updateBibfilesCache(UpdateScope scope) const
 {
        // If this is a child document, use the parent's cache instead.
-       if (d->parent_buffer) {
+       if (d->parent_buffer && scope != UpdateChildOnly) {
                d->parent_buffer->updateBibfilesCache();
                return;
        }
@@ -1379,7 +1390,7 @@ void Buffer::updateBibfilesCache() const
                                static_cast<InsetInclude &>(*it);
                        inset.updateBibfilesCache();
                        support::FileNameList const & bibfiles =
-                                       inset.getBibfilesCache(*this);
+                                       inset.getBibfilesCache();
                        d->bibfilesCache_.insert(d->bibfilesCache_.end(),
                                bibfiles.begin(),
                                bibfiles.end());
@@ -1396,15 +1407,15 @@ void Buffer::invalidateBibinfoCache()
 }
 
 
-support::FileNameList const & Buffer::getBibfilesCache() const
+support::FileNameList const & Buffer::getBibfilesCache(UpdateScope scope) const
 {
        // If this is a child document, use the parent's cache instead.
-       if (d->parent_buffer)
+       if (d->parent_buffer && scope != UpdateChildOnly)
                return d->parent_buffer->getBibfilesCache();
 
        // We update the cache when first used instead of at loading time.
        if (d->bibfilesCache_.empty())
-               const_cast<Buffer *>(this)->updateBibfilesCache();
+               const_cast<Buffer *>(this)->updateBibfilesCache(scope);
 
        return d->bibfilesCache_;
 }
@@ -1754,6 +1765,25 @@ DocIterator Buffer::firstChildPosition(Buffer const * child)
 }
 
 
+std::vector<Buffer *> Buffer::getChildren() const
+{
+       std::vector<Buffer *> clist;
+       // loop over children
+       Impl::BufferPositionMap::iterator it = d->children_positions.begin();
+       Impl::BufferPositionMap::iterator end = d->children_positions.end();
+       for (; it != end; ++it) {
+               Buffer * child = const_cast<Buffer *>(it->first);
+               clist.push_back(child);
+               // there might be grandchildren
+               std::vector<Buffer *> glist = child->getChildren();
+               for (vector<Buffer *>::const_iterator git = glist.begin();
+                    git != glist.end(); ++git)
+                       clist.push_back(*git);
+       }
+       return clist;
+}
+
+
 template<typename M>
 typename M::iterator greatest_below(M & m, typename M::key_type const & x)
 {
@@ -1918,8 +1948,7 @@ void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
                        // is it a nested text inset?
                        if (iit->inset->asInsetText()) {
                                // Inset needs its own scope?
-                               InsetText const * itext
-                               = iit->inset->asInsetText();
+                               InsetText const * itext = iit->inset->asInsetText();
                                bool newScope = itext->isMacroScope();
 
                                // scope which ends just behind the inset
@@ -1936,19 +1965,19 @@ void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
                        // is it an external file?
                        if (iit->inset->lyxCode() == INCLUDE_CODE) {
                                // get buffer of external file
-                               InsetInclude const & inset
-                                       static_cast<InsetInclude const &>(*iit->inset);
+                               InsetInclude const & inset =
+                                       static_cast<InsetInclude const &>(*iit->inset);
                                d->macro_lock = true;
-                               Buffer * child = inset.loadIfNeeded(*this);
+                               Buffer * child = inset.getChildBuffer();
                                d->macro_lock = false;
                                if (!child)
                                        continue;
 
                                // register its position, but only when it is
                                // included first in the buffer
-                               if (d->children_positions.find(child)
-                                       == d->children_positions.end())
-                                       d->children_positions[child] = it;
+                               if (d->children_positions.find(child) ==
+                                       d->children_positions.end())
+                                               d->children_positions[child] = it;
 
                                // register child with its scope
                                d->position_to_children[it] = Impl::ScopeBuffer(scope, child);
@@ -1959,8 +1988,8 @@ void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
                                continue;
 
                        // get macro data
-                       MathMacroTemplate & macroTemplate
-                       = static_cast<MathMacroTemplate &>(*iit->inset);
+                       MathMacroTemplate & macroTemplate =
+                               static_cast<MathMacroTemplate &>(*iit->inset);
                        MacroContext mc(*this, it);
                        macroTemplate.updateToContext(mc);
 
@@ -2276,6 +2305,12 @@ void Buffer::resetAutosaveTimers() const
 }
 
 
+bool Buffer::hasGuiDelegate() const
+{
+       return gui_;
+}
+
+
 void Buffer::setGuiDelegate(frontend::GuiBufferDelegate * gui)
 {
        gui_ = gui;
@@ -2693,7 +2728,13 @@ void Buffer::bufferErrors(TeXErrors const & terr, ErrorList & errorList) const
 }
 
 
-void Buffer::updateLabels(bool childonly) const
+void Buffer::setBuffersForInsets() const
+{
+       inset().setBuffer(const_cast<Buffer &>(*this)); 
+}
+
+
+void Buffer::updateLabels(UpdateScope scope) const
 {
        // Use the master text class also for child documents
        Buffer const * const master = masterBuffer();
@@ -2703,11 +2744,15 @@ void Buffer::updateLabels(bool childonly) const
        // master comes back we can see which of them were actually seen (i.e.
        // via an InsetInclude). The remaining ones in the set need still be updated.
        static std::set<Buffer const *> bufToUpdate;
-       if (!childonly) {
+       if (scope == UpdateMaster) {
                // If this is a child document start with the master
                if (master != this) {
                        bufToUpdate.insert(this);
-                       master->updateLabels(false);
+                       master->updateLabels();
+                       // Do this here in case the master has no gui associated with it. Then, 
+                       // the TocModel is not updated and TocModel::toc_ is invalid (bug 5699).
+                       if (!master->gui_)
+                               structureChanged();     
 
                        // was buf referenced from the master (i.e. not in bufToUpdate anymore)?
                        if (bufToUpdate.find(this) == bufToUpdate.end())
@@ -2723,7 +2768,6 @@ void Buffer::updateLabels(bool childonly) const
 
        // update all caches
        clearReferenceCache();
-       inset().setBuffer(const_cast<Buffer &>(*this));
        updateMacros();
 
        Buffer & cbuf = const_cast<Buffer &>(*this);
@@ -2739,7 +2783,7 @@ void Buffer::updateLabels(bool childonly) const
                return;
 
        cbuf.tocBackend().update();
-       if (!childonly)
+       if (scope == UpdateMaster)
                cbuf.structureChanged();
 }