]> git.lyx.org Git - lyx.git/blob - src/insets/insetcite.C
Don't remove cell selections after fontchange.
[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
22 #include "frontends/Dialogs.h"
23
24 #include "support/lstrings.h"
25
26
27 using std::ostream;
28
29
30 InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
31         : InsetCommand(p)
32 {}
33
34 string const InsetCitation::getScreenLabel(Buffer const *) const
35 {
36         string keys(getContents());
37
38         // If keys is "too long" then only print out the first few tokens
39         string label;
40         if (contains(keys, ",")) {
41                 // Final comma allows while loop to cover all keys
42                 keys = frontStrip(split(keys, label, ',')) + ",";
43
44                 string::size_type const maxSize = 40;
45                 while (contains(keys, ",")) {
46                         string key;
47                         keys = frontStrip(split(keys, key, ','));
48
49                         string::size_type size = label.size() + 2 + key.size();
50                         if (size >= maxSize) {
51                                 label += ", ...";
52                                 break;
53                         }
54                         label += ", " + key;
55                 }
56         } else {
57                 label = keys;
58         }
59
60         if (!getOptions().empty())
61                 label += ", " + getOptions();
62
63         return "[" + label + "]";
64 }
65
66
67 void InsetCitation::edit(BufferView * bv, int, int, unsigned int)
68 {
69         bv->owner()->getDialogs()->showCitation(this);
70 }
71
72 void InsetCitation::edit(BufferView * bv, bool)
73 {
74         edit(bv, 0, 0, 0);
75 }
76
77 int InsetCitation::ascii(Buffer const *, ostream & os, int) const
78 {
79         os << "[" << getContents() << "]";
80         return 0;
81 }
82
83 // Have to overwrite the default InsetCommand method in order to check that
84 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
85 // citations and then changes his mind, turning natbib support off. The output
86 // should revert to \cite[]{}
87 int InsetCitation::latex(Buffer const * buffer, ostream & os,
88                         bool /*fragile*/, bool/*fs*/) const
89 {
90         os << "\\";
91         if (buffer->params.use_natbib)
92                 os << getCmdName();
93         else
94                 os << "cite";
95
96         if (!getOptions().empty())
97                 os << "[" << getOptions() << "]";
98
99         // Paranoia check: make sure that there is no whitespace in here
100         string content;
101         for (string::const_iterator it = getContents().begin();
102              it != getContents().end(); ++it) {
103                 if (*it != ' ') content += *it;
104         }
105         
106         os << "{" << content << "}";
107
108         return 0;
109 }
110
111
112 void InsetCitation::validate(LaTeXFeatures & features) const
113 {
114         if (features.bufferParams().use_natbib)
115                 features.require("natbib");
116 }