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