]> git.lyx.org Git - features.git/blob - src/insets/InsetText.cpp
Transfer Text::drawSelection() from InsetText::drawSelection() to InsetText::draw...
[features.git] / src / insets / InsetText.cpp
1 /**
2  * \file InsetText.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetText.h"
14 #include "InsetNewline.h"
15
16 #include "Buffer.h"
17 #include "buffer_funcs.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CoordCache.h"
21 #include "CutAndPaste.h"
22 #include "Cursor.h"
23 #include "debug.h"
24 #include "DispatchResult.h"
25 #include "ErrorList.h"
26 #include "FuncRequest.h"
27 #include "gettext.h"
28 #include "Intl.h"
29 #include "Color.h"
30 #include "lyxfind.h"
31 #include "Lexer.h"
32 #include "LyXRC.h"
33 #include "Text.h"
34 #include "MetricsInfo.h"
35 #include "OutputParams.h"
36 #include "output_docbook.h"
37 #include "output_latex.h"
38 #include "output_plaintext.h"
39 #include "Paragraph.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
42 #include "ParIterator.h"
43 #include "Row.h"
44 #include "sgml.h"
45 #include "TexRow.h"
46 #include "Undo.h"
47
48 #include "frontends/alert.h"
49 #include "frontends/Painter.h"
50
51 #include "support/lyxalgo.h" // count
52
53 #include <boost/bind.hpp>
54 #include <boost/current_function.hpp>
55 #include <boost/signal.hpp>
56
57 #include <sstream>
58
59
60 namespace lyx {
61
62 using graphics::PreviewLoader;
63
64 using support::isStrUnsignedInt;
65
66 using boost::bind;
67 using boost::ref;
68
69 using std::endl;
70 using std::for_each;
71 using std::max;
72 using std::string;
73 using std::ostream;
74 using std::vector;
75
76
77 int InsetText::border_ = 2;
78
79
80 InsetText::InsetText(BufferParams const & bp)
81         : drawFrame_(false), frame_color_(Color::insetframe)
82 {
83         paragraphs().push_back(Paragraph());
84         paragraphs().back().layout(bp.getTextClass().defaultLayout());
85         // Dispose of the infamous L-shaped cursor.
86         text_.current_font.setLanguage(bp.language);
87         text_.real_current_font.setLanguage(bp.language);
88         init();
89 }
90
91
92 InsetText::InsetText(InsetText const & in)
93         : Inset(in), text_()
94 {
95         text_.autoBreakRows_ = in.text_.autoBreakRows_;
96         drawFrame_ = in.drawFrame_;
97         frame_color_ = in.frame_color_;
98         text_.paragraphs() = in.text_.paragraphs();
99         // Hand current buffer language down to "cloned" textinsets
100         // e.g. tabular cells
101         text_.current_font = in.text_.current_font;
102         text_.real_current_font = in.text_.real_current_font;
103         init();
104 }
105
106
107 InsetText::InsetText()
108 {}
109
110
111 void InsetText::init()
112 {
113         for_each(paragraphs().begin(), paragraphs().end(),
114                  bind(&Paragraph::setInsetOwner, _1, this));
115 }
116
117
118 void InsetText::clear()
119 {
120         ParagraphList & pars = paragraphs();
121
122         // This is a gross hack...
123         LayoutPtr old_layout = pars.begin()->layout();
124
125         pars.clear();
126         pars.push_back(Paragraph());
127         pars.begin()->setInsetOwner(this);
128         pars.begin()->layout(old_layout);
129 }
130
131
132 Inset * InsetText::clone() const
133 {
134         return new InsetText(*this);
135 }
136
137
138 void InsetText::write(Buffer const & buf, ostream & os) const
139 {
140         os << "Text\n";
141         text_.write(buf, os);
142 }
143
144
145 void InsetText::read(Buffer const & buf, Lexer & lex)
146 {
147         clear();
148
149         // delete the initial paragraph
150         Paragraph oldpar = *paragraphs().begin();
151         paragraphs().clear();
152         ErrorList errorList;
153         bool res = text_.read(buf, lex, errorList);
154         init();
155
156         if (!res) {
157                 lex.printError("Missing \\end_inset at this point. "
158                                            "Read: `$$Token'");
159         }
160
161         // sanity check
162         // ensure we have at least one paragraph.
163         if (paragraphs().empty())
164                 paragraphs().push_back(oldpar);
165 }
166
167
168 bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
169 {
170         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
171
172         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
173         mi.base.textwidth -= 2 * border_;
174         font_ = mi.base.font;
175         // Hand font through to contained lyxtext:
176         text_.font_ = mi.base.font;
177         tm.metrics(mi, dim);
178         dim.asc += border_;
179         dim.des += border_;
180         dim.wid += 2 * border_;
181         mi.base.textwidth += 2 * border_;
182         bool const changed = dim_ != dim;
183         dim_ = dim;
184         return changed;
185 }
186
187
188 void InsetText::draw(PainterInfo & pi, int x, int y) const
189 {
190         // update our idea of where we are
191         setPosCache(pi, x, y);
192
193         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
194
195         if (drawFrame_ || pi.full_repaint) {
196                 int const w = hasFixedWidth() ? 
197                         tm.maxWidth() : tm.width() + 2 * border_;
198                 int const a = tm.ascent() + border_;
199                 int const h = a + tm.descent() + border_;
200                 if (pi.full_repaint)
201                         pi.pain.fillRectangle(x, y - a, w, h, backgroundColor());
202                 if (drawFrame_)
203                         pi.pain.rectangle(x, y - a, w, h, frameColor());
204         }
205         text_.drawSelection(pi, x + border_, y);
206         tm.draw(pi, x + border_, y);
207 }
208
209
210 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
211 {
212 }
213
214
215 docstring const InsetText::editMessage() const
216 {
217         return _("Opened Text Inset");
218 }
219
220
221 void InsetText::edit(Cursor & cur, bool left)
222 {
223         //lyxerr << "InsetText: edit left/right" << endl;
224         int const pit = left ? 0 : paragraphs().size() - 1;
225         int const pos = left ? 0 : paragraphs().back().size();
226         text_.setCursor(cur.top(), pit, pos);
227         cur.clearSelection();
228         finishUndo();
229 }
230
231
232 Inset * InsetText::editXY(Cursor & cur, int x, int y)
233 {
234         return text_.editXY(cur, x, y);
235 }
236
237
238 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
239 {
240         LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
241                              << " [ cmd.action = "
242                              << cmd.action << ']' << endl;
243         text_.dispatch(cur, cmd);
244 }
245
246
247 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
248         FuncStatus & status) const
249 {
250         return text_.getStatus(cur, cmd, status);
251 }
252
253
254 void InsetText::setChange(Change const & change)
255 {
256         ParagraphList::iterator pit = paragraphs().begin();
257         ParagraphList::iterator end = paragraphs().end();
258         for (; pit != end; ++pit) {
259                 pit->setChange(change);
260         }
261 }
262
263
264 void InsetText::acceptChanges(BufferParams const & bparams)
265 {
266         text_.acceptChanges(bparams);
267 }
268
269
270 void InsetText::rejectChanges(BufferParams const & bparams)
271 {
272         text_.rejectChanges(bparams);
273 }
274
275
276 int InsetText::latex(Buffer const & buf, odocstream & os,
277                      OutputParams const & runparams) const
278 {
279         TexRow texrow;
280         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
281         return texrow.rows();
282 }
283
284
285 int InsetText::plaintext(Buffer const & buf, odocstream & os,
286                          OutputParams const & runparams) const
287 {
288         ParagraphList::const_iterator beg = paragraphs().begin();
289         ParagraphList::const_iterator end = paragraphs().end();
290         ParagraphList::const_iterator it = beg;
291         bool ref_printed = false;
292         int len = 0;
293         for (; it != end; ++it) {
294                 if (it != beg) {
295                         os << '\n';
296                         if (runparams.linelen > 0)
297                                 os << '\n';
298                 }
299                 odocstringstream oss;
300                 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
301                 docstring const str = oss.str();
302                 os << str;
303                 // FIXME: len is not computed fully correctly; in principle,
304                 // we have to count the characters after the last '\n'
305                 len = str.size();
306         }
307
308         return len;
309 }
310
311
312 int InsetText::docbook(Buffer const & buf, odocstream & os,
313                        OutputParams const & runparams) const
314 {
315         docbookParagraphs(paragraphs(), buf, os, runparams);
316         return 0;
317 }
318
319
320 void InsetText::validate(LaTeXFeatures & features) const
321 {
322         for_each(paragraphs().begin(), paragraphs().end(),
323                  bind(&Paragraph::validate, _1, ref(features)));
324 }
325
326
327 void InsetText::cursorPos(BufferView const & bv,
328                 CursorSlice const & sl, bool boundary, int & x, int & y) const
329 {
330         x = text_.cursorX(bv, sl, boundary) + border_;
331         y = text_.cursorY(bv, sl, boundary);
332 }
333
334
335 bool InsetText::showInsetDialog(BufferView *) const
336 {
337         return false;
338 }
339
340
341 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
342 {
343         clear();
344         Paragraph & first = paragraphs().front();
345         for (unsigned int i = 0; i < data.length(); ++i)
346                 first.insertChar(i, data[i], font, trackChanges);
347 }
348
349
350 void InsetText::setAutoBreakRows(bool flag)
351 {
352         if (flag == text_.autoBreakRows_)
353                 return;
354
355         text_.autoBreakRows_ = flag;
356         if (flag)
357                 return;
358
359         // remove previously existing newlines
360         ParagraphList::iterator it = paragraphs().begin();
361         ParagraphList::iterator end = paragraphs().end();
362         for (; it != end; ++it)
363                 for (int i = 0; i < it->size(); ++i)
364                         if (it->isNewline(i))
365                                 // do not track the change, because the user
366                                 // is not allowed to revert/reject it
367                                 it->eraseChar(i, false);
368 }
369
370
371 void InsetText::setDrawFrame(bool flag)
372 {
373         drawFrame_ = flag;
374 }
375
376
377 Color_color InsetText::frameColor() const
378 {
379         return Color::color(frame_color_);
380 }
381
382
383 void InsetText::setFrameColor(Color_color col)
384 {
385         frame_color_ = col;
386 }
387
388
389 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
390 {
391         // There is little we can do here to keep track of changes.
392         // As of 2006/10/20, appendParagraphs is used exclusively by
393         // LyXTabular::setMultiColumn. In this context, the paragraph break
394         // is lost irreversibly and the appended text doesn't really change
395
396         ParagraphList & pl = paragraphs();
397
398         ParagraphList::iterator pit = plist.begin();
399         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
400         ++pit;
401         mergeParagraph(buffer->params(), pl,
402                        std::distance(pl.begin(), ins) - 1);
403
404         for_each(pit, plist.end(),
405                  bind(&ParagraphList::push_back, ref(pl), _1));
406 }
407
408
409 void InsetText::addPreview(PreviewLoader & loader) const
410 {
411         ParagraphList::const_iterator pit = paragraphs().begin();
412         ParagraphList::const_iterator pend = paragraphs().end();
413
414         for (; pit != pend; ++pit) {
415                 InsetList::const_iterator it  = pit->insetlist.begin();
416                 InsetList::const_iterator end = pit->insetlist.end();
417                 for (; it != end; ++it)
418                         it->inset->addPreview(loader);
419         }
420 }
421
422
423 //FIXME: instead of this hack, which only works by chance,
424 // cells should have their own insetcell type, which returns CELL_CODE!
425 bool InsetText::neverIndent(Buffer const & buffer) const
426 {
427         // this is only true for tabular cells
428         return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
429 }
430
431
432 ParagraphList const & InsetText::paragraphs() const
433 {
434         return text_.paragraphs();
435 }
436
437
438 ParagraphList & InsetText::paragraphs()
439 {
440         return text_.paragraphs();
441 }
442
443
444 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
445 {
446         ParIterator it2 = it;
447         it2.forwardPos();
448         BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
449         lyx::updateLabels(buf, it2);
450 }
451
452
453 } // namespace lyx