]> git.lyx.org Git - lyx.git/blob - src/insets/InsetListings.cpp
Make listings dialog translatable (mostly strings from InsetListingsParams), fix...
[lyx.git] / src / insets / InsetListings.cpp
1 /**
2  * \file InsetListings.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetListings.h"
15 #include "InsetCaption.h"
16
17 #include "Language.h"
18 #include "gettext.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "MetricsInfo.h"
23 #include "Cursor.h"
24 #include "support/lstrings.h"
25
26 #include <sstream>
27
28 namespace lyx {
29
30 using support::token;
31
32 using std::auto_ptr;
33 using std::istringstream;
34 using std::ostream;
35 using std::ostringstream;
36 using std::string;
37
38
39 void InsetListings::init()
40 {
41         setButtonLabel();
42         Font font(Font::ALL_SANE);
43         font.decSize();
44         font.decSize();
45         font.setColor(Color::none);
46         setLabelFont(font);
47         text_.current_font.setLanguage(latex_language);
48         text_.real_current_font.setLanguage(latex_language);
49 }
50
51
52 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
53         : InsetERT(bp, par.status())
54 {
55         init();
56 }
57
58
59 InsetListings::InsetListings(InsetListings const & in)
60         : InsetERT(in), params_(in.params_)
61 {
62         init();
63 }
64
65
66 auto_ptr<Inset> InsetListings::doClone() const
67 {
68         return auto_ptr<Inset>(new InsetListings(*this));
69 }
70
71
72 InsetListings::~InsetListings()
73 {
74         InsetListingsMailer(*this).hideDialog();
75 }
76
77
78 Inset::DisplayType InsetListings::display() const
79 {
80         return params().isInline() || params().isFloat() ? Inline : AlignLeft;
81 }
82
83
84 void InsetListings::write(Buffer const & buf, ostream & os) const
85 {
86         os << "listings" << "\n";
87         InsetListingsParams const & par = params();
88         // parameter string is encoded to be a valid lyx token.
89         string opt = par.encodedString();
90         if (!opt.empty())
91                 os << "lstparams \"" << opt << "\"\n";
92         if (par.isInline())
93                 os << "inline true\n";
94         else
95                 os << "inline false\n";
96         InsetCollapsable::write(buf, os);
97 }
98
99
100 void InsetListings::read(Buffer const & buf, Lexer & lex)
101 {
102         while (lex.isOK()) {
103                 lex.next();
104                 string const token = lex.getString();
105                 if (token == "lstparams") {
106                         lex.next();
107                         string const value = lex.getString();
108                         params().fromEncodedString(value);
109                 } else if (token == "inline") {
110                         lex.next();
111                         params().setInline(lex.getBool());
112                 } else {
113                         // no special option, push back 'status' etc
114                         lex.pushToken(token);
115                         break;
116                 }
117         }
118         InsetERT::read(buf, lex);
119 }
120
121
122 docstring const InsetListings::editMessage() const
123 {
124         return _("Opened Listings Inset");
125 }
126
127
128 int InsetListings::latex(Buffer const & buf, odocstream & os,
129                     OutputParams const & runparams) const
130 {
131         string param_string = params().encodedString();
132         // NOTE: I use {} to quote text, which is an experimental feature
133         // of the listings package (see page 25 of the manual)
134         int lines = 0;
135         bool lstinline = params().isInline();
136         if (lstinline) {
137                 if (param_string.empty())
138                         os << "\\lstinline{";
139                 else
140                         os << "\\lstinline[" << from_ascii(param_string) << "]{";
141         } else {
142                 docstring const caption = getCaption(buf, runparams);
143                 if (param_string.empty() && caption.empty())
144                         os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
145                 else {
146                         os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
147                         if (!caption.empty()) {
148                                 os << "caption={" << caption << '}';
149                                 if (!param_string.empty())
150                                         os << ',';
151                         }
152                         os << from_utf8(param_string) << "]\n";
153                 }
154                 lines += 4;
155         }
156         ParagraphList::const_iterator par = paragraphs().begin();
157         ParagraphList::const_iterator end = paragraphs().end();
158
159         while (par != end) {
160                 pos_type siz = par->size();
161                 bool captionline = false;
162                 for (pos_type i = 0; i < siz; ++i) {
163                         if (i == 0 && par->isInset(i) && i + 1 == siz)
164                                 captionline = true;
165                         // ignore all struck out text and (caption) insets
166                         if (par->isDeleted(i) || par->isInset(i))
167                                 continue;
168                         os.put(par->getChar(i));
169                 }
170                 ++par;
171                 // for the inline case, if there are multiple paragraphs
172                 // they are simply joined. Otherwise, expect latex errors.
173                 if (par != end && !lstinline && !captionline) {
174                         os << "\n";
175                         ++lines;
176                 }
177         }
178         if (lstinline)
179                 os << "}";              
180         else {
181                 os << "\n\\end{lstlisting}\n\\endgroup\n";
182                 lines += 3;
183         }
184
185         return lines;
186 }
187
188
189 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
190 {
191         switch (cmd.action) {
192
193         case LFUN_INSET_MODIFY: {
194                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
195                 break;
196         }
197         case LFUN_INSET_DIALOG_UPDATE:
198                 InsetListingsMailer(*this).updateDialog(&cur.bv());
199                 break;  
200         case LFUN_MOUSE_RELEASE: {
201                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
202                         InsetListingsMailer(*this).showDialog(&cur.bv());
203                         break;
204                 }
205                 InsetERT::doDispatch(cur, cmd);
206                 break;
207         }
208         default:
209                 InsetERT::doDispatch(cur, cmd);
210                 break;
211         }
212 }
213
214
215 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
216         FuncStatus & status) const
217 {
218         switch (cmd.action) {
219                 case LFUN_INSET_DIALOG_UPDATE:
220                         status.enabled(true);
221                         return true;
222                 case LFUN_CAPTION_INSERT:
223                         status.enabled(!params().isInline());
224                         return true;
225                 default:
226                         return InsetERT::getStatus(cur, cmd, status);
227         }
228 }
229
230
231 void InsetListings::setButtonLabel()
232 {
233         // FIXME UNICODE
234         setLabel(isOpen() ?  _("Listings") : getNewLabel(_("Listings")));
235 }
236
237
238 bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
239 {
240         Font tmpfont = mi.base.font;
241         getDrawFont(mi.base.font);
242         mi.base.font.realize(tmpfont);
243         InsetCollapsable::metrics(mi, dim);
244         mi.base.font = tmpfont;
245         bool const changed = dim_ != dim;
246         dim_ = dim;
247         return changed;
248 }
249
250
251 void InsetListings::draw(PainterInfo & pi, int x, int y) const
252 {
253         Font tmpfont = pi.base.font;
254         getDrawFont(pi.base.font);
255         pi.base.font.realize(tmpfont);
256         InsetCollapsable::draw(pi, x, y);
257         pi.base.font = tmpfont;
258 }
259
260
261 void InsetListings::validate(LaTeXFeatures & features) const
262 {
263         features.require("listings");
264         InsetERT::validate(features);
265 }
266
267
268 bool InsetListings::showInsetDialog(BufferView * bv) const
269 {
270         InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
271         return true;
272 }
273
274
275 void InsetListings::getDrawFont(Font & font) const
276 {
277         font = Font(Font::ALL_INHERIT, latex_language);
278         font.setFamily(Font::TYPEWRITER_FAMILY);
279         // FIXME: define Color::listing?
280         font.setColor(Color::foreground);
281 }
282
283
284 docstring InsetListings::getCaption(Buffer const & buf,
285                     OutputParams const & runparams) const
286 {
287         if (paragraphs().empty())
288                 return docstring();
289
290         ParagraphList::const_iterator pit = paragraphs().begin();
291         for (; pit != paragraphs().end(); ++pit) {
292                 InsetList::const_iterator it = pit->insetlist.begin();
293                 for (; it != pit->insetlist.end(); ++it) {
294                         Inset & inset = *it->inset;
295                         if (inset.lyxCode() == Inset::CAPTION_CODE) {
296                                 odocstringstream ods;
297                                 InsetCaption * ins =
298                                         static_cast<InsetCaption *>(it->inset);
299                                 ins->getOptArg(buf, ods, runparams);
300                                 ins->getArgument(buf, ods, runparams);
301                                 return ods.str();
302                         }
303                 }
304         }
305         return docstring();
306 }
307
308
309 string const InsetListingsMailer::name_("listings");
310
311 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
312         : inset_(inset)
313 {}
314
315
316 string const InsetListingsMailer::inset2string(Buffer const &) const
317 {
318         return params2string(inset_.params());
319 }
320
321
322 void InsetListingsMailer::string2params(string const & in,
323                                    InsetListingsParams & params)
324 {
325         params = InsetListingsParams();
326         if (in.empty())
327                 return;
328         istringstream data(in);
329         Lexer lex(0, 0);
330         lex.setStream(data);
331         // discard "listings", which is only used to determine inset
332         lex.next();
333         params.read(lex);
334 }
335
336
337 string const
338 InsetListingsMailer::params2string(InsetListingsParams const & params)
339 {
340         ostringstream data;
341         data << name_ << " ";
342         params.write(data);
343         return data.str();
344 }
345
346
347 } // namespace lyx