]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
This optional argument to the InsetCollapsable constructor
[lyx.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetERT.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "Lexer.h"
26 #include "LyXAction.h"
27 #include "MetricsInfo.h"
28 #include "ParagraphParameters.h"
29 #include "Paragraph.h"
30 #include "TextClass.h"
31
32 #include "frontends/alert.h"
33 #include "frontends/Application.h"
34
35 #include "support/debug.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38
39 #include <sstream>
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46 InsetERT::InsetERT(Buffer const & buf, CollapseStatus status)
47         : InsetCollapsable(buf)
48 {
49         status_ = status;
50 }
51
52
53 InsetERT::~InsetERT()
54 {
55         hideDialogs("ert", this);
56 }
57
58
59 void InsetERT::write(ostream & os) const
60 {
61         os << "ERT" << "\n";
62         InsetCollapsable::write(os);
63 }
64
65
66 docstring InsetERT::editMessage() const
67 {
68         return _("Opened ERT Inset");
69 }
70
71
72 int InsetERT::latex(odocstream & os, OutputParams const & op) const
73 {
74         return InsetCollapsable::latex(os, op);
75 }
76
77
78 int InsetERT::plaintext(odocstream &, OutputParams const &) const
79 {
80         return 0; // do not output TeX code
81 }
82
83
84 int InsetERT::docbook(odocstream & os, OutputParams const &) const
85 {
86         // FIXME can we do the same thing here as for LaTeX?
87         ParagraphList::const_iterator par = paragraphs().begin();
88         ParagraphList::const_iterator end = paragraphs().end();
89
90         int lines = 0;
91         while (par != end) {
92                 pos_type siz = par->size();
93                 for (pos_type i = 0; i < siz; ++i)
94                         os.put(par->getChar(i));
95                 ++par;
96                 if (par != end) {
97                         os << "\n";
98                         ++lines;
99                 }
100         }
101
102         return lines;
103 }
104
105
106 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
107 {
108         BufferParams const & bp = cur.buffer().params();
109         Layout const layout = bp.documentClass().plainLayout();
110         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
111         switch (cmd.action) {
112         case LFUN_QUOTE_INSERT: {
113                 // We need to bypass the fancy quotes in Text
114                 FuncRequest f(LFUN_SELF_INSERT, "\"");
115                 dispatch(cur, f);
116                 break;
117         }
118         case LFUN_INSET_MODIFY: {
119                 setStatus(cur, string2params(to_utf8(cmd.argument())));
120                 break;
121         }
122         default:
123                 // Force any new text to latex_language
124                 // FIXME: This should not be necessary but
125                 // new paragraphs that are created by pressing enter at the
126                 // start of an existing paragraph get the buffer language
127                 // and not latex_language, so we take this brute force
128                 // approach.
129                 cur.current_font.fontInfo() = layout.font;
130                 cur.real_current_font.fontInfo() = layout.font;
131                 InsetCollapsable::doDispatch(cur, cmd);
132                 break;
133         }
134 }
135
136
137 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
138         FuncStatus & status) const
139 {
140         switch (cmd.action) {
141                 case LFUN_CLIPBOARD_PASTE:
142                 case LFUN_INSET_MODIFY:
143                 case LFUN_PASTE:
144                 case LFUN_PRIMARY_SELECTION_PASTE:
145                 case LFUN_QUOTE_INSERT:
146                         status.setEnabled(true);
147                         return true;
148
149                 // this one is difficult to get right. As a half-baked
150                 // solution, we consider only the first action of the sequence
151                 case LFUN_COMMAND_SEQUENCE: {
152                         // argument contains ';'-terminated commands
153                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
154                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
155                         func.origin = cmd.origin;
156                         return getStatus(cur, func, status);
157                 }
158
159                 default:
160                         return InsetCollapsable::getStatus(cur, cmd, status);
161         }
162 }
163
164
165 void InsetERT::setButtonLabel()
166 {
167         if (decoration() == InsetLayout::CLASSIC)
168                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
169         else
170                 setLabel(getNewLabel(_("ERT")));
171 }
172
173
174 bool InsetERT::insetAllowed(InsetCode /* code */) const
175 {
176         return false;
177 }
178
179
180 bool InsetERT::showInsetDialog(BufferView * bv) const
181 {
182         bv->showDialog("ert", params2string(status()), 
183                 const_cast<InsetERT *>(this));
184         return true;
185 }
186
187
188 InsetCollapsable::CollapseStatus InsetERT::string2params(string const & in)
189 {
190         if (in.empty())
191                 return Collapsed;
192         istringstream data(in);
193         Lexer lex;
194         lex.setStream(data);
195         lex.setContext("InsetERT::string2params");
196         lex >> "ert";
197         int s;
198         lex >> s;
199         return static_cast<CollapseStatus>(s);
200 }
201
202
203 string InsetERT::params2string(CollapseStatus status)
204 {
205         ostringstream data;
206         data << "ert" << ' ' << status;
207         return data.str();
208 }
209
210
211 } // namespace lyx