]> git.lyx.org Git - lyx.git/blob - src/insets/InsetIPA.cpp
Remove zombie noncopyable.hpp dependency
[lyx.git] / src / insets / InsetIPA.cpp
1 /**
2  * \file InsetIPA.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 Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include "config.h"
11
12 #include "InsetIPA.h"
13
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "Cursor.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "LaTeXFeatures.h"
21 #include "Lexer.h"
22 #include "MetricsInfo.h"
23 #include "OutputParams.h"
24 #include "RenderPreview.h"
25
26 #include "frontends/Painter.h"
27
28 #include "graphics/PreviewImage.h"
29
30 #include <sstream>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetIPA::InsetIPA(Buffer * buf) 
38         : InsetText(buf),
39           preview_(new RenderPreview(this))
40 {
41         setDrawFrame(true);
42         setFrameColor(Color_insetframe);
43 }
44
45
46 InsetIPA::~InsetIPA() 
47 {}
48
49
50 InsetIPA::InsetIPA(InsetIPA const & other)
51         : InsetText(other)
52 {
53         preview_.reset(new RenderPreview(*other.preview_, this));
54 }
55
56
57 InsetIPA & InsetIPA::operator=(InsetIPA const & other)
58 {
59         if (&other == this)
60                 return *this;
61
62         InsetText::operator=(other);
63         preview_.reset(new RenderPreview(*other.preview_, this));
64
65         return *this;
66 }
67
68
69 void InsetIPA::write(ostream & os) const
70 {
71         os << "IPA" << "\n";
72         text().write(os);
73 }
74
75
76 void InsetIPA::doDispatch(Cursor & cur, FuncRequest & cmd)
77 {
78         switch (cmd.action()) {
79         case LFUN_QUOTE_INSERT: {
80                 FuncRequest fr(LFUN_SELF_INSERT, "\"");
81                 InsetText::doDispatch(cur, fr);
82                 break;
83         }
84         default:
85                 InsetText::doDispatch(cur, cmd);
86                 break;
87         }
88
89 }
90
91
92 bool InsetIPA::getStatus(Cursor & cur, FuncRequest const & cmd,
93                 FuncStatus & flag) const
94 {
95         switch (cmd.action()) {
96         case LFUN_SCRIPT_INSERT: {
97                 if (cmd.argument() == "subscript") {
98                         flag.setEnabled(false);
99                         return true;
100                 }
101                 break;
102         }
103         case LFUN_IN_IPA:
104                 flag.setEnabled(true);
105                 return true;
106                 break;
107         default:
108                 break;
109         }
110         return InsetText::getStatus(cur, cmd, flag);
111 }
112
113
114 void InsetIPA::addPreview(DocIterator const & inset_pos,
115         graphics::PreviewLoader &) const
116 {
117         preparePreview(inset_pos);
118 }
119
120
121 void InsetIPA::preparePreview(DocIterator const & pos) const  
122 {
123         TexRow texrow;
124         odocstringstream str;  
125         otexstream os(str, texrow);
126         OutputParams runparams(&pos.buffer()->params().encoding());
127         latex(os, runparams);
128         docstring const snippet = str.str();
129         preview_->addPreview(snippet, *pos.buffer());  
130 }
131
132
133 bool InsetIPA::previewState(BufferView * bv) const
134 {
135         if (!editing(bv) && RenderPreview::previewText()) {
136                 graphics::PreviewImage const * pimage =
137                         preview_->getPreviewImage(bv->buffer());
138                 return pimage && pimage->image();
139         }
140         return false;
141 }
142
143
144 void InsetIPA::reloadPreview(DocIterator const & pos) const
145 {
146         preparePreview(pos);
147         preview_->startLoading(*pos.buffer());
148 }
149
150
151 void InsetIPA::draw(PainterInfo & pi, int x, int y) const
152 {
153         if (previewState(pi.base.bv)) {
154                 preview_->draw(pi, x + TEXT_TO_INSET_OFFSET, y);
155                 setPosCache(pi, x, y);
156                 return;
157         }
158         InsetText::draw(pi, x, y);
159 }
160
161
162 void InsetIPA::edit(Cursor & cur, bool front, EntryDirection entry_from)
163 {
164         cur.push(*this);
165         InsetText::edit(cur, front, entry_from);
166 }
167
168
169 Inset * InsetIPA::editXY(Cursor & cur, int x, int y)
170 {
171         if (previewState(&cur.bv())) {
172                 edit(cur, true, ENTRY_DIRECTION_IGNORE);
173                 return this;
174         }
175         cur.push(*this);
176         return InsetText::editXY(cur, x, y);
177 }
178
179
180 void InsetIPA::metrics(MetricsInfo & mi, Dimension & dim) const
181 {
182         if (previewState(mi.base.bv)) {
183                 preview_->metrics(mi, dim);
184                 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
185                 
186                 dim.wid = max(dim.wid, 4);
187                 dim.asc = max(dim.asc, 4);
188                 
189                 dim.asc += TEXT_TO_INSET_OFFSET;
190                 dim.des += TEXT_TO_INSET_OFFSET;
191                 dim.wid += TEXT_TO_INSET_OFFSET;
192                 dim.wid += TEXT_TO_INSET_OFFSET;
193                 // insert a one pixel gap
194                 dim.wid += 1;
195                 // Cache the inset dimension.
196                 setDimCache(mi, dim);
197                 Dimension dim_dummy;
198                 MetricsInfo mi_dummy = mi;
199                 InsetText::metrics(mi_dummy, dim_dummy);
200                 return;
201         }
202         InsetText::metrics(mi, dim);
203 }
204
205
206 bool InsetIPA::notifyCursorLeaves(Cursor const & old, Cursor & cur)
207 {
208         reloadPreview(old);
209         cur.screenUpdateFlags(Update::Force);
210         return InsetText::notifyCursorLeaves(old, cur);
211 }
212
213
214 void InsetIPA::validate(LaTeXFeatures & features) const
215 {
216         features.require("tipa");
217         features.require("tipx");
218
219         InsetText::validate(features);
220 }
221
222
223 void InsetIPA::latex(otexstream & os, OutputParams const & runparams_in) const
224 {
225         OutputParams runparams(runparams_in);
226         runparams.inIPA = true;
227         bool const multipar = (text().paragraphs().size() > 1);
228         // fontspec knows \textipa, but not the IPA environment
229         bool const nontexfonts = buffer_->params().useNonTeXFonts;
230         if (multipar && !nontexfonts)
231                 os << "\\begin{IPA}\n";
232         else
233                 os << "\\textipa{";
234         InsetText::latex(os, runparams);
235         if (multipar && !nontexfonts)
236                 os << "\n\\end{IPA}";
237         else
238                 os << "}";
239 }
240
241
242 docstring InsetIPA::xhtml(XHTMLStream & xs, OutputParams const & runparams_in) const
243 {
244         OutputParams runparams(runparams_in);
245         runparams.inIPA = true;
246         return InsetText::xhtml(xs, runparams);
247 }
248
249
250 bool InsetIPA::insetAllowed(InsetCode code) const
251 {
252         switch (code) {
253         // code that is allowed
254         case ERT_CODE:
255         case IPACHAR_CODE:
256         case IPADECO_CODE:
257         case SCRIPT_CODE:
258                 return true;
259         default:
260                 return false;
261         }
262 }
263
264
265 } // namespace lyx