From a51525c4162632d77884827909336ba7d88482e6 Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Thu, 21 Feb 2008 16:31:59 +0000 Subject: [PATCH] Add empty InsetLayout for undefined cases. Should avoid possible bugs caused by empty layout_ pointer in InsetCollapsable. NOTE: Some cleanup is needed here, and I'll do it shortly. Doing it properly, though, requires making InsetLayout a proper class. (At the moment, it's just a C-style struct.) That should be committed separately. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23103 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/CutAndPaste.cpp | 2 +- src/TextClass.cpp | 10 ++++------ src/TextClass.h | 4 ++++ src/insets/InsetCollapsable.cpp | 9 ++++++++- src/insets/InsetCollapsable.h | 4 +++- src/insets/InsetFlex.cpp | 6 ------ src/insets/InsetFlex.h | 3 --- src/insets/InsetLayout.h | 6 ++++++ 8 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index 1dfd91e6af..f06e7cd32a 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -449,7 +449,7 @@ void switchBetweenClasses(TextClassPtr const & oldone, // FIXME: Should we verify all InsetCollapsable? continue; inset->setLayout(newone); - if (inset->getLayout().labelstring != from_utf8("UNDEFINED")) + if (!inset->undefined()) continue; // The flex inset is undefined in newtc docstring const s = bformat(_( diff --git a/src/TextClass.cpp b/src/TextClass.cpp index aad3678723..7f640b85be 100644 --- a/src/TextClass.cpp +++ b/src/TextClass.cpp @@ -133,6 +133,9 @@ TextClass::TextClass(string const & fn, string const & cln, docstring const TextClass::emptylayout_ = from_ascii("PlainLayout"); +InsetLayout TextClass::empty_insetlayout_; + + bool TextClass::isTeXClassAvailable() const { return texClassAvail_; @@ -1168,12 +1171,7 @@ InsetLayout const & TextClass::insetlayout(docstring const & name) const break; n = n.substr(0,i); } - static InsetLayout empty; - empty.labelstring = from_utf8("UNDEFINED"); - empty.labelfont = sane_font; - empty.labelfont.setColor(Color_error); - empty.bgcolor = Color_error; - return empty; + return empty_insetlayout_; } diff --git a/src/TextClass.h b/src/TextClass.h index 4b9c1d3cea..818ce6a56d 100644 --- a/src/TextClass.h +++ b/src/TextClass.h @@ -182,6 +182,8 @@ public: int max_toclevel() const; /// returns true if the class has a ToC structure bool hasTocLevels() const; + /// + static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; } private: /// bool deleteLayout(docstring const &); @@ -265,6 +267,8 @@ private: int min_toclevel_; /// The maximal TocLevel of sectioning layouts int max_toclevel_; + /// + static InsetLayout empty_insetlayout_; }; diff --git a/src/insets/InsetCollapsable.cpp b/src/insets/InsetCollapsable.cpp index 107553fd30..0780999c2e 100644 --- a/src/insets/InsetCollapsable.cpp +++ b/src/insets/InsetCollapsable.cpp @@ -132,7 +132,7 @@ void InsetCollapsable::setLayout(TextClassPtr tc) layout_ = &tc->insetlayout(name()); labelstring_ = layout_->labelstring; } else { - layout_ = 0; + layout_ = &TextClass::emptyInsetLayout(); labelstring_ = _("UNDEFINED"); } @@ -885,4 +885,11 @@ void InsetCollapsable::validate(LaTeXFeatures & features) const } +bool InsetCollapsable::undefined() const +{ + std::string const & n = getLayout().name; + return n.empty() || n == TextClass::emptyInsetLayout().name; +} + + } // namespace lyx diff --git a/src/insets/InsetCollapsable.h b/src/insets/InsetCollapsable.h index 71dba17315..432e52c8cd 100644 --- a/src/insets/InsetCollapsable.h +++ b/src/insets/InsetCollapsable.h @@ -161,7 +161,9 @@ public: virtual bool forceLTR() const { return layout_->forceltr; } /// virtual bool useEmptyLayout() const { return true; } - + /// Is this inset's layout defined in the document's textclass? + /// May be wrong after textclass change or paste from another document + bool undefined() const; protected: /// virtual void doDispatch(Cursor & cur, FuncRequest & cmd); diff --git a/src/insets/InsetFlex.cpp b/src/insets/InsetFlex.cpp index 2b2664c66f..7a83014013 100644 --- a/src/insets/InsetFlex.cpp +++ b/src/insets/InsetFlex.cpp @@ -61,12 +61,6 @@ Inset * InsetFlex::clone() const } -bool InsetFlex::undefined() const -{ - return getLayout().labelstring == from_utf8("UNDEFINED"); -} - - docstring const InsetFlex::editMessage() const { return _("Opened Flex Inset"); diff --git a/src/insets/InsetFlex.h b/src/insets/InsetFlex.h index 75c52bc02d..3bb10abe49 100644 --- a/src/insets/InsetFlex.h +++ b/src/insets/InsetFlex.h @@ -31,9 +31,6 @@ public: /// docstring name() const { return from_utf8(name_); } - /// Is this character style defined in the document's textclass? - /// May be wrong after textclass change or paste from another document - bool undefined() const; /// virtual docstring const editMessage() const; /// diff --git a/src/insets/InsetLayout.h b/src/insets/InsetLayout.h index 37ccb9d28f..c5f64c6a9e 100644 --- a/src/insets/InsetLayout.h +++ b/src/insets/InsetLayout.h @@ -23,6 +23,12 @@ namespace lyx { /// class InsetLayout { public: + InsetLayout() : + name("undefined"), + labelstring(from_utf8("UNDEFINED")), + font(sane_font), labelfont(sane_font), + bgcolor(Color_error) + { labelfont.setColor(Color_error); }; std::string name; std::string lyxtype; docstring labelstring; -- 2.39.5