]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
Make the insets accept LFUN_INSET_SETTINGS. For these insets, LFUN_INSET_SETTINGS...
[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_INSET_MODIFY: {
113                 setStatus(cur, string2params(to_utf8(cmd.argument())));
114                 break;
115         }
116         default:
117                 // Force any new text to latex_language
118                 // FIXME: This should not be necessary but
119                 // new paragraphs that are created by pressing enter at the
120                 // start of an existing paragraph get the buffer language
121                 // and not latex_language, so we take this brute force
122                 // approach.
123                 cur.current_font.fontInfo() = layout.font;
124                 cur.real_current_font.fontInfo() = layout.font;
125                 InsetCollapsable::doDispatch(cur, cmd);
126                 break;
127         }
128 }
129
130
131 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
132         FuncStatus & status) const
133 {
134         switch (cmd.action) {
135                 case LFUN_CLIPBOARD_PASTE:
136                 case LFUN_INSET_MODIFY:
137                 case LFUN_PASTE:
138                 case LFUN_PRIMARY_SELECTION_PASTE:
139                 case LFUN_QUOTE_INSERT:
140                 case LFUN_INSET_SETTINGS:
141                         status.setEnabled(true);
142                         return true;
143
144                 // this one is difficult to get right. As a half-baked
145                 // solution, we consider only the first action of the sequence
146                 case LFUN_COMMAND_SEQUENCE: {
147                         // argument contains ';'-terminated commands
148                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
149                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
150                         func.origin = cmd.origin;
151                         return getStatus(cur, func, status);
152                 }
153
154                 default:
155                         return InsetCollapsable::getStatus(cur, cmd, status);
156         }
157 }
158
159
160 docstring const InsetERT::buttonLabel(BufferView const & bv) const
161 {
162         if (decoration() == InsetLayout::CLASSIC)
163                 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
164         else
165                 return getNewLabel(_("ERT"));
166 }
167
168
169 bool InsetERT::insetAllowed(InsetCode /* code */) const
170 {
171         return false;
172 }
173
174
175 bool InsetERT::showInsetDialog(BufferView * bv) const
176 {
177         bv->showDialog("ert", params2string(status(*bv)), 
178                 const_cast<InsetERT *>(this));
179         return true;
180 }
181
182
183 InsetCollapsable::CollapseStatus InsetERT::string2params(string const & in)
184 {
185         if (in.empty())
186                 return Collapsed;
187         istringstream data(in);
188         Lexer lex;
189         lex.setStream(data);
190         lex.setContext("InsetERT::string2params");
191         lex >> "ert";
192         int s;
193         lex >> s;
194         return static_cast<CollapseStatus>(s);
195 }
196
197
198 string InsetERT::params2string(CollapseStatus status)
199 {
200         ostringstream data;
201         data << "ert" << ' ' << status;
202         return data.str();
203 }
204
205
206 } // namespace lyx