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