]> git.lyx.org Git - lyx.git/blobdiff - src/buffer_funcs.cpp
* src/frontends/controllers/Dialog.{cpp,h}:
[lyx.git] / src / buffer_funcs.cpp
index 811d63d6950f0b5bd1152f1a702a83d5874b635e..fd0a4c32e0aa357d775e81920a0d518e30b41f11 100644 (file)
@@ -24,7 +24,7 @@
 #include "gettext.h"
 #include "Language.h"
 #include "LaTeX.h"
-#include "LyXTextClass.h"
+#include "TextClass.h"
 #include "Paragraph.h"
 #include "paragraph_funcs.h"
 #include "ParagraphList.h"
@@ -180,6 +180,47 @@ bool loadLyXFile(Buffer * b, FileName const & s)
        return false;
 }
 
+
+Buffer * checkAndLoadLyXFile(FileName const & filename)
+{
+       // File already open?
+       if (theBufferList().exists(filename.absFilename())) {
+               docstring const file = makeDisplayPath(filename.absFilename(), 20);
+               docstring text = bformat(_("The document %1$s is already "
+                                                    "loaded.\n\nDo you want to revert "
+                                                    "to the saved version?"), file);
+               if (Alert::prompt(_("Revert to saved document?"),
+                               text, 0, 1,  _("&Revert"), _("&Switch to document")))
+                       return theBufferList().getBuffer(filename.absFilename());
+
+               // FIXME: should be LFUN_REVERT
+               if (theBufferList().close(theBufferList().getBuffer(filename.absFilename()), false))
+                       // Load it again.
+                       return checkAndLoadLyXFile(filename);
+               else
+                       // The file could not be closed.
+                       return 0;
+       }
+
+       if (isFileReadable(filename)) {
+               Buffer * b = theBufferList().newBuffer(filename.absFilename());
+               if (!lyx::loadLyXFile(b, filename)) {
+                       theBufferList().release(b);
+                       return 0;
+               }
+               return b;
+       }
+
+       docstring text = bformat(_("The document %1$s does not yet "
+               "exist.\n\nDo you want to create a new document?"),
+               from_utf8(filename.absFilename()));
+       if (!Alert::prompt(_("Create new document?"),
+                       text, 0, 1, _("&Create"), _("Cancel")))
+               return newFile(filename.absFilename(), string(), true);
+
+       return 0;
+}
+
 // FIXME newFile() should probably be a member method of Application...
 Buffer * newFile(string const & filename, string const & templatename,
                 bool const isNamed)
@@ -355,7 +396,7 @@ bool needEnumCounterReset(ParIterator const & it)
 void setCaptionLabels(Inset & inset, string const & type,
                docstring const label, Counters & counters)
 {
-       LyXText * text = inset.getText(0);
+       Text * text = inset.getText(0);
        if (!text)
                return;
 
@@ -377,7 +418,7 @@ void setCaptionLabels(Inset & inset, string const & type,
                        setCaptionLabels(icap, type, label, counters);
                        if (icap.lyxCode() == Inset::CAPTION_CODE) {
                                // We found a caption!
-                               counters.step(counter); 
+                               counters.step(counter);
                                int number = counters.value(counter);
                                InsetCaption & ic = static_cast<InsetCaption &>(icap);
                                ic.setType(type);
@@ -389,7 +430,7 @@ void setCaptionLabels(Inset & inset, string const & type,
 }
 
 
-void setCaptions(Paragraph & par, LyXTextClass const & textclass)
+void setCaptions(Paragraph & par, TextClass const & textclass)
 {
        if (par.insetlist.empty())
                return;
@@ -400,9 +441,9 @@ void setCaptions(Paragraph & par, LyXTextClass const & textclass)
        InsetList::iterator end = par.insetlist.end();
        for (; it != end; ++it) {
                Inset & inset = *it->inset;
-               if (inset.lyxCode() == Inset::FLOAT_CODE 
+               if (inset.lyxCode() == Inset::FLOAT_CODE
                        || inset.lyxCode() == Inset::WRAP_CODE) {
-                       docstring const & name = inset.insetName();
+                       docstring const name = inset.name();
                        if (name.empty())
                                continue;
 
@@ -417,11 +458,17 @@ void setCaptions(Paragraph & par, LyXTextClass const & textclass)
                        // FIXME: are "table" and "Table" the correct type and label?
                        setCaptionLabels(inset, "table", from_ascii("Table"), counters);
                }
+               else if (inset.lyxCode() == Inset::LISTINGS_CODE)
+                       setCaptionLabels(inset, "listing", from_ascii("Listing"), counters);
+               else if (inset.lyxCode() == Inset::INCLUDE_CODE)
+                       // if this include inset contains lstinputlisting, and has a caption
+                       // it will increase the 'listing' counter by one
+                       static_cast<InsetInclude &>(inset).updateCounter(counters);
        }
 }
 
 // set the label of a paragraph. This includes the counters.
-void setLabel(Buffer const & buf, ParIterator & it, LyXTextClass const & textclass)
+void setLabel(Buffer const & buf, ParIterator & it, TextClass const & textclass)
 {
        Paragraph & par = *it;
        Layout_ptr const & layout = par.layout();
@@ -559,13 +606,13 @@ void setLabel(Buffer const & buf, ParIterator & it, LyXTextClass const & textcla
                                break;
                        }
                }
-               // FIXME Can insetName() return an empty name for wide or
+               // FIXME Can Inset::name() return an empty name for wide or
                // float insets? If not we can put the definition of type
                // inside the if (in) clause and use that instead of
                // if (!type.empty()).
                docstring type;
                if (in)
-                       type = in->insetName();
+                       type = in->name();
 
                if (!type.empty()) {
                        Floating const & fl = textclass.floats().getType(to_ascii(type));
@@ -598,7 +645,7 @@ bool updateCurrentLabel(Buffer const & buf,
     if (it == par_iterator_end(buf.inset()))
        return false;
 
-//     if (it.lastpit == 0 && LyXText::isMainText(buf))
+//     if (it.lastpit == 0 && Text::isMainText(buf))
 //             return false;
 
        switch (it->layout()->labeltype) {
@@ -610,7 +657,7 @@ bool updateCurrentLabel(Buffer const & buf,
        case LABEL_CENTERED_TOP_ENVIRONMENT:
        case LABEL_STATIC:
        case LABEL_ITEMIZE:
-               setLabel(buf, it, buf.params().getLyXTextClass());
+               setLabel(buf, it, buf.params().getTextClass());
                return true;
 
        case LABEL_SENSITIVE:
@@ -651,7 +698,7 @@ void updateLabels(Buffer const & buf, ParIterator & iter, bool childonly)
 void updateLabels(Buffer const & buf, bool childonly)
 {
        // Use the master text class also for child documents
-       LyXTextClass const & textclass = buf.params().getLyXTextClass();
+       TextClass const & textclass = buf.params().getTextClass();
 
        if (!childonly) {
                // If this is a child document start with the master
@@ -696,16 +743,17 @@ void updateLabels(Buffer const & buf, bool childonly)
 
        Buffer & cbuf = const_cast<Buffer &>(buf);
        cbuf.tocBackend().update();
-       cbuf.structureChanged();
+       if (!childonly)
+               cbuf.structureChanged();
 }
 
 
 void checkBufferStructure(Buffer & buffer, ParIterator const & par_it)
 {
-       if (par_it->layout()->labeltype == LABEL_COUNTER
-               && par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
-               buffer.tocBackend().updateItem(par_it);
-               buffer.structureChanged();
+       if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
+               Buffer * master = buffer.getMasterBuffer();
+               master->tocBackend().updateItem(par_it);
+               master->structureChanged();
        }
 }