lyx_mirror/src/insets/InsetPhantom.cpp

408 lines
8.6 KiB
C++
Raw Normal View History

/**
* \file InsetPhantom.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Uwe Stöhr
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "InsetPhantom.h"
#include "Buffer.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "BufferParams.h"
#include "Cursor.h"
#include "Dimension.h"
#include "DispatchResult.h"
#include "Exporter.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
#include "InsetIterator.h"
#include "LaTeXFeatures.h"
#include "Lexer.h"
#include "MetricsInfo.h"
#include "OutputParams.h"
#include "texstream.h"
#include "TextClass.h"
#include "support/docstream.h"
#include "support/gettext.h"
#include "support/lstrings.h"
#include "support/Translator.h"
#include "frontends/Application.h"
#include "frontends/FontMetrics.h"
#include "frontends/Painter.h"
#include <algorithm>
#include <sstream>
using namespace std;
namespace lyx {
namespace {
typedef Translator<string, InsetPhantomParams::Type> PhantomTranslator;
typedef Translator<docstring, InsetPhantomParams::Type> PhantomTranslatorLoc;
PhantomTranslator const init_phantomtranslator()
{
PhantomTranslator translator("Phantom", InsetPhantomParams::Phantom);
translator.addPair("HPhantom", InsetPhantomParams::HPhantom);
translator.addPair("VPhantom", InsetPhantomParams::VPhantom);
return translator;
}
PhantomTranslatorLoc const init_phantomtranslator_loc()
{
PhantomTranslatorLoc translator(_("Phantom"), InsetPhantomParams::Phantom);
translator.addPair(_("HPhantom"), InsetPhantomParams::HPhantom);
translator.addPair(_("VPhantom"), InsetPhantomParams::VPhantom);
return translator;
}
PhantomTranslator const & phantomtranslator()
{
static PhantomTranslator const translator =
init_phantomtranslator();
return translator;
}
PhantomTranslatorLoc const & phantomtranslator_loc()
{
static PhantomTranslatorLoc const translator =
init_phantomtranslator_loc();
return translator;
}
Bulk cleanup/fix incorrect annotation at the end of namespaces. This commit does a bulk fix of incorrect annotations (comments) at the end of namespaces. The commit was generated by initially running clang-format, and then from the diff of the result extracting the hunks corresponding to fixes of namespace comments. The changes being applied and all the results have been manually reviewed. The source code successfully builds on macOS. Further details on the steps below, in case they're of interest to someone else in the future. 1. Checkout a fresh and up to date version of src/ git pull && git checkout -- src && git status src 2. Ensure there's a suitable .clang-format in place, i.e. with options to fix the comment at the end of namespaces, including: FixNamespaceComments: true SpacesBeforeTrailingComments: 1 and that clang-format is >= 5.0.0, by doing e.g.: clang-format -dump-config | grep Comments: clang-format --version 3. Apply clang-format to the source: clang-format -i $(find src -name "*.cpp" -or -name "*.h") 4. Create and filter out hunks related to fixing the namespace git diff -U0 src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace.patch 5. Filter out hunks corresponding to simple fixes into to a separate patch: pcregrep -M -e '^diff[^\n]+\nindex[^\n]+\n--- [^\n]+\n\+\+\+ [^\n]+\n' \ -e '^@@ -[0-9]+ \+[0-9]+ @@[^\n]*\n-\}[^\n]*\n\+\}[^\n]*\n' \ fix_namespace.patch > fix_namespace_simple.patch 6. Manually review the simple patch and then apply it, after first restoring the source. git checkout -- src patch -p1 < fix_namespace_simple.path 7. Manually review the (simple) changes and then stage the changes git diff src git add src 8. Again apply clang-format and filter out hunks related to any remaining fixes to the namespace, this time filter with more context. There will be fewer hunks as all the simple cases have already been handled: clang-format -i $(find src -name "*.cpp" -or -name "*.h") git diff src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace2.patch 9. Manually review/edit the resulting patch file to remove hunks for files which need to be dealt with manually, noting the file names and line numbers. Then restore files to as before applying clang-format and apply the patch: git checkout src patch -p1 < fix_namespace2.patch 10. Manually fix the files noted in the previous step. Stage files, review changes and commit.
2017-07-23 11:11:54 +00:00
} // namespace
InsetPhantomParams::InsetPhantomParams()
: type(Phantom)
{}
void InsetPhantomParams::write(ostream & os) const
{
string const label = phantomtranslator().find(type);
os << "Phantom " << label << "\n";
}
void InsetPhantomParams::read(Lexer & lex)
{
string label;
lex >> label;
if (lex)
type = phantomtranslator().find(label);
}
/////////////////////////////////////////////////////////////////////
//
// InsetPhantom
//
/////////////////////////////////////////////////////////////////////
InsetPhantom::InsetPhantom(Buffer * buf, string const & label)
: InsetCollapsible(buf)
{
setDrawFrame(false);
params_.type = phantomtranslator().find(label);
}
InsetPhantom::~InsetPhantom()
{
hideDialogs("phantom", this);
}
docstring InsetPhantom::layoutName() const
{
return from_ascii("Phantom:" + phantomtranslator().find(params_.type));
}
void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
{
// draw the text
InsetCollapsible::draw(pi, x, y);
// draw the inset marker
drawMarkers(pi, x, y);
2015-05-17 15:27:12 +00:00
// draw the arrow(s)
static int const arrow_size = 4;
ColorCode const origcol = pi.base.font.color();
pi.base.font.setColor(Color_special);
pi.base.font.setColor(origcol);
Dimension const dim = dimension(*pi.base.bv);
if (params_.type == InsetPhantomParams::Phantom ||
params_.type == InsetPhantomParams::VPhantom) {
// y1---------
// / \.
// y2----- / | \.
// |
// |
// y3----- \ | /
// \ /
// y4---------
// | | |
// / | \.
// x1 x2 x3
int const x2 = x + dim.wid / 2;
int const x1 = x2 - arrow_size;
int const x3 = x2 + arrow_size;
int const y1 = y - dim.asc;
int const y2 = y1 + arrow_size;
int const y4 = y + dim.des;
int const y3 = y4 - arrow_size;
// top arrow
pi.pain.line(x2, y1, x1, y2, Color_added_space);
pi.pain.line(x2, y1, x3, y2, Color_added_space);
// bottom arrow
pi.pain.line(x2, y4, x1, y3, Color_added_space);
pi.pain.line(x2, y4, x3, y3, Color_added_space);
// joining line
pi.pain.line(x2, y1, x2, y4, Color_added_space);
}
if (params_.type == InsetPhantomParams::Phantom ||
params_.type == InsetPhantomParams::HPhantom) {
// y1---- / \.
// / \.
// y2--- <---------------->
// \ /
// y3---- \ /
// | | | |
// x1 x2 x3 x4
x = x + TEXT_TO_INSET_OFFSET;
int const x1 = x;
int const x2 = x + arrow_size;
int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
int const x3 = x4 - arrow_size;
int const y2 = y + (dim.des - dim.asc) / 2;
int const y1 = y2 - arrow_size;
int const y3 = y2 + arrow_size;
// left arrow
pi.pain.line(x1, y2, x2, y3, Color_added_space);
pi.pain.line(x1, y2, x2, y1, Color_added_space);
// right arrow
pi.pain.line(x4, y2, x3, y3, Color_added_space);
pi.pain.line(x4, y2, x3, y1, Color_added_space);
// joining line
pi.pain.line(x1, y2, x4, y2, Color_added_space);
}
}
void InsetPhantom::write(ostream & os) const
{
params_.write(os);
InsetCollapsible::write(os);
}
void InsetPhantom::read(Lexer & lex)
{
params_.read(lex);
InsetCollapsible::read(lex);
}
void InsetPhantom::setButtonLabel()
{
docstring const label = phantomtranslator_loc().find(params_.type);
setLabel(label);
}
bool InsetPhantom::showInsetDialog(BufferView * bv) const
{
bv->showDialog("phantom", params2string(params()),
const_cast<InsetPhantom *>(this));
return true;
}
void InsetPhantom::doDispatch(Cursor & cur, FuncRequest & cmd)
{
switch (cmd.action()) {
case LFUN_INSET_MODIFY:
cur.recordUndoInset(this);
string2params(to_utf8(cmd.argument()), params_);
setButtonLabel();
cur.forceBufferUpdate();
break;
case LFUN_INSET_DIALOG_UPDATE:
cur.bv().updateDialog("phantom", params2string(params()));
break;
default:
InsetCollapsible::doDispatch(cur, cmd);
break;
}
}
bool InsetPhantom::getStatus(Cursor & cur, FuncRequest const & cmd,
FuncStatus & flag) const
{
switch (cmd.action()) {
case LFUN_INSET_MODIFY:
if (cmd.getArg(0) == "phantom") {
InsetPhantomParams params;
string2params(to_utf8(cmd.argument()), params);
flag.setOnOff(params_.type == params.type);
}
flag.setEnabled(true);
return true;
case LFUN_INSET_DIALOG_UPDATE:
flag.setEnabled(true);
return true;
default:
return InsetCollapsible::getStatus(cur, cmd, flag);
}
}
docstring InsetPhantom::toolTip(BufferView const &, int, int) const
{
docstring const res = phantomtranslator_loc().find(params_.type);
return toolTipText(res + from_ascii(": "));
}
void InsetPhantom::latex(otexstream & os, OutputParams const & runparams) const
{
if (runparams.moving_arg)
2015-05-17 15:27:12 +00:00
os << "\\protect";
2015-03-16 13:22:03 +00:00
2015-03-16 13:47:38 +00:00
switch (params_.type) {
case InsetPhantomParams::Phantom:
os << "\\phantom{";
2015-03-16 13:47:38 +00:00
break;
case InsetPhantomParams::HPhantom:
os << "\\hphantom{";
2015-03-16 13:47:38 +00:00
break;
case InsetPhantomParams::VPhantom:
os << "\\vphantom{";
2015-03-16 13:47:38 +00:00
break;
default:
os << "\\phantom{";
break;
}
InsetCollapsible::latex(os, runparams);
os << "}";
}
int InsetPhantom::plaintext(odocstringstream & os,
OutputParams const & runparams, size_t max_length) const
{
2015-03-16 13:47:38 +00:00
switch (params_.type) {
case InsetPhantomParams::Phantom:
os << '[' << buffer().B_("phantom") << ":";
break;
2015-03-16 13:47:38 +00:00
case InsetPhantomParams::HPhantom:
os << '[' << buffer().B_("hphantom") << ":";
break;
2015-03-16 13:47:38 +00:00
case InsetPhantomParams::VPhantom:
os << '[' << buffer().B_("vphantom") << ":";
break;
2015-03-16 13:47:38 +00:00
default:
os << '[' << buffer().B_("phantom") << ":";
break;
2015-03-16 13:47:38 +00:00
}
InsetCollapsible::plaintext(os, runparams, max_length);
os << "]";
return PLAINTEXT_NEWLINE;
}
int InsetPhantom::docbook(odocstream & os, OutputParams const & runparams) const
{
docstring cmdname;
2015-03-16 13:47:38 +00:00
switch (params_.type) {
case InsetPhantomParams::Phantom:
case InsetPhantomParams::HPhantom:
case InsetPhantomParams::VPhantom:
default:
cmdname = from_ascii("phantom");
break;
2015-03-16 13:47:38 +00:00
}
os << "<" + cmdname + ">";
int const i = InsetCollapsible::docbook(os, runparams);
os << "</" + cmdname + ">";
return i;
}
docstring InsetPhantom::xhtml(XHTMLStream &, OutputParams const &) const
{
return docstring();
}
string InsetPhantom::contextMenuName() const
{
return "context-phantom";
}
string InsetPhantom::params2string(InsetPhantomParams const & params)
{
ostringstream data;
data << "phantom" << ' ';
params.write(data);
return data.str();
}
void InsetPhantom::string2params(string const & in, InsetPhantomParams & params)
{
params = InsetPhantomParams();
if (in.empty())
return;
istringstream data(in);
Lexer lex;
lex.setStream(data);
lex.setContext("InsetPhantom::string2params");
lex >> "phantom" >> "Phantom";
params.read(lex);
}
} // namespace lyx