]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiListings.cpp
Do not compute caret geometry when we are not ready to do so.
[lyx.git] / src / frontends / qt4 / GuiListings.cpp
index 52c03e9508ccb4c6f13f1ab859cbc772bfaffbac..d41724f15bfa5a126aad3c097664efdcbea4f7bd 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Bo Peng
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "GuiListings.h"
 
 #include "qt_helpers.h"
+
+#include "Buffer.h"
+#include "BufferParams.h"
 #include "FuncRequest.h"
+
 #include "insets/InsetListings.h"
 #include "insets/InsetListingsParams.h"
-#include "support/debug.h"
 
+#include "support/convert.h"
+#include "support/debug.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
@@ -41,7 +46,7 @@ namespace frontend {
 /////////////////////////////////////////////////////////////////////
 
 
-char const * languages[] =
+char const * languages_supported[] =
 { "no language", "ABAP", "ACSL", "Ada", "ALGOL", "Assembler", "Awk", "bash", "Basic", "C",
   "C++", "Caml", "Clean", "Cobol", "Comal 80", "command.com", "Comsol", "csh", "Delphi",
   "Eiffel", "Elan", "erlang", "Euphoria", "Fortran", "Gnuplot", "Haskell", "HTML", "IDL", "inform",
@@ -196,19 +201,21 @@ GuiListings::GuiListings(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(spaceInStringCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
+       connect(tabsizeSB, SIGNAL(valueChanged(int)),
+               this, SLOT(change_adaptor()));
        connect(extendedcharsCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
 
        connect(listingsED,  SIGNAL(textChanged()),
                this, SLOT(change_adaptor()));
        connect(listingsED,  SIGNAL(textChanged()),
-               this, SLOT(set_listings_msg()));
+               this, SLOT(setListingsMsg()));
        connect(bypassCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
        connect(bypassCB, SIGNAL(clicked()),
-               this, SLOT(set_listings_msg()));
+               this, SLOT(setListingsMsg()));
 
-       for (int n = 0; languages[n][0]; ++n)
+       for (int n = 0; languages_supported[n][0]; ++n)
                languageCO->addItem(qt_(languages_gui[n]));
 
        for (int n = 0; font_styles[n][0]; ++n)
@@ -226,6 +233,7 @@ GuiListings::GuiListings(GuiView & lv)
        lastlineLE->setValidator(new QIntValidator(0, 1000000, this));
        placementLE->setValidator(new QRegExpValidator(QRegExp("[\\*tbph]*"), this));
 
+       bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
        bc().setOK(okPB);
        bc().setApply(applyPB);
        bc().setCancel(closePB);
@@ -245,7 +253,7 @@ void GuiListings::change_adaptor()
 
 string GuiListings::construct_params()
 {
-       string language = languages[languageCO->currentIndex()];
+       string language = languages_supported[qMax(0, languageCO->currentIndex())];
        string dialect;
        string const dialect_gui = fromqstr(dialectCO->currentText());
        if (dialectCO->currentIndex() > 0) {
@@ -265,7 +273,7 @@ string GuiListings::construct_params()
                placement = fromqstr(placementLE->text());
 
        string numberSide;
-       switch (numberSideCO->currentIndex()) {
+       switch (qMax(0, numberSideCO->currentIndex())) {
        case 0:
                numberSide = "none";
                break;
@@ -280,26 +288,52 @@ string GuiListings::construct_params()
                break;
        }
        string stepnumber = fromqstr(numberStepLE->text());
-       string numberfontsize = font_sizes[numberFontSizeCO->currentIndex()];
+       string numberfontsize = font_sizes[qMax(0, numberFontSizeCO->currentIndex())];
        string firstline = fromqstr(firstlineLE->text());
        string lastline = fromqstr(lastlineLE->text());
 
-       string fontsize = font_sizes[fontsizeCO->currentIndex()];
-       string fontstyle = font_styles[fontstyleCO->currentIndex()];
+       string fontsize = font_sizes[qMax(0, fontsizeCO->currentIndex())];
+       string fontstyle = font_styles[qMax(0, fontstyleCO->currentIndex())];
        string basicstyle;
-       if (fontsize != "default")
-               basicstyle = "\\" + fontsize;
-       if (fontstyle != "default")
-               basicstyle += "\\" + fontstyle;
+       string mintedsize;
+       bool const use_minted = buffer().params().use_minted;
+       if (fontsize != "default") {
+               if (use_minted)
+                       mintedsize = "\\" + fontsize;
+               else
+                       basicstyle = "\\" + fontsize;
+       }
+       if (fontstyle != "default") {
+               if (use_minted)
+                       basicstyle = fontstyle.substr(0, 2);
+               else
+                       basicstyle += "\\" + fontstyle;
+       }
        bool breakline = breaklinesCB->isChecked();
        bool space = spaceCB->isChecked();
+       int tabsize = tabsizeSB->value();
        bool spaceInString = spaceInStringCB->isChecked();
        bool extendedchars = extendedcharsCB->isChecked();
        string extra = fromqstr(listingsED->toPlainText());
 
        // compose a string
        InsetListingsParams par;
-       if (language != "no language" && !contains(extra, "language=")) {
+       par.setMinted(use_minted);
+       if (use_minted) {
+               if (language == "no language" && !contains(extra, "language=")) {
+                       string const & blp = buffer().params().listings_params;
+                       size_t start = blp.find("language=");
+                       if (start != string::npos) {
+                               start += strlen("language=");
+                               size_t len = blp.find(",", start);
+                               if (len != string::npos)
+                                       len -= start;
+                               par.addParam("language", blp.substr(start, len));
+                       } else
+                               par.addParam("language", "TeX");
+               } else
+                       par.addParam("language", language);
+       } else if (language != "no language" && !contains(extra, "language=")) {
                if (dialect.empty())
                        par.addParam("language", language);
                else
@@ -312,7 +346,7 @@ string GuiListings::construct_params()
                par.addParam("float", placement);
        if (numberSide != "none")
                par.addParam("numbers", numberSide);
-       if (numberfontsize != "default" && numberSide != "none")
+       if (numberfontsize != "default" && numberSide != "none" && !use_minted)
                par.addParam("numberstyle", "\\" + numberfontsize);
        if (!stepnumber.empty() && numberSide != "none")
                par.addParam("stepnumber", stepnumber);
@@ -321,14 +355,18 @@ string GuiListings::construct_params()
        if (!lastline.empty())
                par.addParam("lastline", lastline);
        if (!basicstyle.empty())
-               par.addParam("basicstyle", basicstyle);
+               par.addParam(use_minted ? "fontfamily" : "basicstyle", basicstyle);
+       if (!mintedsize.empty())
+               par.addParam("fontsize", mintedsize);
        if (breakline)
                par.addParam("breaklines", "true");
        if (space)
                par.addParam("showspaces", "true");
-       if (!spaceInString)
+       if (!spaceInString && !use_minted)
                par.addParam("showstringspaces", "false");
-       if (extendedchars)
+       if (tabsize != 8)
+               par.addParam("tabsize", convert<string>(tabsize));
+       if (extendedchars && !use_minted)
                par.addParam("extendedchars", "true");
        par.addParams(extra);
        return par.params();
@@ -337,25 +375,15 @@ string GuiListings::construct_params()
 
 docstring GuiListings::validate_listings_params()
 {
-       // use a cache here to avoid repeated validation
-       // of the same parameters
-       static string param_cache;
-       static docstring msg_cache;
-       
        if (bypassCB->isChecked())
                return docstring();
-
-       string params = construct_params();
-       if (params != param_cache) {
-               param_cache = params;
-               msg_cache = InsetListingsParams(params).validate();
-       }
-       return msg_cache;
+       return InsetListingsParams(construct_params()).validate();
 }
 
 
-void GuiListings::set_listings_msg()
+void GuiListings::setListingsMsg()
 {
+       // FIXME THREAD
        static bool isOK = true;
        docstring msg = validate_listings_params();
        if (msg.empty()) {
@@ -403,7 +431,7 @@ void GuiListings::on_languageCO_currentIndexChanged(int index)
        // 0 is "no dialect"
        int default_dialect = 0;
        dialectCO->addItem(qt_("No dialect"));
-       string const language = languages[index];
+       string const language = languages_supported[index];
 
        for (size_t i = 0; i != nr_dialects; ++i) {
                if (language == dialects[i].language) {
@@ -414,12 +442,14 @@ void GuiListings::on_languageCO_currentIndexChanged(int index)
                }
        }
        dialectCO->setCurrentIndex(default_dialect);
-       dialectCO->setEnabled(dialectCO->count() > 1);
+       dialectCO->setEnabled(dialectCO->count() > 1
+                             && !buffer().params().use_minted);
 }
 
 
 void GuiListings::applyView()
 {
+       params_.setMinted(buffer().params().use_minted);
        params_.setInline(inlineCB->isChecked());
        params_.setParams(construct_params());
 }
@@ -436,10 +466,11 @@ static string plainParam(string const & par)
 
 void GuiListings::updateContents()
 {
+       bool const use_minted = buffer().params().use_minted;
        // set default values
        listingsTB->setPlainText(
                qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
-       languageCO->setCurrentIndex(findToken(languages, "no language"));
+       languageCO->setCurrentIndex(findToken(languages_supported, "no language"));
        dialectCO->setCurrentIndex(0);
        floatCB->setChecked(false);
        placementLE->clear();
@@ -453,6 +484,7 @@ void GuiListings::updateContents()
        breaklinesCB->setChecked(false);
        spaceCB->setChecked(false);
        spaceInStringCB->setChecked(true);
+       tabsizeSB->setValue(8);
        extendedcharsCB->setChecked(false);
 
        // set values from param string
@@ -479,7 +511,7 @@ void GuiListings::updateContents()
                        } else {
                                language = arg;
                        }
-                       int n = findToken(languages, language);
+                       int n = findToken(languages_supported, language);
                        if (n >= 0) {
                                languageCO->setCurrentIndex(n);
                                in_gui = true;
@@ -503,7 +535,7 @@ void GuiListings::updateContents()
                        if (in_gui)
                                *it = "";
                        languageCO->setEnabled(in_gui);
-                       dialectCO->setEnabled(
+                       dialectCO->setEnabled(!use_minted &&
                                in_gui && dialectCO->count() > 1);
                } else if (prefixIs(*it, "float")) {
                        floatCB->setChecked(true);
@@ -571,6 +603,36 @@ void GuiListings::updateContents()
                                }
                                *it = "";
                        }
+               } else if (prefixIs(*it, "fontsize=")) {
+                       string size;
+                       for (int n = 0; font_sizes[n][0]; ++n) {
+                               string const s = font_sizes[n];
+                               if (contains(*it, "\\" + s)) {
+                                       size = "\\" + s;
+                                       break;
+                               }
+                       }
+                       if (!size.empty()) {
+                               int n = findToken(font_sizes, size.substr(1));
+                               if (n >= 0)
+                                       fontsizeCO->setCurrentIndex(n);
+                       }
+                       *it = "";
+               } else if (prefixIs(*it, "fontfamily=")) {
+                       string style;
+                       for (int n = 0; font_styles[n][0]; ++n) {
+                               string const s = font_styles[n];
+                               if (contains(*it, "=" + s.substr(0,2))) {
+                                       style = "\\" + s;
+                                       break;
+                               }
+                       }
+                       if (!style.empty()) {
+                               int n = findToken(font_styles, style.substr(1));
+                               if (n >= 0)
+                                       fontstyleCO->setCurrentIndex(n);
+                       }
+                       *it = "";
                } else if (prefixIs(*it, "breaklines=")) {
                        breaklinesCB->setChecked(contains(*it, "true"));
                        *it = "";
@@ -580,6 +642,9 @@ void GuiListings::updateContents()
                } else if (prefixIs(*it, "showstringspaces=")) {
                        spaceInStringCB->setChecked(contains(*it, "true"));
                        *it = "";
+               } else if (prefixIs(*it, "tabsize=")) {
+                       tabsizeSB->setValue(convert<int>(plainParam(it->substr(8))));
+                       *it = "";
                } else if (prefixIs(*it, "extendedchars=")) {
                        extendedcharsCB->setChecked(contains(*it, "true"));
                        *it = "";
@@ -587,11 +652,15 @@ void GuiListings::updateContents()
        }
 
        numberStepLE->setEnabled(numberSideCO->currentIndex() > 0);
-       numberFontSizeCO->setEnabled(numberSideCO->currentIndex() > 0);
+       numberFontSizeCO->setEnabled(numberSideCO->currentIndex() > 0
+                                    && !use_minted);
+       spaceInStringCB->setEnabled(!use_minted);
+       extendedcharsCB->setEnabled(!use_minted);
        // parameters that can be handled by widgets are cleared
        // the rest is put to the extra edit box.
        string extra = getStringFromVector(pars);
        listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
+       params_.setMinted(use_minted);
 }
 
 
@@ -603,7 +672,7 @@ bool GuiListings::isValid()
 
 bool GuiListings::initialiseParams(string const & data)
 {
-       InsetListingsMailer::string2params(data, params_);
+       InsetListings::string2params(data, params_);
        return true;
 }
 
@@ -611,12 +680,13 @@ bool GuiListings::initialiseParams(string const & data)
 void GuiListings::clearParams()
 {
        params_.clear();
+       params_.setMinted(buffer().params().use_minted);
 }
 
 
 void GuiListings::dispatchParams()
 {
-       string const lfun = InsetListingsMailer::params2string(params_);
+       string const lfun = InsetListings::params2string(params_);
        dispatch(FuncRequest(getLfun(), lfun));
 }
 
@@ -624,6 +694,7 @@ void GuiListings::dispatchParams()
 void GuiListings::setParams(InsetListingsParams const & params)
 {
        params_ = params;
+       params_.setMinted(buffer().params().use_minted);
 }
 
 
@@ -634,4 +705,4 @@ Dialog * createGuiListings(GuiView & lv) { return new GuiListings(lv); }
 } // namespace lyx
 
 
-#include "GuiListings_moc.cpp"
+#include "moc_GuiListings.cpp"