]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
513a4efd74ea39e3c13a6f2a8f681711360b645e
[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 "lyxlex.h"
16
17 #include "frontends/font_metrics.h"
18
19 #include "support/lstrings.h"
20
21
22 using std::max;
23
24
25 int InsetBibitem::key_counter = 0;
26 const string key_prefix = "key-";
27
28 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
29         : InsetCommand(p), counter(1)
30 {
31         if (getContents().empty())
32                 setContents(key_prefix + tostr(++key_counter));
33 }
34
35
36 InsetBibitem::~InsetBibitem()
37 {
38         InsetCommandMailer mailer("bibitem", *this);
39         mailer.hideDialog();
40 }
41
42
43 Inset * InsetBibitem::clone(Buffer const &, bool) const
44 {
45         InsetBibitem * b = new InsetBibitem(params());
46         b->setCounter(counter);
47         return b;
48 }
49
50
51 void InsetBibitem::setCounter(int c)
52 {
53         counter = c;
54 }
55
56
57 // I'm sorry but this is still necessary because \bibitem is used also
58 // as a LyX 2.x command, and lyxlex is not enough smart to understand
59 // real LaTeX commands. Yes, that could be fixed, but would be a waste
60 // of time cause LyX3 won't use lyxlex anyway.  (ale)
61 void InsetBibitem::write(Buffer const *, std::ostream & os) const
62 {
63         os << "\n\\bibitem ";
64         if (!getOptions().empty())
65                 os << '[' << getOptions() << ']';
66         os << '{' << getContents() << "}\n";
67 }
68
69
70 // This is necessary here because this is written without begin_inset
71 // This should be changed!!! (Jug)
72 void InsetBibitem::read(Buffer const *, LyXLex & lex)
73 {
74         if (lex.eatLine()) {
75                 scanCommand(lex.getString());
76         } else {
77                 lex.printError("InsetCommand: Parse error: `$$Token'");
78         }
79
80         if (prefixIs(getContents(), key_prefix)) {
81                 int key = strToInt(getContents().substr(key_prefix.length()));
82                 key_counter = max(key_counter, key);
83         }
84 }
85
86
87 string const InsetBibitem::getBibLabel() const
88 {
89         return getOptions().empty() ? tostr(counter) : getOptions();
90 }
91
92
93 string const InsetBibitem::getScreenLabel(Buffer const *) const
94 {
95         return getContents() + " [" + getBibLabel() + ']';
96 }
97
98
99 void InsetBibitem::edit(BufferView *, int, int, mouse_button::state)
100 {
101         InsetCommandMailer mailer("bibitem", *this);
102         mailer.showDialog();
103 }
104
105
106 void InsetBibitem::edit(BufferView * bv, bool)
107 {
108         edit(bv, 0, 0, mouse_button::none);
109 }
110
111
112 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
113 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
114 {
115         int w = 0;
116         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
117         // Does look like a hack? It is! (but will change at 0.13)
118         ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
119         ParagraphList::iterator end = bv->buffer()->paragraphs.end();
120         for (; it != end; ++it) {
121                 if (it->bibitem()) {
122                         int const wx = it->bibitem()->width(bv, font);
123                         if (wx > w)
124                                 w = wx;
125                 }
126         }
127         return w;
128 }
129
130
131 // ale070405
132 string const bibitemWidest(Buffer const * buffer)
133 {
134         int w = 0;
135         // Does look like a hack? It is! (but will change at 0.13)
136
137         InsetBibitem * bitem = 0;
138         LyXFont font;
139
140         ParagraphList::iterator it = buffer->paragraphs.begin();
141         ParagraphList::iterator end = buffer->paragraphs.end();
142         for (; it != end; ++it) {
143                 if (it->bibitem()) {
144                         int const wx =
145                                 font_metrics::width(it->bibitem()->getBibLabel(),
146                                                     font);
147                         if (wx > w) {
148                                 w = wx;
149                                 bitem = it->bibitem();
150                         }
151                 }
152         }
153
154         if (bitem && !bitem->getBibLabel().empty())
155                 return bitem->getBibLabel();
156
157         return "99";
158 }