]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
Minimal fix needed to give Qt a label dialog again.
[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         if (cmd.action != LFUN_INSET_MODIFY)
55                 return UNDISPATCHED;
56
57         InsetCommandParams p;
58         InsetCommandMailer::string2params(cmd.argument, p);
59         if (p.getCmdName().empty())
60                 return UNDISPATCHED;
61
62         if (view() && p.getContents() != params().getContents()) {
63                 view()->ChangeCitationsIfUnique(params().getContents(),
64                                                 p.getContents());
65         }
66
67         setParams(p);
68         if (view())
69                 view()->updateInset(this, true);
70
71         return DISPATCHED;
72 }
73
74
75 void InsetBibitem::setCounter(int c)
76 {
77         counter = c;
78 }
79
80
81 // I'm sorry but this is still necessary because \bibitem is used also
82 // as a LyX 2.x command, and lyxlex is not enough smart to understand
83 // real LaTeX commands. Yes, that could be fixed, but would be a waste
84 // of time cause LyX3 won't use lyxlex anyway.  (ale)
85 void InsetBibitem::write(Buffer const *, std::ostream & os) const
86 {
87         os << "\n\\bibitem ";
88         if (!getOptions().empty())
89                 os << '[' << getOptions() << ']';
90         os << '{' << getContents() << "}\n";
91 }
92
93
94 // This is necessary here because this is written without begin_inset
95 // This should be changed!!! (Jug)
96 void InsetBibitem::read(Buffer const *, LyXLex & lex)
97 {
98         if (lex.eatLine()) {
99                 scanCommand(lex.getString());
100         } else {
101                 lex.printError("InsetCommand: Parse error: `$$Token'");
102         }
103
104         if (prefixIs(getContents(), key_prefix)) {
105                 int key = strToInt(getContents().substr(key_prefix.length()));
106                 key_counter = max(key_counter, key);
107         }
108 }
109
110
111 string const InsetBibitem::getBibLabel() const
112 {
113         return getOptions().empty() ? tostr(counter) : getOptions();
114 }
115
116
117 string const InsetBibitem::getScreenLabel(Buffer const *) const
118 {
119         return getContents() + " [" + getBibLabel() + ']';
120 }
121
122
123 void InsetBibitem::edit(BufferView *, int, int, mouse_button::state)
124 {
125         InsetCommandMailer mailer("bibitem", *this);
126         mailer.showDialog();
127 }
128
129
130 void InsetBibitem::edit(BufferView * bv, bool)
131 {
132         edit(bv, 0, 0, mouse_button::none);
133 }
134
135
136 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
137 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
138 {
139         int w = 0;
140         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
141         // Does look like a hack? It is! (but will change at 0.13)
142         ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
143         ParagraphList::iterator end = bv->buffer()->paragraphs.end();
144         for (; it != end; ++it) {
145                 if (it->bibitem()) {
146                         int const wx = it->bibitem()->width(bv, font);
147                         if (wx > w)
148                                 w = wx;
149                 }
150         }
151         return w;
152 }
153
154
155 // ale070405
156 string const bibitemWidest(Buffer const * buffer)
157 {
158         int w = 0;
159         // Does look like a hack? It is! (but will change at 0.13)
160
161         InsetBibitem * bitem = 0;
162         LyXFont font;
163
164         ParagraphList::iterator it = buffer->paragraphs.begin();
165         ParagraphList::iterator end = buffer->paragraphs.end();
166         for (; it != end; ++it) {
167                 if (it->bibitem()) {
168                         int const wx =
169                                 font_metrics::width(it->bibitem()->getBibLabel(),
170                                                     font);
171                         if (wx > w) {
172                                 w = wx;
173                                 bitem = it->bibitem();
174                         }
175                 }
176         }
177
178         if (bitem && !bitem->getBibLabel().empty())
179                 return bitem->getBibLabel();
180
181         return "99";
182 }