* InsetList: introducing find() and count()

* Paragraph:
- erase numberOfOptArgs() and bibitem()
- move onlyText() to Private.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21180 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-10-24 15:32:43 +00:00
parent af5c4977cd
commit 1026a87b72
7 changed files with 82 additions and 65 deletions

View File

@ -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

View File

@ -12,6 +12,8 @@
#ifndef INSET_LIST_H
#define INSET_LIST_H
#include "insets/InsetCode.h"
#include "support/types.h"
#include <vector>
@ -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_;

View File

@ -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<InsetBibitem *>(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 << "<![CDATA[";
}
@ -2588,19 +2581,6 @@ Inset const * Paragraph::getInset(pos_type pos) const
}
int Paragraph::numberOfOptArgs() const
{
int num = 0;
InsetList::const_iterator it = insetList().begin();
InsetList::const_iterator end = insetList().end();
for (; it != end ; ++it) {
if (it->inset->lyxCode() == OPTARG_CODE)
++num;
}
return num;
}
void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
pos_type right, TextCase action)
{

View File

@ -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);

View File

@ -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:

View File

@ -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<InsetBibitem const *>(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())

View File

@ -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: