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