diff --git a/src/InsetList.cpp b/src/InsetList.cpp index c3d2805511..5f6398b48c 100644 --- a/src/InsetList.cpp +++ b/src/InsetList.cpp @@ -147,4 +147,29 @@ InsetList::InsetList(InsetList const & il) it->inset = it->inset->clone(); } + +pos_type InsetList::find(InsetCode code, pos_type startpos) const +{ + List::const_iterator it = insetIterator(startpos); + List::const_iterator end = list_.end(); + for (; it != end ; ++it) { + if (it->inset->lyxCode() == code) + return it->pos; + } + return -1; +} + + +int InsetList::count(InsetCode code, pos_type startpos) const +{ + int num = 0; + List::const_iterator it = insetIterator(startpos); + List::const_iterator end = list_.end(); + for (; it != end ; ++it) { + if (it->inset->lyxCode() == code) + ++num; + } + return num; +} + } // namespace lyx diff --git a/src/InsetList.h b/src/InsetList.h index da168c488a..2664a87d17 100644 --- a/src/InsetList.h +++ b/src/InsetList.h @@ -12,6 +12,8 @@ #ifndef INSET_LIST_H #define INSET_LIST_H +#include "insets/InsetCode.h" + #include "support/types.h" #include @@ -76,6 +78,21 @@ public: /// void decreasePosAfterPos(pos_type pos); + /// search for next occurence of an \c Inset type. + /// \return the position of the found inset. + /// \retval -1 if no \c Inset is found. + pos_type find( + InsetCode code, ///< Code of inset to find. + pos_type startpos = 0 ///< start position for the search. + ) const; + + /// count occurences of of an \c Inset type. + /// \return the number of found inset(s). + int count( + InsetCode code, ///< Code of inset type to count. + pos_type startpos = 0 ///< start position for the counting. + ) const; + private: /// List list_; diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index a2253db71a..4e58096bc9 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -169,6 +169,10 @@ public: void validate(LaTeXFeatures & features, Layout const & layout) const; + /// Checks if the paragraph contains only text and no inset or font change. + bool onlyText(Buffer const & buf, Font const & outerfont, + pos_type initial) const; + /// match a string against a particular point in the paragraph bool isTextAt(std::string const & str, pos_type pos) const; @@ -1620,17 +1624,6 @@ void Paragraph::setBeginOfBody() } -InsetBibitem * Paragraph::bibitem() const -{ - if (!d->insetlist_.empty()) { - Inset * inset = d->insetlist_.begin()->inset; - if (inset->lyxCode() == BIBITEM_CODE) - return static_cast(inset); - } - return 0; -} - - bool Paragraph::forceDefaultParagraphs() const { return inInset() && inInset()->forceDefaultParagraphs(0); @@ -2178,13 +2171,13 @@ pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputPara } -bool Paragraph::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const +bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const { Font font_old; - - for (pos_type i = initial; i < size(); ++i) { - Font font = getFont(buf.params(), i, outerfont); - if (isInset(i)) + pos_type size = text_.size(); + for (pos_type i = initial; i < size; ++i) { + Font font = owner_->getFont(buf.params(), i, outerfont); + if (text_[i] == META_INSET) return false; if (i != initial && font != font_old) return false; @@ -2207,7 +2200,7 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf, Font font_old = style->labeltype == LABEL_MANUAL ? style->labelfont : style->font; - if (style->pass_thru && !onlyText(buf, outerfont, initial)) + if (style->pass_thru && !d->onlyText(buf, outerfont, initial)) os << "]]>"; // parsing main loop @@ -2245,7 +2238,7 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf, if (style->free_spacing) os << '\n'; - if (style->pass_thru && !onlyText(buf, outerfont, initial)) + if (style->pass_thru && !d->onlyText(buf, outerfont, initial)) os << "inset->lyxCode() == OPTARG_CODE) - ++num; - } - return num; -} - - void Paragraph::changeCase(BufferParams const & bparams, pos_type pos, pos_type right, TextCase action) { diff --git a/src/Paragraph.h b/src/Paragraph.h index b1f7884a53..9ee108efe7 100644 --- a/src/Paragraph.h +++ b/src/Paragraph.h @@ -71,9 +71,8 @@ enum TextCase { /// A Paragraph holds all text, attributes and insets in a text paragraph -/// \todo FIXME: any reference to ParagraphMetrics (including inheritance) -/// should go in order to complete the Model/View separation of this class. -class Paragraph { +class Paragraph +{ public: /// Paragraph(); @@ -127,10 +126,6 @@ public: odocstream & os, OutputParams const & runparams) const; - /// Checks if the paragraph contains only text and no inset or font change. - bool onlyText(Buffer const & buf, Font const & outerfont, - pos_type initial) const; - /// Writes to stream the docbook representation void simpleDocBookOnePar(Buffer const & buf, odocstream &, @@ -166,9 +161,6 @@ public: /// This is the item depth, only used by enumerate and itemize signed char itemdepth; - /// - InsetBibitem * bibitem() const; // ale970302 - /// look up change at given pos Change const & lookupChange(pos_type pos) const; @@ -354,9 +346,6 @@ public: /// by this author in the paragraph. void checkAuthors(AuthorList const & authorList); - /// return the number of InsetOptArg in a paragraph - int numberOfOptArgs() const; - /// void changeCase(BufferParams const & bparams, pos_type pos, pos_type right, TextCase action); diff --git a/src/Text3.cpp b/src/Text3.cpp index 4e1ed31fba..ad38ecc78c 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -33,6 +33,7 @@ #include "factory.h" #include "FuncRequest.h" #include "gettext.h" +#include "InsetList.h" #include "Intl.h" #include "Language.h" #include "Layout.h" @@ -1742,7 +1743,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, break; case LFUN_OPTIONAL_INSERT: code = OPTARG_CODE; - enable = cur.paragraph().numberOfOptArgs() + enable = cur.paragraph().insetList().count(OPTARG_CODE) < cur.paragraph().layout()->optionalargs; break; case LFUN_ENVIRONMENT_INSERT: diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp index 21477ec151..454d67fdd3 100644 --- a/src/insets/InsetBibitem.cpp +++ b/src/insets/InsetBibitem.cpp @@ -20,6 +20,7 @@ #include "FuncRequest.h" #include "Font.h" #include "InsetIterator.h" +#include "InsetList.h" #include "Lexer.h" #include "Paragraph.h" #include "ParagraphList.h" @@ -149,26 +150,29 @@ docstring const bibitemWidest(Buffer const & buffer) ParagraphList::const_iterator end = buffer.paragraphs().end(); for (; it != end; ++it) { - if (it->bibitem()) { - docstring const label = it->bibitem()->getBibLabel(); + if (it->insetList().empty()) + continue; + Inset * inset = it->insetList().begin()->inset; + if (inset->lyxCode() != BIBITEM_CODE) + continue; - // FIXME: we can't be sure using the following that the GUI - // version and the command-line version will give the same - // result. - // - //int const wx = use_gui? - // theFontMetrics(font).width(label): label.size(); - // - // So for now we just use the label size in order to be sure - // that GUI and no-GUI gives the same bibitem (even if that is - // potentially the wrong one. - int const wx = label.size(); + bitem = static_cast(inset); + docstring const label = bitem->getBibLabel(); - if (wx > w) { - w = wx; - bitem = it->bibitem(); - } - } + // FIXME: we can't be sure using the following that the GUI + // version and the command-line version will give the same + // result. + // + //int const wx = use_gui? + // theFontMetrics(font).width(label): label.size(); + // + // So for now we just use the label size in order to be sure + // that GUI and no-GUI gives the same bibitem (even if that is + // potentially the wrong one. + int const wx = label.size(); + + if (wx > w) + w = wx; } if (bitem && !bitem->getBibLabel().empty()) diff --git a/src/insets/InsetCaption.cpp b/src/insets/InsetCaption.cpp index 6e0d6061c3..9447583ccf 100644 --- a/src/insets/InsetCaption.cpp +++ b/src/insets/InsetCaption.cpp @@ -16,6 +16,7 @@ #include "Buffer.h" #include "BufferParams.h" +#include "Color.h" #include "Counters.h" #include "Cursor.h" #include "BufferView.h" @@ -24,7 +25,7 @@ #include "FuncRequest.h" #include "FuncStatus.h" #include "gettext.h" -#include "Color.h" +#include "InsetList.h" #include "MetricsInfo.h" #include "output_latex.h" #include "OutputParams.h" @@ -203,7 +204,7 @@ bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd, return true; case LFUN_OPTIONAL_INSERT: - status.enabled(cur.paragraph().numberOfOptArgs() == 0); + status.enabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1); return true; default: