lyx_mirror/src/insets/insetert.C

349 lines
6.4 KiB
C++
Raw Normal View History

/**
* \file insetert.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author J<EFBFBD>rgen Vigna
* \author Lars Gullik Bj<EFBFBD>nnes
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "insetert.h"
#include "buffer.h"
#include "bufferparams.h"
#include "BufferView.h"
#include "debug.h"
#include "dispatchresult.h"
#include "funcrequest.h"
#include "FuncStatus.h"
#include "gettext.h"
#include "language.h"
#include "LColor.h"
#include "lyxlex.h"
#include "metricsinfo.h"
#include "paragraph.h"
#include "frontends/Alert.h"
#include "frontends/LyXView.h"
#include <sstream>
using lyx::pos_type;
using std::endl;
using std::min;
using std::auto_ptr;
using std::istringstream;
using std::ostream;
using std::ostringstream;
using std::string;
void InsetERT::init()
{
setButtonLabel();
LyXFont font(LyXFont::ALL_SANE);
font.decSize();
font.decSize();
font.setColor(LColor::latex);
setLabelFont(font);
setInsetName("ERT");
}
InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
: InsetCollapsable(bp, status)
{
init();
}
InsetERT::InsetERT(InsetERT const & in)
: InsetCollapsable(in)
{
init();
}
auto_ptr<InsetBase> InsetERT::clone() const
{
return auto_ptr<InsetBase>(new InsetERT(*this));
}
InsetERT::InsetERT(BufferParams const & bp,
Language const * l, string const & contents, CollapseStatus status)
: InsetCollapsable(bp, status)
{
LyXFont font(LyXFont::ALL_INHERIT, l);
string::const_iterator cit = contents.begin();
string::const_iterator end = contents.end();
pos_type pos = 0;
the stuff from the sneak preview: For one, it still contains a few things that are already in CVS (the 'brown paperbag' changes). Secondly, this changes the ParagraphList to a std::vector but does not yet take full advantage of it except removing LyXText::parOffset() and similar. I had an extensive talk with my profiler and we are happy nevertheless. This also moves almost all Cut&Paste specific stuff from text.C to CutAndPaste.C. Much smaller interface now... Namespace CutAndPaste is now lyx::cap::. Was inconsistent with the rest.... Make ParagraphList a proper class. We'll need this later for a specialized erase/insert. Remove some unneeded prototypes and function declarations Use ParameterStruct directly instead of ShareContainer<ParameterStruct> Inline a few accesses to CursorSlice members as suggested by the profiler. Fix commandline conversion crash reported by Kayvan. Replace PosIterator by DocumentIterator. The latter can also iterate through math and nested text in math... Remove math specific hack from Documentiterator Derive InsetCollapsable from InsetText instead of using an InsetText member. This give us the opportunity to get rid of the InsetOld::owner_ backpointer. Cosmetics in CutAndPaste.C and cursor.C. Fix nasty crash (popping slices off an empty selection anchor). Add a few asserts. Remove all 'manual' update calls. We do now one per user interaction which is completely sufficient. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8527 a592a061-630c-0410-9148-cb99ea01b6c8
2004-03-25 09:16:36 +00:00
for (; cit != end; ++cit)
paragraphs().begin()->insertChar(pos++, *cit, font);
// the init has to be after the initialization of the paragraph
// because of the label settings (draw_label for ert insets).
init();
}
InsetERT::~InsetERT()
{
InsetERTMailer(*this).hideDialog();
}
void InsetERT::write(Buffer const & buf, ostream & os) const
{
os << "ERT" << "\n";
InsetCollapsable::write(buf, os);
}
string const InsetERT::editMessage() const
{
return _("Opened ERT Inset");
}
int InsetERT::latex(Buffer const &, ostream & os,
OutputParams const &) const
{
ParagraphList::const_iterator par = paragraphs().begin();
ParagraphList::const_iterator end = paragraphs().end();
int lines = 0;
while (par != end) {
pos_type siz = par->size();
for (pos_type i = 0; i < siz; ++i) {
// ignore all struck out text
if (isDeletedText(*par, i))
continue;
if (par->isNewline(i)) {
os << '\n';
++lines;
} else {
os << par->getChar(i);
}
}
++par;
if (par != end) {
os << "\n";
++lines;
}
}
return lines;
}
int InsetERT::plaintext(Buffer const &, ostream &,
OutputParams const & /*runparams*/) const
{
return 0;
}
int InsetERT::linuxdoc(Buffer const &, ostream & os,
the stuff from the sneak preview: For one, it still contains a few things that are already in CVS (the 'brown paperbag' changes). Secondly, this changes the ParagraphList to a std::vector but does not yet take full advantage of it except removing LyXText::parOffset() and similar. I had an extensive talk with my profiler and we are happy nevertheless. This also moves almost all Cut&Paste specific stuff from text.C to CutAndPaste.C. Much smaller interface now... Namespace CutAndPaste is now lyx::cap::. Was inconsistent with the rest.... Make ParagraphList a proper class. We'll need this later for a specialized erase/insert. Remove some unneeded prototypes and function declarations Use ParameterStruct directly instead of ShareContainer<ParameterStruct> Inline a few accesses to CursorSlice members as suggested by the profiler. Fix commandline conversion crash reported by Kayvan. Replace PosIterator by DocumentIterator. The latter can also iterate through math and nested text in math... Remove math specific hack from Documentiterator Derive InsetCollapsable from InsetText instead of using an InsetText member. This give us the opportunity to get rid of the InsetOld::owner_ backpointer. Cosmetics in CutAndPaste.C and cursor.C. Fix nasty crash (popping slices off an empty selection anchor). Add a few asserts. Remove all 'manual' update calls. We do now one per user interaction which is completely sufficient. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8527 a592a061-630c-0410-9148-cb99ea01b6c8
2004-03-25 09:16:36 +00:00
OutputParams const &) const
{
ParagraphList::const_iterator par = paragraphs().begin();
ParagraphList::const_iterator end = paragraphs().end();
int lines = 0;
while (par != end) {
pos_type siz = par->size();
for (pos_type i = 0; i < siz; ++i) {
if (par->isNewline(i)) {
os << '\n';
++lines;
} else {
os << par->getChar(i);
}
}
++par;
if (par != end) {
os << "\n";
lines ++;
}
}
return lines;
}
int InsetERT::docbook(Buffer const &, ostream & os,
OutputParams const &) const
{
ParagraphList::const_iterator par = paragraphs().begin();
ParagraphList::const_iterator end = paragraphs().end();
int lines = 0;
while (par != end) {
pos_type siz = par->size();
for (pos_type i = 0; i < siz; ++i) {
if (par->isNewline(i)) {
os << '\n';
++lines;
} else {
os << par->getChar(i);
}
}
++par;
if (par != end) {
os << "\n";
++lines;
}
}
return lines;
}
void InsetERT::priv_dispatch(LCursor & cur, FuncRequest & cmd)
{
//lyxerr << "\nInsetERT::priv_dispatch (begin): cmd: " << cmd << endl;
switch (cmd.action) {
case LFUN_INSET_MODIFY: {
InsetCollapsable::CollapseStatus st;
InsetERTMailer::string2params(cmd.argument, st);
setStatus(st);
break;
}
default:
InsetCollapsable::priv_dispatch(cur, cmd);
break;
}
}
bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
FuncStatus & status) const
{
switch (cmd.action) {
// suppress these
case LFUN_LAYOUT:
case LFUN_BOLD:
case LFUN_CODE:
case LFUN_DEFAULT:
case LFUN_EMPH:
case LFUN_FREEFONT_APPLY:
case LFUN_FREEFONT_UPDATE:
case LFUN_NOUN:
case LFUN_ROMAN:
case LFUN_SANS:
case LFUN_FRAK:
case LFUN_ITAL:
case LFUN_FONT_SIZE:
case LFUN_FONT_STATE:
case LFUN_UNDERLINE:
status.enabled(false);
return true;
default:
return InsetCollapsable::getStatus(cur, cmd, status);
}
}
void InsetERT::setButtonLabel()
{
setLabel(status() == Collapsed ? getNewLabel(_("P-ERT")) : _("P-ERT"));
}
bool InsetERT::insetAllowed(InsetOld::Code code) const
{
return code == InsetOld::NEWLINE_CODE;
}
void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
{
LyXFont tmpfont = mi.base.font;
getDrawFont(mi.base.font);
InsetCollapsable::metrics(mi, dim);
mi.base.font = tmpfont;
dim_ = dim;
}
void InsetERT::draw(PainterInfo & pi, int x, int y) const
{
LyXFont tmpfont = pi.base.font;
getDrawFont(pi.base.font);
InsetCollapsable::draw(pi, x, y);
pi.base.font = tmpfont;
}
bool InsetERT::showInsetDialog(BufferView * bv) const
{
InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
return true;
}
void InsetERT::getDrawFont(LyXFont & font) const
{
font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
font.setFamily(LyXFont::TYPEWRITER_FAMILY);
font.setColor(LColor::latex);
}
string const InsetERTMailer::name_("ert");
InsetERTMailer::InsetERTMailer(InsetERT & inset)
: inset_(inset)
{}
string const InsetERTMailer::inset2string(Buffer const &) const
{
return params2string(inset_.status());
}
void InsetERTMailer::string2params(string const & in,
InsetCollapsable::CollapseStatus & status)
{
status = InsetCollapsable::Collapsed;
if (in.empty())
return;
istringstream data(in);
LyXLex lex(0,0);
lex.setStream(data);
string name;
lex >> name;
if (name != name_)
return print_mailer_error("InsetERTMailer", in, 1, name_);
int s;
lex >> s;
if (lex)
status = static_cast<InsetCollapsable::CollapseStatus>(s);
}
string const
InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
{
ostringstream data;
data << name_ << ' ' << status;
return data.str();
}