2012-03-06 07:54:22 +00:00
|
|
|
/**
|
|
|
|
* \file InsetIPA.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Jürgen Spitzmüller
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "InsetIPA.h"
|
|
|
|
|
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "Cursor.h"
|
2012-03-06 09:14:24 +00:00
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "FuncStatus.h"
|
2012-03-06 07:54:22 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
|
|
|
#include "Lexer.h"
|
|
|
|
#include "MetricsInfo.h"
|
|
|
|
#include "OutputParams.h"
|
|
|
|
#include "RenderPreview.h"
|
2016-06-19 02:39:38 +00:00
|
|
|
#include "texstream.h"
|
2012-03-06 07:54:22 +00:00
|
|
|
|
|
|
|
#include "frontends/Painter.h"
|
|
|
|
|
|
|
|
#include "graphics/PreviewImage.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
InsetIPA::InsetIPA(Buffer * buf)
|
2012-03-06 07:54:22 +00:00
|
|
|
: InsetText(buf),
|
2016-02-03 13:50:00 +00:00
|
|
|
preview_(new RenderPreview(this))
|
2012-03-06 07:54:22 +00:00
|
|
|
{
|
|
|
|
setDrawFrame(true);
|
|
|
|
setFrameColor(Color_insetframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
InsetIPA::~InsetIPA()
|
2012-03-06 07:54:22 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
InsetIPA::InsetIPA(InsetIPA const & other)
|
|
|
|
: InsetText(other)
|
|
|
|
{
|
|
|
|
preview_.reset(new RenderPreview(*other.preview_, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-11 09:16:09 +00:00
|
|
|
InsetIPA & InsetIPA::operator=(InsetIPA const & other)
|
|
|
|
{
|
|
|
|
if (&other == this)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
InsetText::operator=(other);
|
|
|
|
preview_.reset(new RenderPreview(*other.preview_, this));
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-06 07:54:22 +00:00
|
|
|
void InsetIPA::write(ostream & os) const
|
|
|
|
{
|
|
|
|
os << "IPA" << "\n";
|
|
|
|
text().write(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-09 11:14:39 +00:00
|
|
|
void InsetIPA::doDispatch(Cursor & cur, FuncRequest & cmd)
|
|
|
|
{
|
|
|
|
switch (cmd.action()) {
|
|
|
|
case LFUN_QUOTE_INSERT: {
|
|
|
|
FuncRequest fr(LFUN_SELF_INSERT, "\"");
|
|
|
|
InsetText::doDispatch(cur, fr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
InsetText::doDispatch(cur, cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-06 09:14:24 +00:00
|
|
|
bool InsetIPA::getStatus(Cursor & cur, FuncRequest const & cmd,
|
|
|
|
FuncStatus & flag) const
|
|
|
|
{
|
|
|
|
switch (cmd.action()) {
|
|
|
|
case LFUN_SCRIPT_INSERT: {
|
|
|
|
if (cmd.argument() == "subscript") {
|
|
|
|
flag.setEnabled(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-08-21 12:56:34 +00:00
|
|
|
case LFUN_IN_IPA:
|
|
|
|
flag.setEnabled(true);
|
|
|
|
return true;
|
|
|
|
break;
|
2012-03-06 09:14:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return InsetText::getStatus(cur, cmd, flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-06 07:54:22 +00:00
|
|
|
void InsetIPA::addPreview(DocIterator const & inset_pos,
|
|
|
|
graphics::PreviewLoader &) const
|
|
|
|
{
|
|
|
|
preparePreview(inset_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
void InsetIPA::preparePreview(DocIterator const & pos) const
|
2012-03-06 07:54:22 +00:00
|
|
|
{
|
2016-06-19 02:39:38 +00:00
|
|
|
odocstringstream str;
|
2016-09-04 02:02:47 +00:00
|
|
|
otexstream os(str);
|
2012-03-06 07:54:22 +00:00
|
|
|
OutputParams runparams(&pos.buffer()->params().encoding());
|
|
|
|
latex(os, runparams);
|
|
|
|
docstring const snippet = str.str();
|
2017-07-03 17:53:14 +00:00
|
|
|
preview_->addPreview(snippet, *pos.buffer());
|
2012-03-06 07:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetIPA::previewState(BufferView * bv) const
|
|
|
|
{
|
2014-05-20 10:19:26 +00:00
|
|
|
if (!editing(bv) && RenderPreview::previewText()) {
|
2012-03-06 07:54:22 +00:00
|
|
|
graphics::PreviewImage const * pimage =
|
|
|
|
preview_->getPreviewImage(bv->buffer());
|
|
|
|
return pimage && pimage->image();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetIPA::reloadPreview(DocIterator const & pos) const
|
|
|
|
{
|
|
|
|
preparePreview(pos);
|
|
|
|
preview_->startLoading(*pos.buffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetIPA::draw(PainterInfo & pi, int x, int y) const
|
|
|
|
{
|
2016-02-03 13:50:00 +00:00
|
|
|
if (previewState(pi.base.bv)) {
|
2016-07-21 18:16:27 +00:00
|
|
|
preview_->draw(pi, x, y);
|
2012-03-06 07:54:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
InsetText::draw(pi, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetIPA::edit(Cursor & cur, bool front, EntryDirection entry_from)
|
|
|
|
{
|
|
|
|
cur.push(*this);
|
|
|
|
InsetText::edit(cur, front, entry_from);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Inset * InsetIPA::editXY(Cursor & cur, int x, int y)
|
|
|
|
{
|
2016-02-03 13:50:00 +00:00
|
|
|
if (previewState(&cur.bv())) {
|
2012-03-06 07:54:22 +00:00
|
|
|
edit(cur, true, ENTRY_DIRECTION_IGNORE);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
cur.push(*this);
|
|
|
|
return InsetText::editXY(cur, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetIPA::metrics(MetricsInfo & mi, Dimension & dim) const
|
|
|
|
{
|
|
|
|
if (previewState(mi.base.bv)) {
|
|
|
|
preview_->metrics(mi, dim);
|
2016-07-21 18:16:27 +00:00
|
|
|
|
2012-03-06 07:54:22 +00:00
|
|
|
dim.wid = max(dim.wid, 4);
|
|
|
|
dim.asc = max(dim.asc, 4);
|
2016-07-21 18:16:27 +00:00
|
|
|
|
2012-03-06 07:54:22 +00:00
|
|
|
dim.asc += TEXT_TO_INSET_OFFSET;
|
|
|
|
dim.des += TEXT_TO_INSET_OFFSET;
|
|
|
|
// insert a one pixel gap
|
|
|
|
dim.wid += 1;
|
|
|
|
Dimension dim_dummy;
|
|
|
|
MetricsInfo mi_dummy = mi;
|
|
|
|
InsetText::metrics(mi_dummy, dim_dummy);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InsetText::metrics(mi, dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetIPA::notifyCursorLeaves(Cursor const & old, Cursor & cur)
|
|
|
|
{
|
|
|
|
reloadPreview(old);
|
|
|
|
cur.screenUpdateFlags(Update::Force);
|
|
|
|
return InsetText::notifyCursorLeaves(old, cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetIPA::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
2013-12-14 08:13:17 +00:00
|
|
|
features.require("tipa");
|
|
|
|
features.require("tipx");
|
|
|
|
|
2012-08-21 12:56:34 +00:00
|
|
|
InsetText::validate(features);
|
2012-03-06 07:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-09 09:05:13 +00:00
|
|
|
void InsetIPA::latex(otexstream & os, OutputParams const & runparams_in) const
|
2012-03-06 07:54:22 +00:00
|
|
|
{
|
2012-03-09 09:05:13 +00:00
|
|
|
OutputParams runparams(runparams_in);
|
|
|
|
runparams.inIPA = true;
|
2012-03-06 07:54:22 +00:00
|
|
|
bool const multipar = (text().paragraphs().size() > 1);
|
2012-03-06 10:39:01 +00:00
|
|
|
// fontspec knows \textipa, but not the IPA environment
|
|
|
|
bool const nontexfonts = buffer_->params().useNonTeXFonts;
|
|
|
|
if (multipar && !nontexfonts)
|
2012-03-06 07:54:22 +00:00
|
|
|
os << "\\begin{IPA}\n";
|
|
|
|
else
|
|
|
|
os << "\\textipa{";
|
|
|
|
InsetText::latex(os, runparams);
|
2012-03-06 10:39:01 +00:00
|
|
|
if (multipar && !nontexfonts)
|
2012-03-06 07:54:22 +00:00
|
|
|
os << "\n\\end{IPA}";
|
|
|
|
else
|
|
|
|
os << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-15 19:17:21 +00:00
|
|
|
docstring InsetIPA::xhtml(XHTMLStream & xs, OutputParams const & runparams_in) const
|
|
|
|
{
|
|
|
|
OutputParams runparams(runparams_in);
|
|
|
|
runparams.inIPA = true;
|
|
|
|
return InsetText::xhtml(xs, runparams);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-06 07:54:22 +00:00
|
|
|
bool InsetIPA::insetAllowed(InsetCode code) const
|
|
|
|
{
|
2012-03-06 09:14:24 +00:00
|
|
|
switch (code) {
|
|
|
|
// code that is allowed
|
|
|
|
case ERT_CODE:
|
2012-08-23 15:42:53 +00:00
|
|
|
case IPACHAR_CODE:
|
|
|
|
case IPADECO_CODE:
|
2012-03-06 09:14:24 +00:00
|
|
|
case SCRIPT_CODE:
|
2012-03-06 07:54:22 +00:00
|
|
|
return true;
|
2012-03-06 09:14:24 +00:00
|
|
|
default:
|
2012-03-06 07:54:22 +00:00
|
|
|
return false;
|
2012-03-06 09:14:24 +00:00
|
|
|
}
|
2012-03-06 07:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|