X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetcite.C;h=6eedb6df8ccf46d8bf539f5fe5370078359b374f;hb=4a5b7a5952ad2381fcdf4830511293e184c7c5a1;hp=9433e9764c7f342fe0f58ff22b711c3d6ab07b19;hpb=48c9e705592aeea35f2d6cd8dfad98b7eab59685;p=lyx.git diff --git a/src/insets/insetcite.C b/src/insets/insetcite.C index 9433e9764c..6eedb6df8c 100644 --- a/src/insets/insetcite.C +++ b/src/insets/insetcite.C @@ -1,10 +1,9 @@ -// -*- C++ -*- /* This file is part of* * ====================================================== * * LyX, The Document Processor * - * Copyright 2000 The LyX Team. + * Copyright 2000-2001 The LyX Team. * * ====================================================== */ @@ -15,37 +14,40 @@ #endif #include "insetcite.h" -#include "LyXView.h" +#include "buffer.h" #include "BufferView.h" +#include "LaTeXFeatures.h" +#include "LyXView.h" + #include "frontends/Dialogs.h" + #include "support/lstrings.h" -InsetCitation::InsetCitation(string const & key, string const & note) - : InsetCommand("cite", key, note) -{} -InsetCitation::~InsetCitation() -{ - hide(); -} +using std::ostream; -string InsetCitation::getScreenLabel() const + +InsetCitation::InsetCitation(InsetCommandParams const & p, bool) + : InsetCommand(p) +{} + +string const InsetCitation::getScreenLabel(Buffer const *) const { string keys(getContents()); // If keys is "too long" then only print out the first few tokens string label; - if( contains( keys, "," ) ) { + if (contains(keys, ",")) { // Final comma allows while loop to cover all keys - keys = frontStrip( split( keys, label, ',' ) ) + ","; + keys = frontStrip(split(keys, label, ',')) + ","; - const int maxSize( 40 ); - while( contains( keys, "," ) ) { + string::size_type const maxSize = 40; + while (contains(keys, ",")) { string key; - keys = frontStrip( split( keys, key, ',' ) ); + keys = frontStrip(split(keys, key, ',')); - int size = label.size() + 2 + key.size(); - if( size >= maxSize ) { + string::size_type size = label.size() + 2 + key.size(); + if (size >= maxSize) { label += ", ..."; break; } @@ -55,14 +57,60 @@ string InsetCitation::getScreenLabel() const label = keys; } - if( !getOptions().empty() ) + if (!getOptions().empty()) label += ", " + getOptions(); - return '[' + label + ']'; + return "[" + label + "]"; +} + + +void InsetCitation::edit(BufferView * bv, int, int, unsigned int) +{ + bv->owner()->getDialogs()->showCitation(this); } -void InsetCitation::Edit(BufferView * bv, int, int, unsigned int) +void InsetCitation::edit(BufferView * bv, bool) { - bv->owner()->getDialogs()->showCitation( this ); + edit(bv, 0, 0, 0); } +int InsetCitation::ascii(Buffer const *, ostream & os, int) const +{ + os << "[" << getContents() << "]"; + return 0; +} + +// Have to overwrite the default InsetCommand method in order to check that +// the \cite command is valid. Eg, the user has natbib enabled, inputs some +// citations and then changes his mind, turning natbib support off. The output +// should revert to \cite[]{} +int InsetCitation::latex(Buffer const * buffer, ostream & os, + bool /*fragile*/, bool/*fs*/) const +{ + os << "\\"; + if (buffer->params.use_natbib) + os << getCmdName(); + else + os << "cite"; + + if (!getOptions().empty()) + os << "[" << getOptions() << "]"; + + // Paranoia check: make sure that there is no whitespace in here + string content; + for (string::const_iterator it = getContents().begin(); + it != getContents().end(); ++it) { + if (*it != ' ') content += *it; + } + + os << "{" << content << "}"; + + return 0; +} + + +void InsetCitation::validate(LaTeXFeatures & features) const +{ + if (features.bufferParams().use_natbib) + features.require("natbib"); +}