]> git.lyx.org Git - lyx.git/blobdiff - src/lyxtextclass.C
float2string #4 (Spacing)
[lyx.git] / src / lyxtextclass.C
index fb53e5e742d4f3d6588efe4d5474aba29f466f36..168520301dfa345535120f4a6dec85d789450db4 100644 (file)
@@ -32,34 +32,33 @@ using lyx::support::subst;
 using std::endl;
 using std::find_if;
 using std::remove_if;
-
+using std::string;
 using std::ostream;
 
 
-namespace { // anon
-
-struct compare_name {
+namespace {
 
-       compare_name(string const & name)
+class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
+public:
+       LayoutNamesEqual(string const & name)
                : name_(name)
        {}
-
-       bool operator()(boost::shared_ptr<LyXLayout> const & c)
+       bool operator()(LyXLayout_ptr const & c) const
        {
                return c->name() == name_;
        }
-
+private:
        string name_;
-
 };
 
-} // anon
+} // namespace anon
 
 
 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
                           string const & desc, bool texClassAvail )
        : name_(fn), latexname_(cln), description_(desc),
-         floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail)
+         floatlist_(new FloatList), ctrs_(new Counters),
+         texClassAvail_(texClassAvail)
 {
        outputType_ = LATEX;
        columns_ = 1;
@@ -73,7 +72,7 @@ LyXTextClass::LyXTextClass(string const & fn, string const & cln,
        provides_ = nothing;
        titletype_ = TITLE_COMMAND_AFTER;
        titlename_ = "maketitle";
-       loaded = false;
+       loaded_ = false;
 }
 
 
@@ -104,6 +103,7 @@ enum TextClassTags {
        TC_INPUT,
        TC_STYLE,
        TC_DEFAULTSTYLE,
+       TC_CHARSTYLE,
        TC_ENVIRONMENT,
        TC_NOSTYLE,
        TC_COLUMNS,
@@ -131,6 +131,7 @@ enum TextClassTags {
 bool LyXTextClass::Read(string const & filename, bool merge)
 {
        keyword_item textClassTags[] = {
+               { "charstyle",       TC_CHARSTYLE },
                { "classoptions",    TC_CLASSOPTIONS },
                { "columns",         TC_COLUMNS },
                { "counter",         TC_COUNTER },
@@ -233,8 +234,10 @@ bool LyXTextClass::Read(string const & filename, bool merge)
                                        if (le == TC_ENVIRONMENT)
                                                lay.is_environment = true;
                                        if (!(error = do_readStyle(lexrc, lay)))
-                                               layoutlist_.push_back
-                                                       (boost::shared_ptr<LyXLayout>(new LyXLayout(lay)));
+                                               layoutlist_.push_back(
+                                                       boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
+                                                       );
+
                                        if (defaultlayout_.empty()) {
                                                // We do not have a default
                                                // layout yet, so we choose
@@ -344,6 +347,12 @@ bool LyXTextClass::Read(string const & filename, bool merge)
                        if (lexrc.next())
                                rightmargin_ = lexrc.getString();
                        break;
+               case TC_CHARSTYLE:
+                       if (lexrc.next()) {
+                               string const name = subst(lexrc.getString(), '_', ' ');
+                               readCharStyle(lexrc, name);
+                       }
+                       break;
                case TC_FLOAT:
                        readFloat(lexrc);
                        break;
@@ -460,6 +469,7 @@ enum ClassOptionsTags {
        CO_FONTSIZE = 1,
        CO_PAGESTYLE,
        CO_OTHER,
+       CO_HEADER,
        CO_END
 };
 
@@ -469,6 +479,7 @@ void LyXTextClass::readClassOptions(LyXLex & lexrc)
        keyword_item classOptionsTags[] = {
                {"end", CO_END },
                {"fontsize", CO_FONTSIZE },
+               {"header", CO_HEADER },
                {"other", CO_OTHER },
                {"pagestyle", CO_PAGESTYLE }
        };
@@ -496,6 +507,10 @@ void LyXTextClass::readClassOptions(LyXLex & lexrc)
                        lexrc.next();
                        options_ = lexrc.getString();
                        break;
+               case CO_HEADER:
+                       lexrc.next();
+                       class_header_ = subst(lexrc.getString(), "&quot;", "\"");
+                       break;
                case CO_END:
                        getout = true;
                        break;
@@ -504,6 +519,93 @@ void LyXTextClass::readClassOptions(LyXLex & lexrc)
        lexrc.popTable();
 }
 
+enum CharStyleTags {
+       CS_FONT = 1,
+       CS_LABELFONT,
+       CS_LATEXTYPE,
+       CS_LATEXNAME,
+       CS_LATEXPARAM,
+       CS_PREAMBLE,
+       CS_END
+};
+
+
+void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
+{
+       keyword_item elementTags[] = {
+               { "end", CS_END },
+               { "font", CS_FONT },
+               { "labelfont", CS_LABELFONT },
+               { "latexname", CS_LATEXNAME },
+               { "latexparam", CS_LATEXPARAM },
+               { "latextype", CS_LATEXTYPE },
+               { "preamble", CS_PREAMBLE}
+       };
+
+       lexrc.pushTable(elementTags, CS_END);
+
+       string latextype;
+       string latexname;
+       string latexparam;
+       LyXFont font(LyXFont::ALL_INHERIT);
+       LyXFont labelfont(LyXFont::ALL_INHERIT);
+       string preamble;
+
+       bool getout = false;
+       while (!getout && lexrc.isOK()) {
+               int le = lexrc.lex();
+               switch (le) {
+               case LyXLex::LEX_UNDEF:
+                       lexrc.printError("Unknown ClassOption tag `$$Token'");
+                       continue;
+               default: break;
+               }
+               switch (static_cast<CharStyleTags>(le)) {
+               case CS_LATEXTYPE:
+                       lexrc.next();
+                       latextype = lexrc.getString();
+                       break;
+               case CS_LATEXNAME:
+                       lexrc.next();
+                       latexname = lexrc.getString();
+                       break;
+               case CS_LATEXPARAM:
+                       lexrc.next();
+                       latexparam = subst(lexrc.getString(), "&quot;", "\"");
+                       break;
+               case CS_LABELFONT:
+                       labelfont.lyxRead(lexrc);
+                       break;
+               case CS_FONT:
+                       font.lyxRead(lexrc);
+                       labelfont = font;
+                       break;
+               case CS_PREAMBLE:
+                       preamble = lexrc.getLongString("EndPreamble");
+                       break;
+               case CS_END:
+                       getout = true;
+                       break;
+               }
+       }
+
+       //
+       // Here add element to list if getout == true
+       if (getout) {
+               CharStyle cs;
+               cs.name = name;
+               cs.latextype = latextype;
+               cs.latexname = latexname;
+               cs.latexparam = latexparam;
+               cs.font = font;
+               cs.labelfont = labelfont;
+               cs.preamble = preamble;
+               charstyles().push_back(cs);
+       }
+
+       lexrc.popTable();
+}
+
 
 enum FloatTags {
        FT_TYPE = 1,
@@ -517,6 +619,7 @@ enum FloatTags {
        FT_END
 };
 
+
 void LyXTextClass::readFloat(LyXLex & lexrc)
 {
        keyword_item floatTags[] = {
@@ -686,7 +789,7 @@ bool LyXTextClass::hasLayout(string const & n) const
        string const name = (n.empty() ? defaultLayoutName() : n);
 
        return find_if(layoutlist_.begin(), layoutlist_.end(),
-                      compare_name(name))
+                      LayoutNamesEqual(name))
                != layoutlist_.end();
 }
 
@@ -699,7 +802,7 @@ LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
        LayoutList::const_iterator cit =
                find_if(layoutlist_.begin(),
                        layoutlist_.end(),
-                       compare_name(name));
+                       LayoutNamesEqual(name));
 
        if (cit == layoutlist_.end()) {
                lyxerr << "We failed to find the layout '" << name
@@ -725,7 +828,7 @@ bool LyXTextClass::delete_layout(string const & name)
 
        LayoutList::iterator it =
                remove_if(layoutlist_.begin(), layoutlist_.end(),
-                         compare_name(name));
+                         LayoutNamesEqual(name));
 
        LayoutList::iterator end = layoutlist_.end();
        bool const ret = (it != end);
@@ -737,22 +840,22 @@ bool LyXTextClass::delete_layout(string const & name)
 // Load textclass info if not loaded yet
 bool LyXTextClass::load() const
 {
-       if (loaded)
+       if (loaded_)
                return true;
 
        // Read style-file
        string const real_file = LibFileSearch("layouts", name_, "layout");
+       loaded_ = const_cast<LyXTextClass*>(this)->Read(real_file) == 0;
 
-       if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
+       if (!loaded_) {
                lyxerr << "Error reading `"
                       << MakeDisplayPath(real_file)
                       << "'\n(Check `" << name_
                       << "')\nCheck your installation and "
                        "try Options/Reconfigure..." << endl;
-               loaded = false;
        }
-       loaded = true;
-       return loaded;
+
+       return loaded_;
 }
 
 
@@ -774,6 +877,18 @@ Counters & LyXTextClass::counters() const
 }
 
 
+CharStyles::iterator LyXTextClass::charstyle(string const & s) const
+{
+       CharStyles::iterator cs = charstyles().begin();
+       CharStyles::iterator csend = charstyles().end();
+       for (; cs != csend; ++cs) {
+               if (cs->name == s)
+                       return cs;
+       }
+       return csend;
+}
+
+
 string const & LyXTextClass::defaultLayoutName() const
 {
        // This really should come from the actual layout... (Lgb)
@@ -824,6 +939,12 @@ string const & LyXTextClass::options() const
 }
 
 
+string const & LyXTextClass::class_header() const
+{
+       return class_header_;
+}
+
+
 string const & LyXTextClass::pagestyle() const
 {
        return pagestyle_;