]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
Make LyX source look a little less like the human genome
[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
11 #include <config.h>
12
13 #include "insetbibitem.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "lyxfont.h"
20 #include "lyxlex.h"
21 #include "paragraph.h"
22 #include "ParagraphList_fwd.h"
23
24 #include "frontends/font_metrics.h"
25
26 #include "support/lstrings.h"
27 #include "support/std_ostream.h"
28 #include "support/convert.h"
29
30 using lyx::support::prefixIs;
31
32 using std::max;
33 using std::string;
34 using std::auto_ptr;
35 using std::ostream;
36
37 int InsetBibitem::key_counter = 0;
38 string const key_prefix = "key-";
39
40
41 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
42         : InsetCommand(p, "bibitem"), counter(1)
43 {
44         if (getContents().empty())
45                 setContents(key_prefix + convert<string>(++key_counter));
46 }
47
48
49 auto_ptr<InsetBase> InsetBibitem::doClone() const
50 {
51         auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
52         b->setCounter(counter);
53         return auto_ptr<InsetBase>(b);
54 }
55
56
57 void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd)
58 {
59         switch (cmd.action) {
60
61         case LFUN_INSET_MODIFY: {
62                 InsetCommandParams p;
63                 InsetCommandMailer::string2params("bibitem", cmd.argument, p);
64                 if (!p.getCmdName().empty())
65                         setParams(p);
66                 break;
67         }
68
69         default:
70                 InsetCommand::doDispatch(cur, cmd);
71                 break;
72         }
73 }
74
75
76 void InsetBibitem::setCounter(int c)
77 {
78         counter = c;
79 }
80
81
82 // I'm sorry but this is still necessary because \bibitem is used also
83 // as a LyX 2.x command, and lyxlex is not enough smart to understand
84 // real LaTeX commands. Yes, that could be fixed, but would be a waste
85 // of time cause LyX3 won't use lyxlex anyway.  (ale)
86 void InsetBibitem::write(Buffer const &, std::ostream & os) const
87 {
88         os << "\n\\bibitem ";
89         if (!getOptions().empty())
90                 os << '[' << getOptions() << ']';
91         os << '{' << getContents() << "}\n";
92 }
93
94
95 // This is necessary here because this is written without begin_inset
96 // This should be changed!!! (Jug)
97 void InsetBibitem::read(Buffer const &, LyXLex & lex)
98 {
99         if (lex.eatLine())
100                 scanCommand(lex.getString());
101         else
102                 lex.printError("InsetCommand: Parse error: `$$Token'");
103
104         if (prefixIs(getContents(), key_prefix)) {
105                 int const key = convert<int>(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() ? convert<string>(counter) : getOptions();
114 }
115
116
117 string const InsetBibitem::getScreenLabel(Buffer const &) const
118 {
119         return getContents() + " [" + getBibLabel() + ']';
120 }
121
122 int InsetBibitem::plaintext(Buffer const &, ostream & os,
123                             OutputParams const &) const
124 {
125         os << '[' << getCounter() << "] ";
126         return 0;
127 }
128
129
130 // ale070405
131 string const bibitemWidest(Buffer const & buffer)
132 {
133         int w = 0;
134         // Does look like a hack? It is! (but will change at 0.13)
135
136         InsetBibitem const * bitem = 0;
137         LyXFont font;
138
139         ParagraphList::const_iterator it = buffer.paragraphs().begin();
140         ParagraphList::const_iterator end = buffer.paragraphs().end();
141
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 }