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