Will this be usable to do the endnote thing I had in mind previously? I.e., something like:

+InsetLayout Endnote
+       LabelString           endnote
+       LatexType             command
+       LatexName             endnote
+       Preamble
+               \usepackage{endnote}
+       EndPreamble
+End

Will that work by itself? Or will we also need to create an InsetEndnote class to handle it?

Richard

Martin Vermeer wrote:
The attached patch is the beginning of trying to make the
insets as configurable through the layout files as currently
paragrapshs are. It tries to more generally do what only
CharStyles are doing now. So Richard won't have to abuse CS
in the future...

The idea is that this will in the long run replace the
CS functionality. Currently only foot- and marginal notes
and note insets are done, sort-of. It's only a demonstration of princple offered here for review, but once in, it can be
easily extended.

Lots of help from Jean-Marc on this.

- Martin

------------------------------------------------------------------------

Index: src/TextClass.cpp
===================================================================
--- src/TextClass.cpp   (revision 19456)
+++ src/TextClass.cpp   (working copy)
@@ -150,6 +150,7 @@
        TC_STYLE,
        TC_DEFAULTSTYLE,
        TC_CHARSTYLE,
+       TC_INSETLAYOUT,
        TC_ENVIRONMENT,
        TC_NOSTYLE,
        TC_COLUMNS,
@@ -192,6 +193,7 @@
                { "float",           TC_FLOAT },
                { "format",          TC_FORMAT },
                { "input",           TC_INPUT },
+               { "insetlayout",     TC_INSETLAYOUT },
                { "leftmargin",      TC_LEFTMARGIN },
                { "nofloat",         TC_NOFLOAT },
                { "nostyle",         TC_NOSTYLE },
@@ -411,6 +413,12 @@
                                readCharStyle(lexrc, name);
                        }
                        break;
+               case TC_INSETLAYOUT:
+                       if (lexrc.next()) {
+                               docstring const name = 
subst(lexrc.getDocString(), '_', ' ');
+                               readInsetLayout(lexrc, name);
+                       }
+                       break;
                case TC_FLOAT:
                        readFloat(lexrc);
                        break;
@@ -606,6 +614,19 @@
 };
+enum InsetLayoutTags {
+       IL_FONT = 1,
+       IL_LABELFONT,
+       IL_LABELSTRING,
+       IL_LATEXTYPE,
+       IL_LATEXNAME,
+       IL_LATEXPARAM,
+       IL_PREAMBLE,
+       IL_END
+};
+
+
+
 void TextClass::readCharStyle(Lexer & lexrc, string const & name)
 {
        keyword_item elementTags[] = {
@@ -683,6 +704,92 @@
 }
+void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
+{
+       keyword_item elementTags[] = {
+               { "end", IL_END },
+               { "font", IL_FONT },
+               { "labelfont", IL_LABELFONT },
+               { "labelstring", IL_LABELSTRING },
+               { "latexname", IL_LATEXNAME },
+               { "latexparam", IL_LATEXPARAM },
+               { "latextype", IL_LATEXTYPE },
+               { "preamble", IL_PREAMBLE}
+       };
+
+       lexrc.pushTable(elementTags, IL_END);
+
+       docstring labelstring;
+       string latextype;
+       string latexname;
+       string latexparam;
+       Font font(Font::ALL_INHERIT);
+       Font labelfont(Font::ALL_INHERIT);
+       string preamble;
+
+       bool getout = false;
+       while (!getout && lexrc.isOK()) {
+               int le = lexrc.lex();
+               switch (le) {
+               case Lexer::LEX_UNDEF:
+                       lexrc.printError("Unknown ClassOption tag `$$Token'");
+                       continue;
+               default: break;
+               }
+               switch (static_cast<InsetLayoutTags>(le)) {
+               case IL_LATEXTYPE:
+                       lexrc.next();
+                       latextype = lexrc.getString();
+                       break;
+               case IL_LABELSTRING:
+                       lexrc.next();
+                       labelstring = lexrc.getDocString();
+                       break;
+               case IL_LATEXNAME:
+                       lexrc.next();
+                       latexname = lexrc.getString();
+                       break;
+               case IL_LATEXPARAM:
+                       lexrc.next();
+                       latexparam = subst(lexrc.getString(), "&quot;", "\"");
+                       break;
+               case IL_LABELFONT:
+                       labelfont.lyxRead(lexrc);
+                       labelfont.realize(defaultfont());
+                       break;
+               case IL_FONT:
+                       font.lyxRead(lexrc);
+                       font.realize(defaultfont());
+                       labelfont = font;
+                       break;
+               case IL_PREAMBLE:
+                       preamble = lexrc.getLongString("EndPreamble");
+                       break;
+               case IL_END:
+                       getout = true;
+                       break;
+               }
+       }
+
+       //
+       // Here add element to list if getout == true
+       if (getout) {
+               InsetLayout il;
+               il.labelstring = labelstring;
+               il.latextype = latextype;
+               il.latexname = latexname;
+               il.latexparam = latexparam;
+               il.font = font;
+               il.labelfont = labelfont;
+               il.preamble = from_utf8(preamble);
+               insetlayoutlist_[name] = il;
+       }
+
+       lexrc.popTable();
+}
+
+
+
 enum FloatTags {
        FT_TYPE = 1,
        FT_NAME,
@@ -955,7 +1062,12 @@
        return *ctrs_.get();
 }
+InsetLayout const & TextClass::insetlayout(docstring const & name) const +{ + return insetlayoutlist_[name]; +} +
 CharStyles::iterator TextClass::charstyle(string const & s) const
 {
        CharStyles::iterator cs = charstyles().begin();
Index: src/insets/InsetFoot.cpp
===================================================================
--- src/insets/InsetFoot.cpp    (revision 19456)
+++ src/insets/InsetFoot.cpp    (working copy)
@@ -32,15 +32,13 @@
 InsetFoot::InsetFoot(BufferParams const & bp)
        : InsetFootlike(bp)
 {
-       setLabel(_("foot"));
+       setLayout(bp);
 }
InsetFoot::InsetFoot(InsetFoot const & in)
        : InsetFootlike(in)
-{
-       setLabel(_("foot"));
-}
+{}
auto_ptr<Inset> InsetFoot::doClone() const
Index: src/insets/Inset.cpp
===================================================================
--- src/insets/Inset.cpp        (revision 19456)
+++ src/insets/Inset.cpp        (working copy)
@@ -17,6 +17,7 @@
 #include "Inset.h"
#include "Buffer.h"
+#include "BufferParams.h"
 #include "BufferView.h"
 #include "Color.h"
 #include "CoordCache.h"
@@ -29,6 +30,7 @@
 #include "FuncStatus.h"
 #include "gettext.h"
 #include "Text.h"
+#include "TextClass.h"
 #include "MetricsInfo.h"
 #include "MetricsInfo.h"
@@ -344,6 +346,12 @@
 }
+InsetLayout const & Inset::getLayout(BufferParams const & bp) const
+{
+ return bp.getTextClass().insetlayout(name()); +}
+
+
 void Inset::dump() const
 {
        Buffer buf("foo", 1);
Index: src/insets/InsetMarginal.cpp
===================================================================
--- src/insets/InsetMarginal.cpp        (revision 19456)
+++ src/insets/InsetMarginal.cpp        (working copy)
@@ -30,15 +30,13 @@
 InsetMarginal::InsetMarginal(BufferParams const & bp)
        : InsetFootlike(bp)
 {
-       setLabel(_("margin"));
+       setLayout(bp);
 }
InsetMarginal::InsetMarginal(InsetMarginal const & in)
        : InsetFootlike(in)
-{
-       setLabel(_("margin"));
-}
+{}
auto_ptr<Inset> InsetMarginal::doClone() const
Index: src/insets/InsetNote.cpp
===================================================================
--- src/insets/InsetNote.cpp    (revision 19456)
+++ src/insets/InsetNote.cpp    (working copy)
@@ -16,6 +16,7 @@
#include "Buffer.h"
 #include "BufferView.h"
+#include "BufferParams.h"
 #include "Cursor.h"
 #include "debug.h"
 #include "DispatchResult.h"
@@ -109,24 +110,19 @@
 }
-void InsetNote::init()
-{
-       setButtonLabel();
-}
-
-
 InsetNote::InsetNote(BufferParams const & bp, string const & label)
        : InsetCollapsable(bp)
 {
        params_.type = notetranslator().find(label);
-       init();
+       setLayout(bp);
+       setButtonLabel();
 }
InsetNote::InsetNote(InsetNote const & in)
        : InsetCollapsable(in), params_(in.params_)
 {
-       init();
+       setButtonLabel();
 }
@@ -148,6 +144,12 @@
 }
+docstring InsetNote::name() const +{
+       return from_ascii(string("Note") + string(":") + 
string(notetranslator().find(params_.type)));
+}
+
+
 Inset::DisplayType InsetNote::display() const
 {
        switch (params_.type) {
@@ -171,6 +173,7 @@
 {
        params_.read(lex);
        InsetCollapsable::read(buf, lex);
+       setLayout(buf.params());
        setButtonLabel();
 }
@@ -179,31 +182,7 @@
 {
        docstring const label = notetranslator_loc().find(params_.type);
        setLabel(label);
-
-       Font font(Font::ALL_SANE);
-       font.decSize();
-       font.decSize();
-
-       Color_color c;
-       switch (params_.type) {
-       case InsetNoteParams::Note:
-               c = Color::note;
-               break;
-       case InsetNoteParams::Comment:
-               c = Color::comment;
-               break;
-       case InsetNoteParams::Greyedout:
-               c = Color::greyedout;
-               break;
-       case InsetNoteParams::Framed:
-               c = Color::greyedout;
-               break;
-       case InsetNoteParams::Shaded:
-               c = Color::greyedout;
-               break;
-       }
-       font.setColor(c);
-       setLabelFont(font);
+       setLabelFont(layout_.labelfont);
 }
@@ -244,6 +223,8 @@ case LFUN_INSET_MODIFY:
                InsetNoteMailer::string2params(to_utf8(cmd.argument()), 
params_);
+               // get a bp from cur:
+               setLayout(cur.buffer().params());
                setButtonLabel();
                break;
Index: src/insets/Inset.h
===================================================================
--- src/insets/Inset.h  (revision 19456)
+++ src/insets/Inset.h  (working copy)
@@ -32,6 +32,7 @@
 class CursorSlice;
 class FuncRequest;
 class FuncStatus;
+class InsetLayout;
 class InsetMath;
 class InsetText;
 class LaTeXFeatures;
@@ -378,6 +379,8 @@
///
        virtual docstring name() const { return from_ascii("unknown"); }
+       ///
+       virtual InsetLayout const & getLayout(BufferParams const & bp) const;
        /// used to toggle insets
        /// is the inset open?
        /// should this inset be handled like a normal charater
Index: src/insets/InsetNote.h
===================================================================
--- src/insets/InsetNote.h      (revision 19456)
+++ src/insets/InsetNote.h      (working copy)
@@ -52,7 +52,7 @@
        ///
        Inset::Code lyxCode() const { return Inset::NOTE_CODE; }
        ///
-       docstring name() const { return from_ascii("Note"); }
+       docstring name() const;
        /// framed and shaded notes are displayed
        virtual DisplayType display() const;
        ///
Index: src/insets/InsetFootlike.cpp
===================================================================
--- src/insets/InsetFootlike.cpp        (revision 19456)
+++ src/insets/InsetFootlike.cpp        (working copy)
@@ -17,29 +17,16 @@
 #include "BufferParams.h"
 #include "MetricsInfo.h"
-
 namespace lyx {
InsetFootlike::InsetFootlike(BufferParams const & bp)
        : InsetCollapsable(bp)
-{
-       Font font(Font::ALL_SANE);
-       font.decSize();
-       font.decSize();
-       font.setColor(Color::collapsable);
-       setLabelFont(font);
-}
+{}
InsetFootlike::InsetFootlike(InsetFootlike const & in)
        : InsetCollapsable(in)
-{
-       Font font(Font::ALL_SANE);
-       font.decSize();
-       font.decSize();
-       font.setColor(Color::collapsable);
-       setLabelFont(font);
-}
+{}
bool InsetFootlike::metrics(MetricsInfo & mi, Dimension & dim) const
Index: src/insets/InsetCollapsable.cpp
===================================================================
--- src/insets/InsetCollapsable.cpp     (revision 19456)
+++ src/insets/InsetCollapsable.cpp     (working copy)
@@ -63,7 +63,7 @@
InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
        : InsetText(rhs),
-               labelfont_(rhs.labelfont_),
+               layout_(rhs.layout_),
                button_dim(rhs.button_dim),
                topx(rhs.topx),
                topbaseline(rhs.topbaseline),
@@ -78,6 +78,13 @@
 }
+void InsetCollapsable::setLayout(BufferParams const & bp)
+{
+       setLabelFont(getLayout(bp).labelfont);
+       setLabel(getLayout(bp).labelstring);
+}
+
+
 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
 {
        os << "status ";
@@ -141,7 +148,7 @@
 Dimension InsetCollapsable::dimensionCollapsed() const
 {
        Dimension dim;
-       theFontMetrics(labelfont_).buttonText(
+       theFontMetrics(layout_.labelfont).buttonText(
                label, dim.wid, dim.asc, dim.des);
        return dim;
 }
@@ -206,7 +213,7 @@
                button_dim.y1 = top;
                button_dim.y2 = top + dimc.height();
- pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_);
+               pi.pain.buttonText(xx, top + dimc.asc, label, 
layout_.labelfont, mouse_hover_);
if (status() == Open) {
                        int textx, texty;
@@ -448,9 +455,9 @@
 }
-void InsetCollapsable::setLabelFont(Font & font)
+void InsetCollapsable::setLabelFont(Font const & font)
 {
-       labelfont_ = font;
+       layout_.labelfont = font;
 }
docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
Index: src/insets/InsetCollapsable.h
===================================================================
--- src/insets/InsetCollapsable.h       (revision 19456)
+++ src/insets/InsetCollapsable.h       (working copy)
@@ -16,6 +16,7 @@
#include "Inset.h"
 #include "InsetText.h"
+#include "TextClass.h"
#include "Box.h"
 #include "Font.h"
@@ -27,6 +28,7 @@
 class Text;
 class Paragraph;
 class CursorSlice;
+class InsetLayout;
namespace frontend { class Painter; } @@ -46,6 +48,8 @@
        ///
        docstring name() const { return from_ascii("Collapsable"); }
        ///
+       void setLayout(BufferParams const &);
+       ///
        void read(Buffer const &, Lexer &);
        ///
        void write(Buffer const &, std::ostream &) const;
@@ -71,7 +75,7 @@
        ///
        virtual void setButtonLabel() {}
        ///
-       void setLabelFont(Font & f);
+       void setLabelFont(Font const & f);
        ///
        bool isOpen() const { return status_ == Open || status_ == Inlined; }
        ///
@@ -105,8 +109,6 @@
protected:
        ///
-       Font labelfont_;
-       ///
        mutable Box button_dim;
        ///
        mutable int topx;
@@ -114,6 +116,8 @@
        mutable int topbaseline;
        ///
        mutable docstring label;
+       ///
+       mutable InsetLayout layout_;
 private:
        ///
        mutable CollapseStatus status_;
Index: src/TextClass.h
===================================================================
--- src/TextClass.h     (revision 19456)
+++ src/TextClass.h     (working copy)
@@ -17,6 +17,7 @@
#include <vector>
 #include <set>
+#include <map>
namespace lyx { @@ -40,9 +41,23 @@
 };
+class InsetLayout {
+public:
+       docstring labelstring;
+       std::string latextype;
+       std::string latexname;
+       std::string latexparam;
+       Font font;
+       Font labelfont;
+       docstring preamble;
+};
+
+
 /// List of semantically defined character style insets
 typedef std::vector<CharStyle> CharStyles;
+/// List of inset layouts
+typedef std::map<docstring, InsetLayout> InsetLayouts;
/// Stores the layout specification of a LyX document class.
 class TextClass {
@@ -79,6 +94,8 @@
        ///
        void readCharStyle(Lexer &, std::string const &);
        ///
+       void readInsetLayout(Lexer &, docstring const &);
+       ///
        void readFloat(Lexer &);
        ///
        void readCounter(Lexer &);
@@ -101,6 +118,8 @@
        Counters & counters() const;
        /// CharStyles of this doc class
        CharStyles & charstyles() const { return charstylelist_; };
+       ///  Inset layouts of this doc class
+       InsetLayout const & insetlayout(docstring const & name) const;
        /// Retrieve element of name s:
        CharStyles::iterator charstyle(std::string const & s) const;
        ///
@@ -110,6 +129,8 @@
        ///
        std::string const & name() const;
        ///
+       docstring const & labelstring() const;
+       ///
        std::string const & latexname() const;
        ///
        std::string const & description() const;
@@ -229,6 +250,9 @@
        /// CharStyles available to this layout
        mutable CharStyles charstylelist_;
+ /// Input layouts available to this layout
+       mutable InsetLayouts insetlayoutlist_;
+
        /// available types of float, eg. figure, algorithm.
        boost::shared_ptr<FloatList> floatlist_;
Index: lib/layouts/stdinsets.inc
===================================================================
--- lib/layouts/stdinsets.inc   (revision 0)
+++ lib/layouts/stdinsets.inc   (revision 0)
@@ -0,0 +1,78 @@
+# Textclass definition file for LaTeX.
+# Author : Martin vermeer <[EMAIL PROTECTED]>
+# Inset layouts definition
+
+Format 2
+
+InsetLayout Marginal
+       LabelString           margin
+       LatexType             command
+       LatexName             marginpar
+       LabelFont
+         Color               Red
+         Size                Small
+       EndFont
+End
+
+InsetLayout Foot
+       LabelString           foot
+       LatexType             command
+       LatexName             footnote
+       LabelFont
+         Color               Green
+         Size                Small
+       EndFont
+End
+
+InsetLayout Note:Comment
+       LabelString           comment
+       LatexType             environment
+       LatexName             comment
+       LabelFont
+         Color               Magenta
+         Size                Small
+       EndFont
+End
+
+
+InsetLayout Note:Note
+       LabelString           note
+       LatexType             command
+       LatexName             note
+       LabelFont
+         Color               Blue
+         Size                Small
+       EndFont
+End
+
+InsetLayout Note:Greyedout
+       LabelString           greyedout
+       LatexType             command
+       LatexName             greyedout
+       LabelFont
+         Color               Red
+         Size                Small
+       EndFont
+End
+
+InsetLayout Note:Framed
+       LabelString           framed
+       LatexType             command
+       LatexName             framed
+       LabelFont
+         Color               Red
+         Size                Small
+       EndFont
+End
+
+InsetLayout Note:Shaded
+       LabelString           shaded
+       LatexType             command
+       LatexName             shaded
+       LabelFont
+         Color               Red
+         Size                Small
+       EndFont
+End
+
+
Index: lib/layouts/scrclass.inc
===================================================================
--- lib/layouts/scrclass.inc    (revision 19456)
+++ lib/layouts/scrclass.inc    (working copy)
@@ -19,6 +19,7 @@
        AlignPossible         Block, Left, Right, Center
 End
+Input stdinsets.inc
 Input stdlists.inc
 Input stdcounters.inc
 Input stdfloats.inc
Index: lib/layouts/stdclass.inc
===================================================================
--- lib/layouts/stdclass.inc    (revision 19456)
+++ lib/layouts/stdclass.inc    (working copy)
@@ -37,6 +37,7 @@
 End
+Input stdinsets.inc
 Input stdlists.inc
 Input stdsections.inc
 Input stdstarsections.inc


--
==================================================================
Richard G Heck, Jr
Professor of Philosophy
Brown University
http://frege.brown.edu/heck/
==================================================================
Get my public key from http://sks.keyserver.penguin.de
Hash: 0x1DE91F1E66FFBDEC
Learn how to sign your email using Thunderbird and GnuPG at:
http://dudu.dyn.2-h.org/nist/gpg-enigmail-howto

Reply via email to