]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcite.C
Don't remove cell selections after fontchange.
[lyx.git] / src / insets / insetcite.C
index 9f31ae2186234769310c7ce4f2a0a838704f2989..6eedb6df8ccf46d8bf539f5fe5370078359b374f 100644 (file)
@@ -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.
  * 
  * ====================================================== */
 
 #endif
 
 #include "insetcite.h"
-#include "LyXView.h"
+#include "buffer.h"
 #include "BufferView.h"
+#include "LaTeXFeatures.h"
+#include "LyXView.h"
+
 #include "frontends/Dialogs.h"
 
-InsetCitation::InsetCitation(string const & key, string const & note)
-               : InsetCommand("cite", key, note), dialogs_(0)
+#include "support/lstrings.h"
+
+
+using std::ostream;
+
+
+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, ",")) {
+               // Final comma allows while loop to cover all keys
+               keys = frontStrip(split(keys, label, ',')) + ",";
+
+               string::size_type const maxSize = 40;
+               while (contains(keys, ",")) {
+                       string key;
+                       keys = frontStrip(split(keys, key, ','));
+
+                       string::size_type size = label.size() + 2 + key.size();
+                       if (size >= maxSize) {
+                               label += ", ...";
+                               break;
+                       }
+                       label += ", " + key;
+               }
+       } else {
+               label = keys;
+       }
+
+       if (!getOptions().empty())
+               label += ", " + getOptions();
+
+       return "[" + label + "]";
 }
 
-InsetCitation::~InsetCitation()
+
+void InsetCitation::edit(BufferView * bv, int, int, unsigned int)
 {
-       if( dialogs_ != 0 )
-               dialogs_->hideCitation( this );
+       bv->owner()->getDialogs()->showCitation(this);
 }
 
-string InsetCitation::getScreenLabel() const
+void InsetCitation::edit(BufferView * bv, bool)
 {
-       string temp("[");
+       edit(bv, 0, 0, 0);
+}
 
-       temp += getContents();
+int InsetCitation::ascii(Buffer const *, ostream & os, int) const
+{
+        os << "[" << getContents() << "]";
+        return 0;
+}
 
-       if( !getOptions().empty() ) {
-               temp += ", " + getOptions();
+// 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 temp + ']';
+       return 0;
 }
 
-void InsetCitation::Edit(BufferView * bv, int, int, unsigned int)
+
+void InsetCitation::validate(LaTeXFeatures & features) const
 {
-       dialogs_ = bv->owner()->getDialogs();
-       dialogs_->showCitation( this );
+       if (features.bufferParams().use_natbib)
+               features.require("natbib");
 }
-