]> git.lyx.org Git - lyx.git/blob - src/insets/insetcite.C
591e450bba40914e359746f104c9272c836b18b6
[lyx.git] / src / insets / insetcite.C
1 /* This file is part of*
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000-2001 The LyX Team.
7  * 
8  * ====================================================== */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "insetcite.h"
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "LaTeXFeatures.h"
20 #include "LyXView.h"
21 #include "frontends/Dialogs.h"
22 #include "support/lstrings.h"
23
24 InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
25         : InsetCommand(p)
26 {}
27
28 string const InsetCitation::getScreenLabel() const
29 {
30         string keys(getContents());
31
32         // If keys is "too long" then only print out the first few tokens
33         string label;
34         if (contains(keys, ",")) {
35                 // Final comma allows while loop to cover all keys
36                 keys = frontStrip(split(keys, label, ',')) + ",";
37
38                 string::size_type const maxSize = 40;
39                 while (contains( keys, "," )) {
40                         string key;
41                         keys = frontStrip(split(keys, key, ','));
42
43                         string::size_type size = label.size() + 2 + key.size();
44                         if (size >= maxSize) {
45                                 label += ", ...";
46                                 break;
47                         }
48                         label += ", " + key;
49                 }
50         } else {
51                 label = keys;
52         }
53
54         if (!getOptions().empty())
55                 label += ", " + getOptions();
56
57         return "[" + label + "]";
58 }
59
60
61 void InsetCitation::edit(BufferView * bv, int, int, unsigned int)
62 {
63         bv->owner()->getDialogs()->showCitation(this);
64 }
65
66 int InsetCitation::ascii(Buffer const *, std::ostream & os, int) const
67 {
68         os << "[" << getContents() << "]";
69         return 0;
70 }
71
72 // Have to overwrite the default InsetCommand method in order to check that
73 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
74 // citations and then changes his mind, turning natbib support off. The output
75 // should revert to \cite[]{}
76 int InsetCitation::latex(Buffer const * buffer, std::ostream & os,
77                         bool /*fragile*/, bool/*fs*/) const
78 {
79         os << "\\";
80         if (buffer->params.use_natbib)
81                 os << getCmdName();
82         else
83                 os << "cite";
84
85         if (!getOptions().empty())
86                 os << "[" << getOptions() << "]";
87
88         os << "{" << getContents() << "}";
89
90         return 0;
91 }
92
93
94 void InsetCitation::validate(LaTeXFeatures & features) const
95 {
96         if (getCmdName() != "cite" && features.bufferParams().use_natbib)
97                 features.natbib = true;
98 }
99