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