]> git.lyx.org Git - lyx.git/blob - src/insets/insetcite.C
2001-12-28 Lars Gullik Bj�nnes <larsbj@birdstep.com>
[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(Buffer const *) 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 void InsetCitation::edit(BufferView * bv, bool)
67 {
68         edit(bv, 0, 0, 0);
69 }
70
71 int InsetCitation::ascii(Buffer const *, std::ostream & os, int) const
72 {
73         os << "[" << getContents() << "]";
74         return 0;
75 }
76
77 // Have to overwrite the default InsetCommand method in order to check that
78 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
79 // citations and then changes his mind, turning natbib support off. The output
80 // should revert to \cite[]{}
81 int InsetCitation::latex(Buffer const * buffer, std::ostream & os,
82                         bool /*fragile*/, bool/*fs*/) const
83 {
84         os << "\\";
85         if (buffer->params.use_natbib)
86                 os << getCmdName();
87         else
88                 os << "cite";
89
90         if (!getOptions().empty())
91                 os << "[" << getOptions() << "]";
92
93         // Paranoia check: make sure that there is no whitespace in here
94         string content;
95         for (string::const_iterator it = getContents().begin();
96              it != getContents().end(); ++it) {
97                 if (*it != ' ') content += *it;
98         }
99         
100         os << "{" << content << "}";
101
102         return 0;
103 }
104
105
106 void InsetCitation::validate(LaTeXFeatures & features) const
107 {
108         if (features.bufferParams().use_natbib)
109                 features.require("natbib");
110 }