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