]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetCollapsible.cpp
de.po
[lyx.git] / src / insets / InsetCollapsible.cpp
index 11c9e2a43d42de598cbd0897ef119adf3db2edd7..0420452bb98d7f8483613e6d1e80681c5989844f 100644 (file)
 #include "InsetCollapsible.h"
 
 #include "Buffer.h"
+#include "BufferParams.h"
 #include "BufferView.h"
+#include "CutAndPaste.h"
 #include "Cursor.h"
 #include "Dimension.h"
+#include "Format.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "InsetLayout.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
+#include "TextClass.h"
 #include "TocBackend.h"
 
 #include "frontends/FontMetrics.h"
@@ -35,6 +39,7 @@
 #include "support/lassert.h"
 #include "support/lstrings.h"
 #include "support/RefChanger.h"
+#include "support/TempFile.h"
 
 using namespace std;
 
@@ -55,7 +60,18 @@ InsetCollapsible::InsetCollapsible(InsetCollapsible const & rhs)
        : InsetText(rhs),
          status_(rhs.status_),
          labelstring_(rhs.labelstring_)
-{}
+{
+       tempfile_.reset();
+}
+
+
+InsetCollapsible const & InsetCollapsible::operator=(InsetCollapsible const & that)
+{
+       if (&that == this)
+               return *this;
+       *this = InsetCollapsible(that);
+       return *this;
+}
 
 
 InsetCollapsible::~InsetCollapsible()
@@ -276,7 +292,7 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
                }
                // Do not draw the cue for INSERTED -- it is already in the button and
                // that's enough.
-               Changer dummy = (pi.change_.type == Change::INSERTED)
+               Changer cdummy = (pi.change_.type == Change::INSERTED)
                        ? make_change(pi.change_, Change())
                        : Changer();
                InsetText::draw(pi, textx, texty);
@@ -293,9 +309,10 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
        case Corners:
                textx = x;
                texty = baseline;
-               {       // We will take care of the frame and the change tracking cue
-                       // ourselves, below.
-                       Changer dummy = make_change(pi.change_, Change());
+               // We will take care of the frame and the change tracking cue
+               // ourselves, below.
+               {
+                       Changer cdummy = make_change(pi.change_, Change());
                        const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
                        InsetText::draw(pi, textx, texty);
                        const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
@@ -392,6 +409,9 @@ void InsetCollapsible::cursorPos(BufferView const & bv,
 
 bool InsetCollapsible::editable() const
 {
+       if (tempfile_)
+               return false;
+       
        switch (decoration()) {
        case InsetLayout::CLASSIC:
        case InsetLayout::MINIMALISTIC:
@@ -404,6 +424,9 @@ bool InsetCollapsible::editable() const
 
 bool InsetCollapsible::descendable(BufferView const & bv) const
 {
+       if (tempfile_)
+               return false;
+
        return geometry(bv) != ButtonOnly;
 }
 
@@ -423,6 +446,8 @@ docstring const InsetCollapsible::getNewLabel(docstring const & l) const
        pos_type i = 0;
        pos_type j = 0;
        for (; i < n && j < p_siz; ++j) {
+               if (paragraphs().begin()->isDeleted(j))
+                       continue;
                if (paragraphs().begin()->isInset(j)) {
                        if (!paragraphs().begin()->getInset(j)->isChar())
                                continue;
@@ -451,6 +476,7 @@ Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
 {
        //lyxerr << "InsetCollapsible: edit xy" << endl;
        if (geometry(cur.bv()) == ButtonOnly
+               || !descendable(cur.bv())
            || (view_[&cur.bv()].button_dim_.contains(x, y)
                && geometry(cur.bv()) != NoButton))
                return this;
@@ -546,6 +572,41 @@ void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.dispatched();
                break;
 
+       case LFUN_INSET_EDIT: {
+               cur.push(*this);
+               text().selectAll(cur);
+               string const format =
+                       cur.buffer()->params().documentClass().outputFormat();
+               string const ext = theFormats().extension(format);
+               tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
+               support::FileName const tempfilename = tempfile_->name();
+               string const name = tempfilename.toFilesystemEncoding();
+               ofdocstream os(name.c_str());
+               os << cur.selectionAsString(false);
+               os.close();
+               // Since we lock the inset while the external file is edited,
+               // we need to move the cursor outside and clear any selection inside
+               cur.clearSelection();
+               cur.pop();
+               cur.leaveInset(*this);
+               theFormats().edit(buffer(), tempfilename, format);
+               break;
+       }
+       case LFUN_INSET_END_EDIT: {
+               support::FileName const tempfilename = tempfile_->name();
+               docstring const s = tempfilename.fileContents("UTF-8");
+               cur.recordUndoInset(this);
+               cur.push(*this);
+               text().selectAll(cur);
+               cap::replaceSelection(cur);
+               cur.text()->insertStringAsLines(cur, s, cur.current_font);
+               // FIXME (gb) it crashes without this
+               cur.fixIfBroken();
+               tempfile_.reset();
+               cur.pop();
+               break;
+       }
+
        default:
                InsetText::doDispatch(cur, cmd);
                break;
@@ -569,6 +630,14 @@ bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
                        flag.setEnabled(false);
                return true;
 
+       case LFUN_INSET_EDIT:
+               flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
+               return true;
+
+       case LFUN_INSET_END_EDIT:
+               flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
+               return true;
+
        default:
                return InsetText::getStatus(cur, cmd, flag);
        }
@@ -670,23 +739,23 @@ void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
 {
        bool doing_output = output_active && producesOutput();
        InsetLayout const & layout = getLayout();
-       if (layout.addToToc()) {
-               TocBuilder & b = backend.builder(layout.tocType());
-               // Cursor inside the inset
-               DocIterator pit = cpit;
-               pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
-               docstring const label = getLabel();
-               b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
-               // Proceed with the rest of the inset.
-               InsetText::addToToc(cpit, doing_output, utype, backend);
-               if (layout.isTocCaption()) {
-                       docstring str;
-                       text().forOutliner(str, TOC_ENTRY_LENGTH);
-                       b.argumentItem(str);
-               }
-               b.pop();
-       } else
-               InsetText::addToToc(cpit, doing_output, utype, backend);
+       if (!layout.addToToc())
+               return InsetText::addToToc(cpit, doing_output, utype, backend);
+
+       TocBuilder & b = backend.builder(layout.tocType());
+       // Cursor inside the inset
+       DocIterator pit = cpit;
+       pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
+       docstring const label = getLabel();
+       b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
+       // Proceed with the rest of the inset.
+       InsetText::addToToc(cpit, doing_output, utype, backend);
+       if (layout.isTocCaption()) {
+               docstring str;
+               text().forOutliner(str, TOC_ENTRY_LENGTH);
+               b.argumentItem(str);
+       }
+       b.pop();
 }