From 72e0b87a90ef78658ad7ea25d0cc1f17c0ef6362 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Mon, 14 May 2007 20:42:14 +0000 Subject: [PATCH] Separate caption and label from InsetListingsParams and handle them separately in listings and Include dialog, from Jurgen git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18324 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/layouts/stdcounters.inc | 4 + src/Text.cpp | 2 + src/buffer_funcs.cpp | 2 + src/frontends/qt4/QInclude.cpp | 51 ++++++++--- src/frontends/qt4/QListings.cpp | 45 +++------- src/frontends/qt4/ui/IncludeUi.ui | 134 +++++++++++++++++++++-------- src/frontends/qt4/ui/ListingsUi.ui | 85 +----------------- src/insets/InsetCaption.cpp | 14 +++ src/insets/InsetCaption.h | 6 ++ src/insets/InsetListings.cpp | 59 +++++++++++-- src/insets/InsetListings.h | 2 + src/insets/InsetListingsParams.cpp | 20 +++-- 12 files changed, 244 insertions(+), 180 deletions(-) diff --git a/lib/layouts/stdcounters.inc b/lib/layouts/stdcounters.inc index f5a41ca8a4..9c8801f460 100644 --- a/lib/layouts/stdcounters.inc +++ b/lib/layouts/stdcounters.inc @@ -76,3 +76,7 @@ End Counter Name equation End + +Counter + Name listing +End diff --git a/src/Text.cpp b/src/Text.cpp index 0512915623..6c810b9869 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -1871,6 +1871,8 @@ docstring Text::getPossibleLabel(Cursor & cur) const name = from_ascii("thm"); else if (name == "Foot") name = from_ascii("fn"); + else if (name == "listing") + name = from_ascii("lst"); if (!name.empty()) text = name.substr(0, 3) + ':' + text; diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index f8469d702e..6527018748 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -417,6 +417,8 @@ void setCaptions(Paragraph & par, TextClass const & textclass) // FIXME: are "table" and "Table" the correct type and label? setCaptionLabels(inset, "table", from_ascii("Table"), counters); } + else if (inset.lyxCode() == Inset::LISTINGS_CODE) + setCaptionLabels(inset, "listing", from_ascii("Listing"), counters); } } diff --git a/src/frontends/qt4/QInclude.cpp b/src/frontends/qt4/QInclude.cpp index 007a89ae8e..1680e7e7b1 100644 --- a/src/frontends/qt4/QInclude.cpp +++ b/src/frontends/qt4/QInclude.cpp @@ -11,6 +11,7 @@ #include #include "support/os.h" +#include "support/lstrings.h" #include "QInclude.h" @@ -30,9 +31,12 @@ #include using std::string; +using std::vector; using lyx::support::os::internal_path; - +using lyx::support::prefixIs; +using lyx::support::getStringFromVector; +using lyx::support::getVectorFromString; namespace lyx { namespace frontend { @@ -58,6 +62,8 @@ QIncludeDialog::QIncludeDialog(QInclude * form) connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor())); connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int))); connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); + connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor())); + connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor())); connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor())); connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params())); @@ -85,13 +91,11 @@ void QIncludeDialog::validate_listings_params() InsetListingsParams par(fromqstr(listingsED->toPlainText())); if (!isOK) { isOK = true; - // listingsTB->setTextColor("black"); - listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters."); + listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters."); okPB->setEnabled(true); } } catch (invalidParam & e) { isOK = false; - // listingsTB->setTextColor("red"); listingsTB->setPlainText(e.what()); okPB->setEnabled(false); } @@ -115,7 +119,6 @@ void QIncludeDialog::typeChanged(int v) previewCB->setEnabled(false); previewCB->setChecked(false); listingsGB->setEnabled(false); - listingsED->setEnabled(false); break; //case Input case 1: @@ -123,7 +126,6 @@ void QIncludeDialog::typeChanged(int v) visiblespaceCB->setChecked(false); previewCB->setEnabled(true); listingsGB->setEnabled(false); - listingsED->setEnabled(false); break; //case listings case 3: @@ -131,7 +133,6 @@ void QIncludeDialog::typeChanged(int v) visiblespaceCB->setChecked(false); previewCB->setEnabled(false); listingsGB->setEnabled(true); - listingsED->setEnabled(true); break; //case Verbatim default: @@ -139,7 +140,6 @@ void QIncludeDialog::typeChanged(int v) previewCB->setEnabled(false); previewCB->setChecked(false); listingsGB->setEnabled(false); - listingsED->setEnabled(false); break; } } @@ -183,7 +183,7 @@ void QInclude::build_dialog() bcview().addReadOnly(dialog_->visiblespaceCB); bcview().addReadOnly(dialog_->typeCO); bcview().addReadOnly(dialog_->listingsED); - dialog_->listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters."); + dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters."); addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA); } @@ -235,7 +235,29 @@ void QInclude::update_contents() dialog_->listingsGB->setEnabled(true); dialog_->listingsED->setEnabled(true); InsetListingsParams par(params.getOptions()); - dialog_->listingsED->setPlainText(toqstr(par.separatedParams())); + // extract caption and label and put them into their respective editboxes + vector pars = getVectorFromString(par.separatedParams(), "\n"); + for (vector::iterator it = pars.begin(); + it != pars.end(); ++it) { + if (prefixIs(*it, "caption=")) { + string cap = it->substr(8); + if (cap[0] == '{' && cap[cap.size()-1] == '}') + dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2))); + else + throw invalidParam("caption parameter is not quoted with braces"); + *it = ""; + } else if (prefixIs(*it, "label=")) { + string lbl = it->substr(6); + if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') + dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2))); + else + throw invalidParam("label parameter is not quoted with braces"); + *it = ""; + } + } + // the rest is put to the extra edit box. + string extra = getStringFromVector(pars); + dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams())); } } @@ -257,7 +279,14 @@ void QInclude::apply() } else if (item == 3) { params.setCmdName("lstinputlisting"); // the parameter string should have passed validation - params.setOptions(InsetListingsParams(fromqstr(dialog_->listingsED->toPlainText())).params()); + InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText())); + string caption = fromqstr(dialog_->captionLE->text()); + string label = fromqstr(dialog_->labelLE->text()); + if (!caption.empty()) + par.addParam("caption", "{" + caption + "}"); + if (!label.empty()) + par.addParam("label", "{" + label + "}"); + params.setOptions(par.params()); } else { if (dialog_->visiblespaceCB->isChecked()) params.setCmdName("verbatiminput*"); diff --git a/src/frontends/qt4/QListings.cpp b/src/frontends/qt4/QListings.cpp index 7398d5417d..d9e6907656 100644 --- a/src/frontends/qt4/QListings.cpp +++ b/src/frontends/qt4/QListings.cpp @@ -4,6 +4,7 @@ * 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. */ @@ -78,8 +79,6 @@ QListingsDialog::QListingsDialog(QListings * form) connect(breaklinesCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); connect(spaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); connect(extendedcharsCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); - connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor())); - connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor())); connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor())); connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params())); @@ -108,7 +107,7 @@ string QListingsDialog::construct_params() bool left = numberLeftCB->checkState() == Qt::Checked; bool right = numberRightCB->checkState() == Qt::Checked; - string step = fromqstr(numberStepLE->text()); + string stepnumber = fromqstr(numberStepLE->text()); string numberfontsize = fromqstr(numberFontSizeCO->currentText()); string firstline = fromqstr(firstlineLE->text()); string lastline = fromqstr(lastlineLE->text()); @@ -122,11 +121,7 @@ string QListingsDialog::construct_params() basicstyle += "\\" + fontstyle; bool breakline = breaklinesCB->checkState() == Qt::Checked; bool space = spaceCB->checkState() == Qt::Checked; - bool extendedchar = extendedcharsCB->checkState() == Qt::Checked; - - string caption = fromqstr(captionLE->text()); - string label = fromqstr(labelLE->text()); - + bool extendedchars = extendedcharsCB->checkState() == Qt::Checked; string extra = fromqstr(listingsED->toPlainText()); // compose a string @@ -147,18 +142,16 @@ string QListingsDialog::construct_params() par.addParam("firstline", firstline); if (!lastline.empty()) par.addParam("lastline", lastline); - if (basicstyle != "") + if (!stepnumber.empty()) + par.addParam("stepnumber", stepnumber); + if (!basicstyle.empty()) par.addParam("basicstyle", basicstyle); if (breakline) par.addParam("breaklines", "true"); if (space) par.addParam("showspaces", "true"); - if (extendedchar) + if (extendedchars) par.addParam("extendedchars", "true"); - if (!caption.empty()) - par.addParam("caption", "{" + caption + "}"); - if (!label.empty()) - par.addParam("label", "{" + label + "}"); par.addParams(extra); return par.params(); } @@ -171,13 +164,11 @@ void QListingsDialog::validate_listings_params() InsetListingsParams par(construct_params()); if (!isOK) { isOK = true; - // listingsTB->setTextColor("black"); - listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters."); + listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters."); okPB->setEnabled(true); } } catch (invalidParam & e) { isOK = false; - // listingsTB->setTextColor("red"); listingsTB->setPlainText(e.what()); okPB->setEnabled(false); } @@ -203,7 +194,7 @@ void QListings::build_dialog() bcview().setOK(dialog_->okPB); bcview().setCancel(dialog_->closePB); - dialog_->listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters."); + dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters."); update_contents(); } @@ -257,7 +248,7 @@ void QListings::update_contents() dialog_->placementLE->setValidator(new QRegExpValidator(QRegExp("[tbph]*"), this)); // - dialog_->listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters."); + dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters."); // set values from param string InsetListingsParams & params = controller().params(); @@ -345,22 +336,6 @@ void QListings::update_contents() } else if (prefixIs(*it, "extendedchars=")) { dialog_->extendedcharsCB->setChecked(contains(*it, "true")); *it = ""; - } else if (prefixIs(*it, "caption=")) { - string cap = it->substr(8); - if ((cap[0] == '{' && cap[cap.size()-1] == '}') || - (cap[0] == '"' && cap[cap.size()-1] == '"') ) - dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2))); - else - dialog_->captionLE->setText(toqstr(cap)); - *it = ""; - } else if (prefixIs(*it, "label=")) { - string lbl = it->substr(6); - if ((lbl[0] == '{' && lbl[lbl.size()-1] == '}') || - (lbl[0] == '"' && lbl[lbl.size()-1] == '"') ) - dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2))); - else - dialog_->labelLE->setText(toqstr(lbl)); - *it = ""; } } // parameters that can be handled by widgets are cleared diff --git a/src/frontends/qt4/ui/IncludeUi.ui b/src/frontends/qt4/ui/IncludeUi.ui index 89be7cf080..bbbeaeae09 100644 --- a/src/frontends/qt4/ui/IncludeUi.ui +++ b/src/frontends/qt4/ui/IncludeUi.ui @@ -5,8 +5,8 @@ 0 0 - 389 - 385 + 315 + 379 @@ -237,51 +237,111 @@ 6 - - - - 0 - 0 - - - - - 200 - 16777215 - - - - 0 - - - false - - - QFrame::NoFrame - - - QFrame::Plain - - + + 0 - - false + + 6 - + + + + Label + + + + + + + + 150 + 0 + + + + + + + + Caption + + + + + + + - - + + + Qt::Horizontal + + - 0 - 0 + 20 + 20 - + + + + + More parameters + + + + + + + Qt::Horizontal + + + + + 0 + 0 + + + + + 200 + 16777215 + + + + 0 + + + false + + + QFrame::NoFrame + + + QFrame::Plain + + + 0 + + + false + + + + + + 0 + 0 + + + + + @@ -339,6 +399,8 @@ loadPB visiblespaceCB previewCB + captionLE + labelLE listingsTB listingsED diff --git a/src/frontends/qt4/ui/ListingsUi.ui b/src/frontends/qt4/ui/ListingsUi.ui index bcdc72a23c..39cb60b48a 100644 --- a/src/frontends/qt4/ui/ListingsUi.ui +++ b/src/frontends/qt4/ui/ListingsUi.ui @@ -8,8 +8,8 @@ 0 0 - 499 - 355 + 511 + 325 @@ -213,9 +213,6 @@ false - - captionLE - @@ -404,79 +401,6 @@ - - - - Display - - - - 9 - - - 6 - - - - - &Label: - - - false - - - labelLE - - - - - - - &Caption: - - - false - - - captionLE - - - - - - - - 150 - 0 - - - - A caption for the List of Listings - - - - - - - - - - - 150 - 0 - - - - A Label for the caption - - - - - - - - - @@ -599,10 +523,11 @@ okPB closePB - languageCO + listingsTW inlineCB floatCB placementLE + languageCO numberLeftCB numberRightCB numberStepLE @@ -614,8 +539,6 @@ breaklinesCB spaceCB extendedcharsCB - captionLE - labelLE listingsTB listingsED diff --git a/src/insets/InsetCaption.cpp b/src/insets/InsetCaption.cpp index 19bcc3859c..56d8543280 100644 --- a/src/insets/InsetCaption.cpp +++ b/src/insets/InsetCaption.cpp @@ -278,6 +278,20 @@ int InsetCaption::docbook(Buffer const & buf, odocstream & os, } +int InsetCaption::getArgument(Buffer const & buf, odocstream & os, + OutputParams const & runparams) const +{ + return InsetText::latex(buf, os, runparams); +} + + +int InsetCaption::getOptArg(Buffer const & buf, odocstream & os, + OutputParams const & runparams) const +{ + return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1); +} + + void InsetCaption::computeFullLabel(Buffer const & buf) const { if (type_.empty()) diff --git a/src/insets/InsetCaption.h b/src/insets/InsetCaption.h index 296ca8a802..dabfa39e99 100644 --- a/src/insets/InsetCaption.h +++ b/src/insets/InsetCaption.h @@ -69,6 +69,12 @@ public: /// int docbook(Buffer const & buf, odocstream & os, OutputParams const & runparams) const; + /// return the mandatory argument (LaTeX format) only + int getArgument(Buffer const & buf, odocstream & os, + OutputParams const &) const; + /// return the optional argument(s) only + int getOptArg(Buffer const & buf, odocstream & os, + OutputParams const &) const; /// void setCount(int c) { counter_ = c; } /// diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp index 30ba1a2a4e..baf9ff9860 100644 --- a/src/insets/InsetListings.cpp +++ b/src/insets/InsetListings.cpp @@ -4,6 +4,7 @@ * 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. */ @@ -11,6 +12,7 @@ #include #include "InsetListings.h" +#include "InsetCaption.h" #include "Language.h" #include "gettext.h" @@ -127,8 +129,8 @@ docstring const InsetListings::editMessage() const } -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 @@ -141,10 +143,18 @@ int InsetListings::latex(Buffer const &, odocstream & os, 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_ascii(param_string) << "]\n"; + } lines += 4; } ParagraphList::const_iterator par = paragraphs().begin(); @@ -152,16 +162,19 @@ int InsetListings::latex(Buffer const &, odocstream & os, 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; } @@ -210,6 +223,9 @@ bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd, 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); } @@ -245,6 +261,31 @@ void InsetListings::getDrawFont(Font & font) const } +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(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) diff --git a/src/insets/InsetListings.h b/src/insets/InsetListings.h index 1d02c7cd4f..6bbca9d5fd 100644 --- a/src/insets/InsetListings.h +++ b/src/insets/InsetListings.h @@ -67,6 +67,8 @@ private: /// void setButtonLabel(); /// + docstring getCaption(Buffer const &, OutputParams const &) const; + /// InsetListingsParams params_; }; diff --git a/src/insets/InsetListingsParams.cpp b/src/insets/InsetListingsParams.cpp index 90a79dc8c1..6c41db8b0f 100644 --- a/src/insets/InsetListingsParams.cpp +++ b/src/insets/InsetListingsParams.cpp @@ -33,12 +33,12 @@ namespace lyx { enum param_type { - ALL, - TRUEFALSE, - INTEGER, - LENGTH, - ONEOF, - SUBSETOF, + ALL, // accept all + TRUEFALSE, // accept 'true' or 'false' + INTEGER, // accept an integer + LENGTH, // accept an latex length + ONEOF, // accept one of a few values + SUBSETOF, // accept a string composed of given characters }; @@ -148,8 +148,12 @@ listings_param_info const listings_param_table[] = { { "name", "", false, ALL, "", "" }, { "thelstnumber", "", false, ALL, "", "" }, { "title", "", false, ALL, "", "" }, - { "caption", "", false, ALL, "", "" }, - { "label", "", false, ALL, "", "" }, + // this option is not handled in the parameter box + { "caption", "", false, ALL, "", "This parameter should not be entered here. " + "Please use caption editbox (Include dialog) or insert->caption (listings inset)" }, + // this option is not handled in the parameter box + { "label", "", false, ALL, "", "This parameter should not be entered here." + "Please use label editbox (Include dialog) or insert->caption (listings inset)"}, { "nolol", "", false, TRUEFALSE, "", "" }, { "captionpos", "", false, SUBSETOF, "tb", "" }, { "abovecaptionskip", "", false, LENGTH, "", "" }, -- 2.39.2