]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
71b49ac8d8235a8edf83b6ac2cfbe5e8415d996f
[lyx.git] / src / insets / insetbibitem.C
1 /**
2  * \file insetbibitem.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  *
8  * Full author contact details are available in file CREDITS
9  */
10 #include <config.h>
11
12 #include "insetbibitem.h"
13 #include "buffer.h"
14 #include "BufferView.h"
15 #include "funcrequest.h"
16 #include "lyxlex.h"
17
18 #include "frontends/font_metrics.h"
19
20 #include "support/tostr.h"
21 #include "support/lstrings.h"
22
23
24 using std::max;
25
26
27 int InsetBibitem::key_counter = 0;
28 const string key_prefix = "key-";
29
30 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
31         : InsetCommand(p), counter(1)
32 {
33         if (getContents().empty())
34                 setContents(key_prefix + tostr(++key_counter));
35 }
36
37
38 InsetBibitem::~InsetBibitem()
39 {
40         InsetCommandMailer mailer("bibitem", *this);
41         mailer.hideDialog();
42 }
43
44
45 Inset * InsetBibitem::clone(Buffer const &, bool) const
46 {
47         InsetBibitem * b = new InsetBibitem(params());
48         b->setCounter(counter);
49         return b;
50 }
51
52
53 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
54 {
55         Inset::RESULT result = UNDISPATCHED;
56
57         switch (cmd.action) {
58         case LFUN_INSET_MODIFY: {
59                 InsetCommandParams p;
60                 InsetCommandMailer::string2params(cmd.argument, p);
61                 if (p.getCmdName().empty())
62                         break;
63
64                 if (view() && p.getContents() != params().getContents()) {
65                         view()->ChangeCitationsIfUnique(params().getContents(),
66                                                         p.getContents());
67                 }
68
69                 setParams(p);
70                 cmd.view()->updateInset(this);
71
72                 // We need to do a redraw because the maximum
73                 // InsetBibitem width could have changed
74 #warning please check you mean repaint() not update(),
75 #warning and whether the repaint() is needed at all
76                 cmd.view()->repaint();
77                 cmd.view()->fitCursor();
78
79                 result = DISPATCHED;
80         }
81         break;
82         default:
83                 result = InsetCommand::localDispatch(cmd);
84         }
85
86         return result;
87 }
88
89
90 void InsetBibitem::setCounter(int c)
91 {
92         counter = c;
93 }
94
95
96 // I'm sorry but this is still necessary because \bibitem is used also
97 // as a LyX 2.x command, and lyxlex is not enough smart to understand
98 // real LaTeX commands. Yes, that could be fixed, but would be a waste
99 // of time cause LyX3 won't use lyxlex anyway.  (ale)
100 void InsetBibitem::write(Buffer const *, std::ostream & os) const
101 {
102         os << "\n\\bibitem ";
103         if (!getOptions().empty())
104                 os << '[' << getOptions() << ']';
105         os << '{' << getContents() << "}\n";
106 }
107
108
109 // This is necessary here because this is written without begin_inset
110 // This should be changed!!! (Jug)
111 void InsetBibitem::read(Buffer const *, LyXLex & lex)
112 {
113         if (lex.eatLine()) {
114                 scanCommand(lex.getString());
115         } else {
116                 lex.printError("InsetCommand: Parse error: `$$Token'");
117         }
118
119         if (prefixIs(getContents(), key_prefix)) {
120                 int key = strToInt(getContents().substr(key_prefix.length()));
121                 key_counter = max(key_counter, key);
122         }
123 }
124
125
126 string const InsetBibitem::getBibLabel() const
127 {
128         return getOptions().empty() ? tostr(counter) : getOptions();
129 }
130
131
132 string const InsetBibitem::getScreenLabel(Buffer const *) const
133 {
134         return getContents() + " [" + getBibLabel() + ']';
135 }
136
137
138 void InsetBibitem::edit(BufferView * bv, int, int, mouse_button::state)
139 {
140         InsetCommandMailer mailer("bibitem", *this);
141         mailer.showDialog(bv);
142 }
143
144
145 void InsetBibitem::edit(BufferView * bv, bool)
146 {
147         edit(bv, 0, 0, mouse_button::none);
148 }
149
150
151 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
152 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
153 {
154         int w = 0;
155         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
156         // Does look like a hack? It is! (but will change at 0.13)
157         ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
158         ParagraphList::iterator end = bv->buffer()->paragraphs.end();
159         for (; it != end; ++it) {
160                 if (it->bibitem()) {
161                         int const wx = it->bibitem()->width(bv, font);
162                         if (wx > w)
163                                 w = wx;
164                 }
165         }
166         return w;
167 }
168
169
170 // ale070405
171 string const bibitemWidest(Buffer const * buffer)
172 {
173         int w = 0;
174         // Does look like a hack? It is! (but will change at 0.13)
175
176         InsetBibitem * bitem = 0;
177         LyXFont font;
178
179         ParagraphList::iterator it = buffer->paragraphs.begin();
180         ParagraphList::iterator end = buffer->paragraphs.end();
181         for (; it != end; ++it) {
182                 if (it->bibitem()) {
183                         int const wx =
184                                 font_metrics::width(it->bibitem()->getBibLabel(),
185                                                     font);
186                         if (wx > w) {
187                                 w = wx;
188                                 bitem = it->bibitem();
189                         }
190                 }
191         }
192
193         if (bitem && !bitem->getBibLabel().empty())
194                 return bitem->getBibLabel();
195
196         return "99";
197 }