]> git.lyx.org Git - features.git/commitdiff
InsetNomencl: calculate the needed symbol width for \printnomenclature, fixes #5909
authorUwe Stöhr <uwestoehr@web.de>
Thu, 21 May 2009 19:33:09 +0000 (19:33 +0000)
committerUwe Stöhr <uwestoehr@web.de>
Thu, 21 May 2009 19:33:09 +0000 (19:33 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29770 a592a061-630c-0410-9148-cb99ea01b6c8

src/insets/InsetNomencl.cpp
src/insets/InsetNomencl.h

index eae20721ac4412d8a18a7cd389c6ad3a440bde8b..bc2e0bb00a191ffe353ede6b401bf4eb92d178ad 100644 (file)
@@ -5,6 +5,8 @@
  *
  * \author Lars Gullik Bjønnes
  * \author O. U. Baran
+ * \author Uwe Stöhr
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -17,6 +19,7 @@
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "InsetIterator.h"
+#include "InsetList.h"
 #include "LaTeXFeatures.h"
 #include "MetricsInfo.h"
 #include "sgml.h"
@@ -116,16 +119,16 @@ void InsetNomencl::validate(LaTeXFeatures & features) const
 /////////////////////////////////////////////////////////////////////
 
 InsetPrintNomencl::InsetPrintNomencl(InsetCommandParams const & p)
-       : InsetCommand(p, string())
+       : InsetCommand(p, "printnomenclature")
 {}
 
 
 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
 {
+       // there are no parameters to give because the symbol width is set via
+       // nomencl's \nomlabelwidth in InsetPrintNomencl::latex and not as
+       // optional parameter of \printnomenclature
        static ParamInfo param_info_;
-       if (param_info_.empty()) {
-               param_info_.add("labelwidth", ParamInfo::LATEX_REQUIRED);
-       }
        return param_info_;
 }
 
@@ -160,6 +163,59 @@ int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
 }
 
 
+docstring nomenclWidest(Buffer const & buffer)
+{
+       // nomenclWidest() determines and returns the widest used nomenclature
+       // symbol in the document
+
+       int w = 0;
+       docstring symb;
+       InsetNomencl const * nomencl = 0;
+
+       ParagraphList::const_iterator it = buffer.paragraphs().begin();
+       ParagraphList::const_iterator end = buffer.paragraphs().end();
+
+       for (; it != end; ++it) {
+               if (it->insetList().empty())
+                       continue;
+               InsetList::const_iterator iit = it->insetList().begin();
+               InsetList::const_iterator eend = it->insetList().end();
+               for (; iit != eend; ++iit) {
+                       Inset * inset = iit->inset;
+                       if (inset->lyxCode() != NOMENCL_CODE)
+                               continue;
+                       nomencl = static_cast<InsetNomencl const *>(inset);
+                       docstring const symbol = nomencl->getParam("symbol");
+                       int const wx = symbol.size();
+                       if (wx > w) {
+                               w = wx;
+                               symb = symbol;
+                       }
+               }
+       }
+
+       // return the widest symbol
+       return symb;
+}
+
+
+int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
+{
+       int lines = 0;
+       // this must be output before the command \printnomenclature
+       docstring widest = nomenclWidest(buffer());
+       if (!widest.empty()) {
+               // set the label width via nomencl's command \nomlabelwidth
+               os << "\\settowidth{\\nomlabelwidth}{";
+               os << widest <<"}\n";
+               ++lines;
+       }
+       // output the command \printnomenclature
+       os << getCommand();
+       return lines;
+}
+
+
 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
 {
        features.require("nomencl");
index d63e99c2496060955409d07762f67ecd7b67c8f4..c81657157f770ac4928e26b93b8a4dff33af4a30 100644 (file)
@@ -61,10 +61,6 @@ public:
        InsetPrintNomencl(InsetCommandParams const &);
        /// Updates needed features for this inset.
        void validate(LaTeXFeatures & features) const;
-       // FIXME: This should be editable to set the label width (stored
-       // in params_["labelwidth"]).
-       // Currently the width can be read from file and written, but not
-       // changed.
        ///
        int docbook(odocstream &, OutputParams const &) const;
        ///
@@ -80,10 +76,17 @@ public:
        ///
        static bool isCompatibleCommand(std::string const & s) 
                { return s == "printnomenclature"; }
+       ///
+       int latex(odocstream &, OutputParams const &) const;
 private:
        Inset * clone() const { return new InsetPrintNomencl(*this); }
+       ///
+       friend docstring nomenclWidest(Buffer const & buffer);
 };
 
+/// return the widest symbol of all nomenclature entries of the document
+docstring nomenclWidest(Buffer const &);
+
 
 } // namespace lyx