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