]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
Point fix, earlier forgotten
[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 "funcrequest.h"
16 #include "lyxlex.h"
17
18 #include "frontends/font_metrics.h"
19
20 #include "support/tostr.h"
21 #include "support/lstrings.h"
22
23 using namespace lyx::support;
24
25 using std::max;
26 using std::auto_ptr;
27
28
29 int InsetBibitem::key_counter = 0;
30 string const key_prefix = "key-";
31
32
33 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
34         : InsetCommand(p), counter(1)
35 {
36         if (getContents().empty())
37                 setContents(key_prefix + tostr(++key_counter));
38 }
39
40
41 InsetBibitem::~InsetBibitem()
42 {
43         InsetCommandMailer("bibitem", *this).hideDialog();
44 }
45
46
47 auto_ptr<InsetBase> InsetBibitem::clone() const
48 {
49         InsetBibitem * b = new InsetBibitem(params());
50         b->setCounter(counter);
51         return auto_ptr<InsetBase>(b);
52 }
53
54
55 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
56 {
57         switch (cmd.action) {
58
59         case LFUN_INSET_EDIT:
60                 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
61                 return DISPATCHED;
62
63         case LFUN_INSET_MODIFY: {
64                 InsetCommandParams p;
65                 InsetCommandMailer::string2params(cmd.argument, p);
66                 if (p.getCmdName().empty())
67                         return DISPATCHED;
68                 setParams(p);
69                 cmd.view()->updateInset(this);
70                 cmd.view()->fitCursor();
71                 return DISPATCHED;
72         }
73
74         default:
75                 return InsetCommand::localDispatch(cmd);
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
109         if (prefixIs(getContents(), key_prefix)) {
110                 int key = strToInt(getContents().substr(key_prefix.length()));
111                 key_counter = max(key_counter, key);
112         }
113 }
114
115
116 string const InsetBibitem::getBibLabel() const
117 {
118         return getOptions().empty() ? tostr(counter) : getOptions();
119 }
120
121
122 string const InsetBibitem::getScreenLabel(Buffer const &) const
123 {
124         return getContents() + " [" + getBibLabel() + ']';
125 }
126
127
128 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
129 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
130 {
131         int w = 0;
132         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
133         // Does look like a hack? It is! (but will change at 0.13)
134         ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
135         ParagraphList::iterator end = bv->buffer()->paragraphs.end();
136         for (; it != end; ++it) {
137                 if (it->bibitem()) {
138 #warning metrics broken!
139                         int const wx = it->bibitem()->width();
140                         if (wx > w)
141                                 w = wx;
142                 }
143         }
144         return w;
145 }
146
147
148 // ale070405
149 string const bibitemWidest(Buffer const & buffer)
150 {
151         int w = 0;
152         // Does look like a hack? It is! (but will change at 0.13)
153
154         InsetBibitem const * bitem = 0;
155         LyXFont font;
156
157         ParagraphList::const_iterator it = buffer.paragraphs.begin();
158         ParagraphList::const_iterator end = buffer.paragraphs.end();
159
160         for (; it != end; ++it) {
161                 if (it->bibitem()) {
162                         int const wx =
163                                 font_metrics::width(it->bibitem()->getBibLabel(),
164                                                     font);
165                         if (wx > w) {
166                                 w = wx;
167                                 bitem = it->bibitem();
168                         }
169                 }
170         }
171
172         if (bitem && !bitem->getBibLabel().empty())
173                 return bitem->getBibLabel();
174
175         return "99";
176 }