]> git.lyx.org Git - lyx.git/blobdiff - src/TextClass.cpp
Cleanup mouse/selection/context-menu interactions.
[lyx.git] / src / TextClass.cpp
index dd94d3aa9a33aba4e1e7983f075832c19272cfab..3c2afbce3f93c267041c37ee55c9370ec4c0a206 100644 (file)
@@ -16,7 +16,7 @@
 
 #include "TextClass.h"
 
-#include "BaseClassList.h"
+#include "LayoutFile.h"
 #include "Color.h"
 #include "Counters.h"
 #include "Floating.h"
 #include "support/lstrings.h"
 #include "support/os.h"
 
+#include <algorithm>
+#include <fstream>
 #include <sstream>
 
+#include "boost/assert.hpp"
+
 using namespace std;
 using namespace lyx::support;
 
@@ -44,21 +48,21 @@ namespace lyx {
 
 namespace {
 
-class LayoutNamesEqual : public unary_function<LayoutPtr, bool> {
+class LayoutNamesEqual : public unary_function<Layout, bool> {
 public:
        LayoutNamesEqual(docstring const & name)
                : name_(name)
        {}
-       bool operator()(LayoutPtr const & c) const
+       bool operator()(Layout const & c) const
        {
-               return c->name() == name_;
+               return c.name() == name_;
        }
 private:
        docstring name_;
 };
 
 
-int const FORMAT = 6;
+int const FORMAT = 7;
 
 
 bool layout2layout(FileName const & filename, FileName const & tempfile)
@@ -98,6 +102,8 @@ std::string translateRT(TextClass::ReadType rt)
                return "input file";
        case TextClass::MODULE:
                return "module file";
+       case TextClass::VALIDATION:
+               return "validation";
        }
        // shutup warning
        return string();
@@ -106,7 +112,7 @@ std::string translateRT(TextClass::ReadType rt)
 } // namespace anon
 
 
-docstring const TextClass::emptylayout_ = from_ascii("PlainLayout");
+docstring const TextClass::emptylayout_ = from_ascii(N_("Plain Layout"));
 
 
 InsetLayout DocumentClass::empty_insetlayout_;
@@ -114,8 +120,6 @@ InsetLayout DocumentClass::empty_insetlayout_;
 
 TextClass::TextClass()
 {
-       floatlist_ = boost::shared_ptr<FloatList>(new FloatList);
-       counters_ = boost::shared_ptr<Counters>(new Counters);
        outputType_ = LATEX;
        columns_ = 1;
        sides_ = OneSide;
@@ -128,9 +132,6 @@ TextClass::TextClass()
        titletype_ = TITLE_COMMAND_AFTER;
        titlename_ = "maketitle";
        loaded_ = false;
-       // a hack to make this available for translation
-       // i'm sure there must be a better way (rgh)
-       _("PlainLayout");
 }
 
 
@@ -175,20 +176,15 @@ enum TextClassTags {
        TC_NOFLOAT,
        TC_TITLELATEXNAME,
        TC_TITLELATEXTYPE,
-       TC_FORMAT
+       TC_FORMAT,
+       TC_ADDTOPREAMBLE
 };
 
 
-// Reads a textclass structure from file.
-bool TextClass::read(FileName const & filename, ReadType rt)
-{
-       if (!filename.isReadableFile()) {
-               lyxerr << "Cannot read layout file `" << filename << "'."
-                      << endl;
-               return false;
-       }
+namespace {
 
        keyword_item textClassTags[] = {
+               { "addtopreamble",   TC_ADDTOPREAMBLE },
                { "classoptions",    TC_CLASSOPTIONS },
                { "columns",         TC_COLUMNS },
                { "counter",         TC_COUNTER },
@@ -215,6 +211,28 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                { "titlelatextype",  TC_TITLELATEXTYPE },
                { "tocdepth",        TC_TOCDEPTH }
        };
+       
+} //namespace anon
+
+
+bool TextClass::convertLayoutFormat(support::FileName const & filename, ReadType rt)
+{
+       LYXERR(Debug::TCLASS, "Converting layout file to " << FORMAT);
+               FileName const tempfile = FileName::tempName();
+               bool success = layout2layout(filename, tempfile);
+               if (success)
+                       success = read(tempfile, rt);
+               tempfile.removeFile();
+               return success;
+}
+
+bool TextClass::read(FileName const & filename, ReadType rt)
+{
+       if (!filename.isReadableFile()) {
+               lyxerr << "Cannot read layout file `" << filename << "'."
+                      << endl;
+               return false;
+       }
 
        LYXERR(Debug::TCLASS, "Reading " + translateRT(rt) + ": " +
                to_utf8(makeDisplayPath(filename.absFilename())));
@@ -222,7 +240,7 @@ bool TextClass::read(FileName const & filename, ReadType rt)
        // Define the `empty' layout used in table cells, ert, etc. Note that 
        // we do this before loading any layout file, so that classes can 
        // override features of this layout if they should choose to do so.
-       if (rt == BASECLASS) {
+       if (rt == BASECLASS && !hasLayout(emptylayout_)) {
                static char const * s = "Margin Static\n"
                        "LatexType Paragraph\n"
                        "LatexName dummy\n"
@@ -239,14 +257,74 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                        // The only way this happens is because the hardcoded layout above
                        // is wrong.
                        BOOST_ASSERT(false);
-               }
-               layoutlist_.push_back(boost::shared_ptr<Layout>(new Layout(lay)));
+               };
+               layoutlist_.push_back(lay);
        }
-
        Lexer lexrc(textClassTags,
                sizeof(textClassTags) / sizeof(textClassTags[0]));
 
        lexrc.setFile(filename);
+       ReturnValues retval = read(lexrc, rt);
+       
+       LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " +
+                       to_utf8(makeDisplayPath(filename.absFilename())));
+       
+       if (retval != FORMAT_MISMATCH) 
+               return retval == OK;
+       
+       bool const worx = convertLayoutFormat(filename, rt);
+       if (!worx) {
+               lyxerr << "Unable to convert " << filename << 
+                       " to format " << FORMAT << std::endl;
+               return false;
+       }
+       return true;
+}
+
+
+bool TextClass::validate(std::string const & str)
+{
+       TextClass tc;
+       return tc.read(str, VALIDATION);
+}
+
+
+bool TextClass::read(std::string const & str, ReadType rt) 
+{
+       Lexer lexrc(textClassTags,
+               sizeof(textClassTags) / sizeof(textClassTags[0]));
+       istringstream is(str);
+       lexrc.setStream(is);
+       ReturnValues retval = read(lexrc, rt);
+
+       if (retval != FORMAT_MISMATCH) 
+               return retval == OK;
+
+       // write the layout string to a temporary file
+       FileName const tempfile = FileName::tempName();
+       ofstream os(tempfile.toFilesystemEncoding().c_str());
+       if (!os) {
+               lyxerr << "Unable to create tempoary file in TextClass::read!!" 
+                       << std::endl;
+               return false;
+       }
+       os << str;
+       os.close();
+
+       // now try to convert it
+       bool const worx = convertLayoutFormat(tempfile, rt);
+       if (!worx) {
+               lyxerr << "Unable to convert internal layout information to format " 
+                       << FORMAT << std::endl;
+       }
+       tempfile.removeFile();
+       return worx;
+}
+
+
+// Reads a textclass structure from file.
+TextClass::ReturnValues TextClass::read(Lexer & lexrc, ReadType rt) 
+{
        bool error = !lexrc.isOK();
 
        // Format of files before the 'Format' tag was introduced
@@ -315,13 +393,12 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                                                + lexrc.getString() + " is probably not valid UTF-8!";
                                        lexrc.printError(s.c_str());
                                        Layout lay;
-                                       //FIXME If we're just dropping this layout, do we really
-                                       //care whether there's an error?? Or should we just set
-                                       //error to true, since we couldn't even read the name?
+                                       // Since we couldn't read the name, we just scan the rest
+                                       // of the style and discard it.
                                        error = !readStyle(lexrc, lay);
                                } else if (hasLayout(name)) {
-                                       Layout * lay = operator[](name).get();
-                                       error = !readStyle(lexrc, *lay);
+                                       Layout & lay = operator[](name);
+                                       error = !readStyle(lexrc, lay);
                                } else {
                                        Layout lay;
                                        lay.setName(name);
@@ -329,7 +406,7 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                                                lay.is_environment = true;
                                        error = !readStyle(lexrc, lay);
                                        if (!error)
-                                               layoutlist_.push_back(boost::shared_ptr<Layout>(new Layout(lay)));
+                                               layoutlist_.push_back(lay);
 
                                        if (defaultlayout_.empty()) {
                                                // We do not have a default layout yet, so we choose
@@ -411,6 +488,10 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                        preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
                        break;
 
+               case TC_ADDTOPREAMBLE:
+                       preamble_ += from_utf8(lexrc.getLongString("EndPreamble"));
+                       break;
+
                case TC_PROVIDES: {
                        lexrc.next();
                        string const feature = lexrc.getString();
@@ -470,7 +551,7 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                case TC_NOFLOAT:
                        if (lexrc.next()) {
                                string const nofloat = lexrc.getString();
-                               floatlist_->erase(nofloat);
+                               floatlist_.erase(nofloat);
                        }
                        break;
                }
@@ -481,22 +562,11 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                        break;
        }
 
-       if (format != FORMAT) {
-               LYXERR(Debug::TCLASS, "Converting layout file from format "
-                                     << format << " to " << FORMAT);
-               FileName const tempfile = FileName::tempName();
-               bool success = layout2layout(filename, tempfile);
-               if (success)
-                        read(tempfile, rt);
-               tempfile.removeFile();
-               return success;
-       }
-
-       LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " +
-                       to_utf8(makeDisplayPath(filename.absFilename())));
+       if (format != FORMAT)
+               return FORMAT_MISMATCH;
 
        if (rt != BASECLASS) 
-               return !error;
+               return (error ? ERROR : OK);
 
        if (defaultlayout_.empty()) {
                lyxerr << "Error: Textclass '" << name_
@@ -531,11 +601,10 @@ bool TextClass::read(FileName const & filename, ReadType rt)
 
        min_toclevel_ = Layout::NOT_IN_TOC;
        max_toclevel_ = Layout::NOT_IN_TOC;
-       DocumentClass::const_iterator lit = begin();
-       DocumentClass::const_iterator len = end();
+       const_iterator lit = begin();
+       const_iterator len = end();
        for (; lit != len; ++lit) {
-               Layout const & lt = **lit;
-               int const toclevel = lt.toclevel;
+               int const toclevel = lit->toclevel;
                if (toclevel != Layout::NOT_IN_TOC) {
                        if (min_toclevel_ == Layout::NOT_IN_TOC)
                                min_toclevel_ = toclevel;
@@ -547,7 +616,7 @@ bool TextClass::read(FileName const & filename, ReadType rt)
        LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
                << ", maximum is " << max_toclevel_);
 
-       return !error;
+       return (error ? ERROR : OK);
 }
 
 
@@ -713,8 +782,8 @@ void TextClass::readFloat(Lexer & lexrc)
                case FT_TYPE:
                        lexrc.next();
                        type = lexrc.getString();
-                       if (floatlist_->typeExist(type)) {
-                               Floating const & fl = floatlist_->getType(type);
+                       if (floatlist_.typeExist(type)) {
+                               Floating const & fl = floatlist_.getType(type);
                                placement = fl.placement();
                                ext = fl.ext();
                                within = fl.within();
@@ -764,13 +833,13 @@ void TextClass::readFloat(Lexer & lexrc)
        if (getout) {
                Floating fl(type, placement, ext, within,
                            style, name, listName, builtin);
-               floatlist_->newFloat(fl);
+               floatlist_.newFloat(fl);
                // each float has its own counter
-               counters_->newCounter(from_ascii(type), from_ascii(within),
+               counters_.newCounter(from_ascii(type), from_ascii(within),
                                      docstring(), docstring());
                // also define sub-float counters
                docstring const subtype = "sub-" + from_ascii(type);
-               counters_->newCounter(subtype, from_ascii(type),
+               counters_.newCounter(subtype, from_ascii(type),
                                      "\\alph{" + subtype + "}", docstring());
        }
 
@@ -817,7 +886,7 @@ void TextClass::readCounter(Lexer & lexrc)
                case CT_NAME:
                        lexrc.next();
                        name = lexrc.getDocString();
-                       if (counters_->hasCounter(name))
+                       if (counters_.hasCounter(name))
                                LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
                        else
                                LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
@@ -845,7 +914,7 @@ void TextClass::readCounter(Lexer & lexrc)
 
        // Here if have a full counter if getout == true
        if (getout)
-               counters_->newCounter(name, within, 
+               counters_.newCounter(name, within, 
                                      labelstring, labelstring_appendix);
 
        lexrc.popTable();
@@ -863,28 +932,46 @@ bool TextClass::hasLayout(docstring const & n) const
 
 
 
-LayoutPtr const & TextClass::operator[](docstring const & name) const
+Layout const & TextClass::operator[](docstring const & name) const
 {
        BOOST_ASSERT(!name.empty());
 
-       LayoutList::const_iterator cit =
-               find_if(layoutlist_.begin(),
-                       layoutlist_.end(),
-                       LayoutNamesEqual(name));
+       const_iterator it = 
+               find_if(begin(), end(), LayoutNamesEqual(name));
 
-       if (cit == layoutlist_.end()) {
+       if (it == end()) {
                lyxerr << "We failed to find the layout '" << to_utf8(name)
                       << "' in the layout list. You MUST investigate!"
                       << endl;
-               for (LayoutList::const_iterator it = layoutlist_.begin();
-                        it != layoutlist_.end(); ++it)
-                       lyxerr  << " " << to_utf8(it->get()->name()) << endl;
+               for (const_iterator cit = begin(); cit != end(); ++cit)
+                       lyxerr  << " " << to_utf8(cit->name()) << endl;
 
                // we require the name to exist
                BOOST_ASSERT(false);
        }
 
-       return *cit;
+       return *it;
+}
+
+
+Layout & TextClass::operator[](docstring const & name)
+{
+       BOOST_ASSERT(!name.empty());
+
+       iterator it = find_if(begin(), end(), LayoutNamesEqual(name));
+
+       if (it == end()) {
+               lyxerr << "We failed to find the layout '" << to_utf8(name)
+                      << "' in the layout list. You MUST investigate!"
+                      << endl;
+               for (const_iterator cit = begin(); cit != end(); ++cit)
+                       lyxerr  << " " << to_utf8(cit->name()) << endl;
+
+               // we require the name to exist
+               BOOST_ASSERT(false);
+       }
+
+       return *it;
 }
 
 
@@ -933,9 +1020,11 @@ bool TextClass::load(string const & path) const
 InsetLayout const & DocumentClass::insetLayout(docstring const & name) const 
 {
        docstring n = name;
+       InsetLayouts::const_iterator cen = insetlayoutlist_.end();
        while (!n.empty()) {
-               if (insetlayoutlist_.count(n) > 0)
-                       return insetlayoutlist_[n];
+               InsetLayouts::const_iterator cit = insetlayoutlist_.lower_bound(n);
+               if (cit != cen && cit->first == n)
+                       return cit->second;
                size_t i = n.find(':');
                if (i == string::npos)
                        break;
@@ -952,17 +1041,29 @@ docstring const & TextClass::defaultLayoutName() const
 }
 
 
-LayoutPtr const & TextClass::defaultLayout() const
+Layout const & TextClass::defaultLayout() const
 {
        return operator[](defaultLayoutName());
 }
 
 
+bool TextClass::isDefaultLayout(Layout const & lay) const 
+{
+       return lay.name() == defaultLayoutName();
+}
+
+
+bool TextClass::isEmptyLayout(Layout const & lay) const 
+{
+       return lay.name() == emptyLayoutName();
+}
+
+
 DocumentClass & DocumentClassBundle::newClass(LayoutFile const & baseClass)
 {
-       DocumentClass dc(baseClass);
+       DocumentClass * dc = new DocumentClass(baseClass);
        tc_list_.push_back(dc);
-       return tc_list_.back();
+       return *tc_list_.back();
 }
 
 
@@ -983,7 +1084,7 @@ bool DocumentClass::hasLaTeXLayout(std::string const & lay) const
        LayoutList::const_iterator it  = layoutlist_.begin();
        LayoutList::const_iterator end = layoutlist_.end();
        for (; it != end; ++it)
-               if (it->get()->latexname() == lay)
+               if (it->latexname() == lay)
                        return true;
        return false;
 }