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