lyx_mirror/src/insets/InsetVSpace.cpp

272 lines
5.7 KiB
C++
Raw Normal View History

/**
* \file InsetVSpace.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author various
* \author André Pönitz
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "InsetVSpace.h"
#include "Buffer.h"
#include "BufferView.h"
#include "Cursor.h"
#include "Dimension.h"
#include "DispatchResult.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
#include "Lexer.h"
#include "MetricsInfo.h"
#include "OutputParams.h"
#include "output_xhtml.h"
#include "texstream.h"
#include "Text.h"
#include "support/debug.h"
#include "support/docstream.h"
#include "support/gettext.h"
#include "support/lassert.h"
#include "frontends/Application.h"
#include "frontends/FontMetrics.h"
#include "frontends/Painter.h"
#include <sstream>
using namespace std;
namespace lyx {
namespace {
int const ADD_TO_VSPACE_WIDTH = 5;
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
InsetVSpace::InsetVSpace(VSpace const & space)
: Inset(0), space_(space)
{}
void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
{
switch (cmd.action()) {
case LFUN_INSET_MODIFY: {
cur.recordUndo();
string arg = to_utf8(cmd.argument());
if (arg == "vspace custom")
arg = (space_.kind() == VSpace::LENGTH)
? "vspace " + space_.length().asString()
: "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
InsetVSpace::string2params(arg, space_);
break;
}
default:
Inset::doDispatch(cur, cmd);
break;
}
}
bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
FuncStatus & status) const
{
switch (cmd.action()) {
// we handle these
case LFUN_INSET_MODIFY:
if (cmd.getArg(0) == "vspace") {
VSpace vspace;
string arg = to_utf8(cmd.argument());
if (arg == "vspace custom")
arg = (space_.kind() == VSpace::LENGTH)
? "vspace " + space_.length().asString()
: "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
InsetVSpace::string2params(arg, vspace);
status.setOnOff(vspace == space_);
2017-07-03 17:53:14 +00:00
}
status.setEnabled(true);
return true;
2017-07-03 17:53:14 +00:00
default:
return Inset::getStatus(cur, cmd, status);
}
}
void InsetVSpace::read(Lexer & lex)
{
LASSERT(lex.isOK(), return);
string vsp;
lex >> vsp;
Fix bug 3293 by Bernhard Roider: This changes the semantics of isOK() and operator(), comments from Bernhard below: With the old version of lyxlex it was _impossible_ to check whether reading an integer, float, ... succeeded or not. The current solution to check for is.bad() in some cases and in other cases use is.good() does not give the desired information. Moreover the result of is.bad() depends on the stl implementation and behaves different for linux and windows. the bug was introduced by the patch that fixed the bug that crashed lyx when "inset-insert ert" was executed from the command buffer. The lexer has the method isOK() which reflects the status of the stream is. The operators void* and ! are not really well defined (they depend on the value of is.bad()). What is missing is a test if the last reading operation was successful and thus the returned value is valid. That's what i implemented in this patch. The new rule for using the lexer: if you want to know if the lexer still has data to read (either from the stream or from the pushed token) then use "lex.isOK()". If you want to test if the last reading operation was successful then use eg. "if (lex) {...}" or unsuccessful then use eg. "if (!lex) {...}" an example: int readParam(LyxLex &lex) { int param = 1; // default value if (lex.isOK()) { // the lexer has data to read int p; // temporary variable lex >> p; if (lex) param = p; // only use the input if the reading operation was successful } return param; } git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@17569 a592a061-630c-0410-9148-cb99ea01b6c8
2007-03-26 13:43:49 +00:00
if (lex)
space_ = VSpace(vsp);
lex >> "\\end_inset";
}
void InsetVSpace::write(ostream & os) const
{
os << "VSpace " << space_.asLyXCommand();
}
docstring const InsetVSpace::label() const
{
static docstring const label = _("Vertical Space");
return label + " (" + space_.asGUIName() + ')';
}
namespace {
int const vspace_arrow_size = 4;
}
void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
{
int height = 3 * vspace_arrow_size;
if (space_.length().len().value() >= 0.0)
height = max(height, space_.inPixels(*mi.base.bv));
FontInfo font;
font.decSize();
font.decSize();
int w = 0;
int a = 0;
int d = 0;
theFontMetrics(font).rectText(label(), w, a, d);
height = max(height, a + d);
dim.asc = height / 2 + (a - d) / 2; // align cursor with the
dim.des = height - dim.asc; // label text
dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
}
void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
{
Dimension const dim = dimension(*pi.base.bv);
x += ADD_TO_VSPACE_WIDTH;
int const start = y - dim.asc;
int const end = y + dim.des;
// y-values for top arrow
int ty1, ty2;
// y-values for bottom arrow
int by1, by2;
if (space_.kind() == VSpace::VFILL) {
ty1 = ty2 = start;
by1 = by2 = end;
} else {
// adding or removing space
bool const added = space_.kind() != VSpace::LENGTH ||
space_.length().len().value() >= 0.0;
ty1 = added ? (start + vspace_arrow_size) : start;
ty2 = added ? start : (start + vspace_arrow_size);
by1 = added ? (end - vspace_arrow_size) : end;
by2 = added ? end : (end - vspace_arrow_size);
}
int const midx = x + vspace_arrow_size;
int const rightx = midx + vspace_arrow_size;
// first the string
int w = 0;
int a = 0;
int d = 0;
FontInfo font;
font.setColor(Color_added_space);
font.decSize();
font.decSize();
docstring const lab = label();
theFontMetrics(font).rectText(lab, w, a, d);
pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
start + (end - start) / 2 + (a - d) / 2,
lab, font, Color_none, Color_none);
// top arrow
pi.pain.line(x, ty1, midx, ty2, Color_added_space);
pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
// bottom arrow
pi.pain.line(x, by1, midx, by2, Color_added_space);
pi.pain.line(midx, by2, rightx, by1, Color_added_space);
// joining line
pi.pain.line(midx, ty2, midx, by2, Color_added_space);
}
void InsetVSpace::latex(otexstream & os, OutputParams const &) const
{
os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
}
int InsetVSpace::plaintext(odocstringstream & os,
OutputParams const &, size_t) const
{
os << "\n\n";
return PLAINTEXT_NEWLINE;
}
int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
{
os << '\n';
return 1;
}
docstring InsetVSpace::xhtml(XHTMLStream & os, OutputParams const &) const
{
string const len = space_.asHTMLLength();
string const attr = "style='height:" + (len.empty() ? "1em" : len) + "'";
os << html::StartTag("div", attr, true) << html::EndTag("div");
return docstring();
}
string InsetVSpace::contextMenuName() const
{
return "context-vspace";
}
void InsetVSpace::string2params(string const & in, VSpace & vspace)
{
vspace = VSpace();
if (in.empty())
return;
istringstream data(in);
Lexer lex;
lex.setStream(data);
lex.setContext("InsetVSpace::string2params");
lex >> "vspace" >> vspace;
}
string InsetVSpace::params2string(VSpace const & vspace)
{
ostringstream data;
data << "vspace" << ' ' << vspace.asLyXCommand();
return data.str();
}
} // namespace lyx