* Licence details can be found in the file COPYING.
*
* \author Bo Peng
+ * \author Jürgen Spitzmüller
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "InsetListings.h"
+#include "InsetCaption.h"
#include "Language.h"
#include "gettext.h"
#include "DispatchResult.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
+#include "MetricsInfo.h"
#include "Cursor.h"
#include "support/lstrings.h"
void InsetListings::init()
{
- // FIXME: define Color::listing?
+ setButtonLabel();
Font font(Font::ALL_SANE);
font.decSize();
font.decSize();
- font.setColor(Color::foreground);
+ font.setColor(Color::none);
setLabelFont(font);
- // FIXME: english_language?
- text_.current_font.setLanguage(english_language);
- text_.real_current_font.setLanguage(english_language);
- // FIXME: why I can not make text of source code black with the following two lines?
- text_.current_font.setColor(Color::foreground);
- text_.real_current_font.setColor(Color::foreground);
+ text_.current_font.setLanguage(latex_language);
+ text_.real_current_font.setLanguage(latex_language);
}
InsetListings::InsetListings(InsetListings const & in)
- : InsetERT(in)
+ : InsetERT(in), params_(in.params_)
{
init();
}
}
-bool InsetListings::display() const
+Inset::DisplayType InsetListings::display() const
{
- return !params().isInline();
+ return params().isInline() || params().isFloat() ? Inline : AlignLeft;
}
break;
}
}
- InsetCollapsable::read(buf, lex);
+ InsetERT::read(buf, lex);
}
}
-int InsetListings::latex(Buffer const &, odocstream & os,
- OutputParams const &) const
+int InsetListings::latex(Buffer const & buf, odocstream & os,
+ OutputParams const & runparams) const
{
string param_string = params().encodedString();
// NOTE: I use {} to quote text, which is an experimental feature
else
os << "\\lstinline[" << from_ascii(param_string) << "]{";
} else {
- if (param_string.empty())
+ docstring const caption = getCaption(buf, runparams);
+ if (param_string.empty() && caption.empty())
os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
- else
- os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[" << from_ascii(param_string) << "]\n";
+ else {
+ os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
+ if (!caption.empty()) {
+ os << "caption={" << caption << '}';
+ if (!param_string.empty())
+ os << ',';
+ }
+ os << from_utf8(param_string) << "]\n";
+ }
lines += 4;
}
ParagraphList::const_iterator par = paragraphs().begin();
while (par != end) {
pos_type siz = par->size();
+ bool captionline = false;
for (pos_type i = 0; i < siz; ++i) {
- // ignore all struck out text
- if (par->isDeleted(i))
+ if (i == 0 && par->isInset(i) && i + 1 == siz)
+ captionline = true;
+ // ignore all struck out text and (caption) insets
+ if (par->isDeleted(i) || par->isInset(i))
continue;
os.put(par->getChar(i));
}
++par;
// for the inline case, if there are multiple paragraphs
- // they are simply joined. Otherwise, expect latex errors.
- if (par != end && !lstinline) {
+ // they are simply joined. Otherwise, expect latex errors.
+ if (par != end && !lstinline && !captionline) {
os << "\n";
++lines;
}
case LFUN_INSET_DIALOG_UPDATE:
status.enabled(true);
return true;
+ case LFUN_CAPTION_INSERT:
+ status.enabled(!params().isInline());
+ return true;
default:
return InsetERT::getStatus(cur, cmd, status);
}
}
+bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+ Font tmpfont = mi.base.font;
+ getDrawFont(mi.base.font);
+ mi.base.font.realize(tmpfont);
+ InsetCollapsable::metrics(mi, dim);
+ mi.base.font = tmpfont;
+ bool const changed = dim_ != dim;
+ dim_ = dim;
+ return changed;
+}
+
+
+void InsetListings::draw(PainterInfo & pi, int x, int y) const
+{
+ Font tmpfont = pi.base.font;
+ getDrawFont(pi.base.font);
+ pi.base.font.realize(tmpfont);
+ InsetCollapsable::draw(pi, x, y);
+ pi.base.font = tmpfont;
+}
+
+
void InsetListings::validate(LaTeXFeatures & features) const
{
features.require("listings");
void InsetListings::getDrawFont(Font & font) const
{
- font = Font(Font::ALL_INHERIT, english_language);
+ font = Font(Font::ALL_INHERIT, latex_language);
font.setFamily(Font::TYPEWRITER_FAMILY);
+ // FIXME: define Color::listing?
font.setColor(Color::foreground);
}
+docstring InsetListings::getCaption(Buffer const & buf,
+ OutputParams const & runparams) const
+{
+ if (paragraphs().empty())
+ return docstring();
+
+ ParagraphList::const_iterator pit = paragraphs().begin();
+ for (; pit != paragraphs().end(); ++pit) {
+ InsetList::const_iterator it = pit->insetlist.begin();
+ for (; it != pit->insetlist.end(); ++it) {
+ Inset & inset = *it->inset;
+ if (inset.lyxCode() == Inset::CAPTION_CODE) {
+ odocstringstream ods;
+ InsetCaption * ins =
+ static_cast<InsetCaption *>(it->inset);
+ ins->getOptArg(buf, ods, runparams);
+ ins->getArgument(buf, ods, runparams);
+ return ods.str();
+ }
+ }
+ }
+ return docstring();
+}
+
+
string const InsetListingsMailer::name_("listings");
InsetListingsMailer::InsetListingsMailer(InsetListings & inset)