Here is  the patch.

Objections ?

Vincent

Now the correct one.

Vincent
Index: src/Text3.cpp
===================================================================
--- src/Text3.cpp       (revision 33051)
+++ src/Text3.cpp       (working copy)
@@ -1569,6 +1569,7 @@
        case LFUN_MARGINALNOTE_INSERT:
        case LFUN_OPTIONAL_INSERT:
        case LFUN_INDEX_INSERT:
+       case LFUN_PREVIEW_INSERT:
                // Open the inset, and move the current selection
                // inside it.
                doInsertInset(cur, this, cmd, true, true);
@@ -2335,6 +2336,9 @@
                if (cur.inTexted())
                        code = SPACE_CODE;
                break;
+       case LFUN_PREVIEW_INSERT:
+               code = PREVIEW_CODE;
+               break;
 
        case LFUN_MATH_INSERT:
        case LFUN_MATH_AMS_MATRIX:
Index: src/Makefile.am
===================================================================
--- src/Makefile.am     (revision 33051)
+++ src/Makefile.am     (working copy)
@@ -549,6 +549,7 @@
        insets/InsetNote.cpp \
        insets/InsetOptArg.cpp \
        insets/InsetPhantom.cpp \
+       insets/InsetPreview.cpp \
        insets/InsetQuotes.cpp \
        insets/InsetRef.cpp \
        insets/InsetSpace.cpp \
@@ -591,6 +592,7 @@
        insets/InsetInclude.h \
        insets/InsetIndex.h \
        insets/InsetInfo.h \
+       insets/InsetPreview.h \
        insets/InsetLabel.h \
        insets/InsetLayout.h \
        insets/InsetLine.h \
Index: src/LyXAction.cpp
===================================================================
--- src/LyXAction.cpp   (revision 33051)
+++ src/LyXAction.cpp   (working copy)
@@ -367,6 +367,14 @@
  */
                { LFUN_LISTING_INSERT, "listing-insert", Noop, Edit },
 /*!
+ * \var lyx::FuncCode lyx::LFUN_PREVIEW_INSERT
+ * \li Action: Inserts a new preview inset.
+ * \li Syntax: preview-insert
+ * \li Origin: vfr, 17 Dec 2009
+ * \endvar
+ */
+               { LFUN_PREVIEW_INSERT, "preview-insert", Noop, Edit },
+/*!
  * \var lyx::FuncCode lyx::LFUN_TAB_INSERT
  * \li Action: Insert a tab into a listings inset.
  * \li Notion: It also works on a selection.
Index: src/insets/InsetTabular.cpp
===================================================================
--- src/insets/InsetTabular.cpp (revision 33051)
+++ src/insets/InsetTabular.cpp (working copy)
@@ -4181,6 +4181,7 @@
        case LFUN_BRANCH_INSERT:
        case LFUN_PHANTOM_INSERT:
        case LFUN_WRAP_INSERT:
+       case LFUN_PREVIEW_INSERT:
        case LFUN_ERT_INSERT: {
                if (cur.selIsMultiCell()) {
                        status.setEnabled(false);
Index: src/insets/InsetPreview.h
===================================================================
--- src/insets/InsetPreview.h   (revision 0)
+++ src/insets/InsetPreview.h   (revision 0)
@@ -0,0 +1,81 @@
+// -*- C++ -*-
+/**
+ * \file InsetPreview.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Vincent van Ravesteijn
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef INSETPREVIEW_H
+#define INSETPREVIEW_H
+
+#include "InsetText.h"
+
+#include "Dimension.h"
+#include "RenderPreview.h"
+
+#include "graphics/PreviewLoader.h"
+
+#include <boost/scoped_ptr.hpp>
+
+namespace lyx {
+
+/// An inset with an instant preview
+class InsetPreview : public InsetText {
+       
+public:
+       ///
+       InsetPreview(Buffer *);
+       ///
+       ~InsetPreview();
+       ///
+       InsetPreview(InsetPreview const & other);
+
+       /// \name Methods inherited from Inset class
+       //@{
+       Inset * clone() const { return new InsetPreview(*this); }
+
+       bool neverIndent() const { return true; }
+       
+       InsetCode lyxCode() const { return PREVIEW_CODE; }
+       
+       docstring name() const { return from_ascii("Preview"); }
+       
+       bool descendable(BufferView const & /*bv*/) const { return true; }
+
+       void metrics(MetricsInfo & mi, Dimension & dim) const;
+       Inset * editXY(Cursor & cur, int x, int y);
+       void draw(PainterInfo & pi, int x, int y) const;
+       void addPreview(DocIterator const & inset_pos,
+               graphics::PreviewLoader & ploader) const;
+       bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
+       void write(std::ostream & os) const;
+       void edit(Cursor & cur, bool front, EntryDirection entry_from);
+       //@}
+       
+protected:
+       ///
+       bool previewState(BufferView * bv) const;
+       ///
+       void reloadPreview(DocIterator const & pos) const;
+       ///
+       void preparePreview(DocIterator const & pos) const;
+       ///
+       boost::scoped_ptr<RenderPreview> preview_;
+       ///
+       mutable bool use_preview_;
+
+private:
+       ///
+       mutable Dimension dim_;
+};
+
+
+} // namespace lyx
+
+
+#endif
+
Index: src/insets/InsetPreview.cpp
===================================================================
--- src/insets/InsetPreview.cpp (revision 0)
+++ src/insets/InsetPreview.cpp (revision 0)
@@ -0,0 +1,169 @@
+/**
+ * \file InsetPreview.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Vincent van Ravesteijn
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+#include "config.h"
+
+#include "InsetPreview.h"
+
+#include "Buffer.h"
+#include "BufferParams.h"
+#include "BufferView.h"
+#include "Cursor.h"
+#include "Lexer.h"
+#include "LyXRC.h"
+#include "MetricsInfo.h"
+#include "OutputParams.h"
+
+#include "frontends/Painter.h"
+
+#include "graphics/PreviewImage.h"
+
+#include <sstream>
+
+using namespace std;
+
+namespace lyx {
+
+
+InsetPreview::InsetPreview(Buffer * buf) 
+       : InsetText(buf, InsetText::PlainLayout),
+         preview_(new RenderPreview(this)), use_preview_(true)
+{
+       setAutoBreakRows(true);
+       setDrawFrame(true);
+       setFrameColor(Color_previewframe);
+}
+
+
+InsetPreview::~InsetPreview() 
+{}
+
+
+InsetPreview::InsetPreview(InsetPreview const & other)
+       : InsetText(other)
+{
+       preview_.reset(new RenderPreview(*other.preview_, this));
+}
+
+
+void InsetPreview::write(ostream & os) const
+{
+       os << "Preview" << "\n";
+       text().write(os);
+}
+
+
+void InsetPreview::addPreview(DocIterator const & inset_pos,
+       graphics::PreviewLoader & ploader) const
+{
+       preparePreview(inset_pos);
+}
+
+
+void InsetPreview::preparePreview(DocIterator const & pos) const  
+{
+       odocstringstream str;  
+       OutputParams runparams(&pos.buffer()->params().encoding());
+       latex(str, runparams);
+       docstring const snippet = str.str();
+       preview_->addPreview(snippet, *pos.buffer());  
+}
+
+
+bool InsetPreview::previewState(BufferView * bv) const
+{
+       if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
+               graphics::PreviewImage const * pimage =
+                       preview_->getPreviewImage(bv->buffer());
+               return pimage && pimage->image();
+       }
+       return false;
+}
+
+
+void InsetPreview::reloadPreview(DocIterator const & pos) const
+{
+       preparePreview(pos);
+       preview_->startLoading(*pos.buffer());
+}
+
+
+void InsetPreview::draw(PainterInfo & pi, int x, int y) const
+{
+       use_preview_ = previewState(pi.base.bv);
+
+       if (use_preview_) {
+               // one pixel gap in front
+               preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y);
+               setPosCache(pi, x, y);
+               int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
+               int const yframe = y - dim_.asc;
+               int const w = dim_.width();
+               int const h = dim_.height();
+               pi.pain.rectangle(xframe, yframe, w, h, frameColor());
+               return;
+       }
+       InsetText::draw(pi, x, y);
+}
+
+
+void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
+{
+       cur.push(*this);
+       InsetText::edit(cur, front, entry_from);
+}
+
+
+Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
+{
+       if (use_preview_) {
+               edit(cur, true, ENTRY_DIRECTION_IGNORE);
+               return this;
+       }
+       cur.push(*this);
+       return InsetText::editXY(cur, x, y);
+}
+
+
+void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+       if (previewState(mi.base.bv)) {
+               preview_->metrics(mi, dim);
+               mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
+               
+               dim.wid = max(dim.wid, 4);
+               dim.asc = max(dim.asc, 4);
+               
+               dim.asc += TEXT_TO_INSET_OFFSET;
+               dim.des += TEXT_TO_INSET_OFFSET;
+               dim.wid += TEXT_TO_INSET_OFFSET;
+               dim_ = dim;
+               dim.wid += TEXT_TO_INSET_OFFSET;
+               // insert a one pixel gap in front of the formula
+               dim.wid += 1;
+               // Cache the inset dimension.
+               setDimCache(mi, dim);
+               Dimension dim_dummy;
+               MetricsInfo mi_dummy = mi;
+               InsetText::metrics(mi_dummy, dim_dummy);
+               return;
+       }
+       InsetText::metrics(mi, dim);
+}
+
+
+bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
+{
+       reloadPreview(old);
+       cur.updateFlags(Update::Force);
+       return InsetText::notifyCursorLeaves(old, cur);
+}
+
+
+} // namespace lyx
Index: src/insets/InsetCode.h
===================================================================
--- src/insets/InsetCode.h      (revision 33051)
+++ src/insets/InsetCode.h      (working copy)
@@ -220,6 +220,8 @@
        MATH_MACRO_CODE,
        ///
        ARGUMENT_PROXY_CODE, // 100
+       ///
+       PREVIEW_CODE,
 };
 
 } // namespace lyx
Index: src/FuncCode.h
===================================================================
--- src/FuncCode.h      (revision 33051)
+++ src/FuncCode.h      (working copy)
@@ -443,6 +443,7 @@
        LFUN_GRAPHICS_RELOAD,           // vfr 20090810
        LFUN_SCREEN_SHOW_CURSOR,        // vfr, 20090325
        // 345
+       LFUN_PREVIEW_INSERT,            // vfr, 20091217
 
        LFUN_LASTACTION                 // end of the table
 };
Index: src/factory.cpp
===================================================================
--- src/factory.cpp     (revision 33051)
+++ src/factory.cpp     (working copy)
@@ -47,6 +47,7 @@
 #include "insets/InsetNote.h"
 #include "insets/InsetOptArg.h"
 #include "insets/InsetPhantom.h"
+#include "insets/InsetPreview.h"
 #include "insets/InsetRef.h"
 #include "insets/InsetSpace.h"
 #include "insets/InsetTabular.h"
@@ -224,6 +225,9 @@
                        return inset;
                }
 
+               case LFUN_PREVIEW_INSERT:
+                       return new InsetPreview(buf);
+
                case LFUN_INSET_INSERT: {
                        string const name = cmd.getArg(0);
                        InsetCode code = insetCode(name);
@@ -336,6 +340,11 @@
                                
InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
                                return new InsetVSpace(vspace);
                        }
+
+                       case PREVIEW_CODE:
+                               //InsetPreviewParamas ipp;
+                               
//InsetPreview::string2params(to_utf8(cmd.argument()), ipp);
+                               return new InsetPreview(buf);
                        
                        default:
                                lyxerr << "Inset '" << name << "' not permitted 
with LFUN_INSET_INSERT."
@@ -586,6 +595,8 @@
                        inset.reset(new InsetFloatList(buf));
                } else if (tmptok == "Info") {
                        inset.reset(new InsetInfo(buf));
+               } else if (tmptok == "Preview") {
+                       inset.reset(new InsetPreview(buf));
                } else {
                        lyxerr << "unknown Inset type '" << tmptok
                               << "'" << endl;
Index: src/ColorCode.h
===================================================================
--- src/ColorCode.h     (revision 33051)
+++ src/ColorCode.h     (working copy)
@@ -191,6 +191,8 @@
        Color_buttonhoverbg,
        /// Color used for the pilcrow sign to mark the end of a paragraph
        Color_paragraphmarker,
+       /// Preview frame color
+       Color_previewframe,
 
        // Logical attributes
 
Index: src/Color.cpp
===================================================================
--- src/Color.cpp       (revision 33051)
+++ src/Color.cpp       (working copy)
@@ -240,6 +240,7 @@
        { Color_buttonbg, N_("button background"), "buttonbg", "#dcd2c8", 
"buttonbg" },
        { Color_buttonhoverbg, N_("button background under focus"), 
"buttonhoverbg", "#C7C7CA", "buttonhoverbg" },
        { Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", 
grey80, "paragraphmarker"},
+       { Color_previewframe, N_("preview frame"), "previewframe", black, 
"previewframe"},
        { Color_inherit, N_("inherit"), "inherit", "black", "inherit" },
        { Color_ignore, N_("ignore"), "ignore", "black", "ignore" },
        { Color_ignore, 0, 0, 0, 0 }
Index: lib/ui/stdmenus.inc
===================================================================
--- lib/ui/stdmenus.inc (revision 33051)
+++ lib/ui/stdmenus.inc (working copy)
@@ -363,6 +363,7 @@
                Item "TeX Code|X" "ert-insert"
                Item "Program Listing[[Menu]]" "listing-insert"
                Item "Date" "date-insert"
+               Item "Preview|w" "preview-insert"
        End
 
        Menu "insert_special"
Index: lib/layouts/stdinsets.inc
===================================================================
--- lib/layouts/stdinsets.inc   (revision 33051)
+++ lib/layouts/stdinsets.inc   (working copy)
@@ -408,3 +408,9 @@
        EndHTMLStyle
 End
 
+
+InsetLayout Preview
+       LabelString           Preview   
+       Decoration            minimalistic
+       MultiPar              true
+End
\ No newline at end of file
Index: development/scons/scons_manifest.py
===================================================================
--- development/scons/scons_manifest.py (revision 33051)
+++ development/scons/scons_manifest.py (working copy)
@@ -1041,6 +1041,7 @@
     InsetNote.h
     InsetOptArg.h
     InsetPhantom.h
+    InsetPreview.h
     InsetQuotes.h
     InsetRef.h
     InsetSpace.h
@@ -1096,6 +1097,7 @@
     InsetNote.cpp
     InsetOptArg.cpp
     InsetPhantom.cpp
+    InsetPreview.cpp
     InsetQuotes.cpp
     InsetRef.cpp
     InsetSpace.cpp

Reply via email to