]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
6a45246adea85db7c54807d800e326d5c90d3201
[lyx.git] / src / insets / insettext.C
1 /**
2  * \file insettext.C
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 "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "intl.h"
27 #include "LColor.h"
28 #include "lyxfind.h"
29 #include "lyxlex.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "metricsinfo.h"
33 #include "output_docbook.h"
34 #include "output_latex.h"
35 #include "output_linuxdoc.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
41 #include "lyxrow.h"
42 #include "sgml.h"
43 #include "texrow.h"
44 #include "undo.h"
45
46 #include "frontends/Alert.h"
47 #include "frontends/font_metrics.h"
48 #include "frontends/Painter.h"
49
50 #include "support/lyxalgo.h" // lyx::count
51
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
54
55 using lyx::pos_type;
56
57 using lyx::graphics::PreviewLoader;
58
59 using lyx::support::isStrUnsignedInt;
60
61 using boost::bind;
62 using boost::ref;
63
64 using std::endl;
65 using std::for_each;
66 using std::max;
67 using std::string;
68 using std::auto_ptr;
69 using std::ostream;
70 using std::vector;
71
72
73 int InsetText::border_ = 2;
74
75
76 InsetText::InsetText(BufferParams const & bp)
77         : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
78 {
79         paragraphs().push_back(Paragraph());
80         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
81         if (bp.tracking_changes)
82                 paragraphs().back().trackChanges();
83         // Dispose of the infamous L-shaped cursor.
84         text_.current_font.setLanguage(bp.language);
85         text_.real_current_font.setLanguage(bp.language);
86         init();
87 }
88
89
90 InsetText::InsetText(InsetText const & in)
91         : InsetOld(in), text_(in.text_.bv_owner)
92 {
93         text_.autoBreakRows_ = in.text_.autoBreakRows_;
94         drawFrame_ = in.drawFrame_;
95         frame_color_ = in.frame_color_;
96         text_.paragraphs() = in.text_.paragraphs();
97         // Hand current buffer language down to "cloned" textinsets 
98         // e.g. tabular cells
99         text_.current_font = in.text_.current_font;
100         text_.real_current_font = in.text_.real_current_font;
101         init();
102 }
103
104
105 InsetText::InsetText()
106         : text_(0)
107 {}
108
109
110 void InsetText::init()
111 {
112         for_each(paragraphs().begin(), paragraphs().end(),
113                  bind(&Paragraph::setInsetOwner, _1, this));
114 }
115
116
117 void InsetText::markErased(bool erased)
118 {
119         ParagraphList & pars = paragraphs();
120         for_each(pars.begin(), pars.end(),
121                  bind(&Paragraph::markErased, _1, erased));
122 }
123
124
125 void InsetText::clear()
126 {
127         ParagraphList & pars = paragraphs();
128
129         // This is a gross hack...
130         LyXLayout_ptr old_layout = pars.begin()->layout();
131
132         pars.clear();
133         pars.push_back(Paragraph());
134         pars.begin()->setInsetOwner(this);
135         pars.begin()->layout(old_layout);
136 }
137
138
139 auto_ptr<InsetBase> InsetText::doClone() const
140 {
141         return auto_ptr<InsetBase>(new InsetText(*this));
142 }
143
144
145 void InsetText::write(Buffer const & buf, ostream & os) const
146 {
147         os << "Text\n";
148         text_.write(buf, os);
149 }
150
151
152 void InsetText::read(Buffer const & buf, LyXLex & lex)
153 {
154         clear();
155
156 #ifdef WITH_WARNINGS
157 #warning John, look here. Doesnt make much sense.
158 #endif
159         if (buf.params().tracking_changes)
160                 paragraphs().begin()->trackChanges();
161
162         // delete the initial paragraph
163         Paragraph oldpar = *paragraphs().begin();
164         paragraphs().clear();
165         bool res = text_.read(buf, lex);
166         init();
167
168         if (!res) {
169                 lex.printError("Missing \\end_inset at this point. "
170                                            "Read: `$$Token'");
171         }
172
173         // sanity check
174         // ensure we have at least one paragraph.
175         if (paragraphs().empty())
176                 paragraphs().push_back(oldpar);
177 }
178
179
180 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
181 {
182         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
183         setViewCache(mi.base.bv);
184         mi.base.textwidth -= 2 * border_;
185         font_ = mi.base.font;
186         // Hand font through to contained lyxtext:
187         text_.font_ = mi.base.font;
188         text_.metrics(mi, dim);
189         dim.asc += border_;
190         dim.des += border_;
191         dim.wid += 2 * border_;
192         mi.base.textwidth += 2 * border_;
193         dim_ = dim;
194 }
195
196
197 void InsetText::draw(PainterInfo & pi, int x, int y) const
198 {
199         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
200         // update our idea of where we are
201         setPosCache(pi, x, y);
202
203         text_.background_color_ = backgroundColor();
204         text_.draw(pi, x + border_, y);
205
206         if (drawFrame_) {
207                 int const w = text_.width() + 2 * border_;
208                 int const a = text_.ascent() + border_;
209                 int const h = a + text_.descent() + border_;
210                 int const ww = pi.base.bv->workWidth();
211                 if (w > ww - 40 || Wide())  {
212                         pi.pain.line(0, y - a, ww, y - a, frameColor());
213                         pi.pain.line(0, y - a + h, ww, y - a + h, frameColor());
214                 } else {
215                         pi.pain.rectangle(x, y - a, w, h, frameColor());
216                 }
217         }
218 }
219
220
221 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
222 {
223         int const w = text_.width() + 2 * border_;
224         int const a = text_.ascent() + border_;
225         int const h = a + text_.descent() + border_;
226         int const ww = pi.base.bv->workWidth();
227         if (Wide())
228                 pi.pain.fillRectangle(0, y - a, ww, h, 
229                         backgroundColor());
230         else
231                 pi.pain.fillRectangle(x, y - a, w, h, 
232                         backgroundColor());
233         text_.drawSelection(pi, x, y);
234 }
235
236
237 string const InsetText::editMessage() const
238 {
239         return _("Opened Text Inset");
240 }
241
242
243 void InsetText::edit(LCursor & cur, bool left)
244 {
245         //lyxerr << "InsetText: edit left/right" << endl;
246         setViewCache(&cur.bv());
247         int const pit = left ? 0 : paragraphs().size() - 1;
248         int const pos = left ? 0 : paragraphs().back().size();
249         text_.setCursor(cur.top(), pit, pos);
250         cur.clearSelection();
251         finishUndo();
252 }
253
254
255 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
256 {
257         return text_.editXY(cur, x, y);
258 }
259
260
261 bool const InsetText::Tall() const
262 {
263         return text_.ascent() + text_.descent() > 2  * defaultRowHeight(); 
264 }
265
266
267 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
268 {
269         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
270     << " [ cmd.action = " << cmd.action << ']' << endl;
271         setViewCache(&cur.bv());
272         text_.dispatch(cur, cmd);
273 }
274
275
276 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
277         FuncStatus & status) const
278 {
279         return text_.getStatus(cur, cmd, status);
280 }
281
282
283 int InsetText::latex(Buffer const & buf, ostream & os,
284                      OutputParams const & runparams) const
285 {
286         TexRow texrow;
287         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
288         return texrow.rows();
289 }
290
291
292 int InsetText::plaintext(Buffer const & buf, ostream & os,
293                      OutputParams const & runparams) const
294 {
295         ParagraphList::const_iterator beg = paragraphs().begin();
296         ParagraphList::const_iterator end = paragraphs().end();
297         ParagraphList::const_iterator it = beg;
298         bool ref_printed = false;
299         for (; it != end; ++it)
300                 asciiParagraph(buf, *it, os, runparams, ref_printed);
301
302         // FIXME: Give the total numbers of lines
303         return 1;
304 }
305
306
307 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
308                         OutputParams const & runparams) const
309 {
310         linuxdocParagraphs(buf, paragraphs(), os, runparams);
311         return 0;
312 }
313
314
315 int InsetText::docbook(Buffer const & buf, ostream & os,
316                        OutputParams const & runparams) const
317 {
318         docbookParagraphs(paragraphs(), buf, os, runparams);
319         return 0;
320 }
321
322
323 void InsetText::validate(LaTeXFeatures & features) const
324 {
325         for_each(paragraphs().begin(), paragraphs().end(),
326                  bind(&Paragraph::validate, _1, ref(features)));
327 }
328
329
330 void InsetText::cursorPos
331         (CursorSlice const & sl, bool boundary, int & x, int & y) const
332 {
333         x = text_.cursorX(sl, boundary) + border_;
334         y = text_.cursorY(sl, boundary);
335 }
336
337
338 bool InsetText::showInsetDialog(BufferView *) const
339 {
340         return false;
341 }
342
343
344 void InsetText::markNew(bool track_changes)
345 {
346         ParagraphList::iterator pit = paragraphs().begin();
347         ParagraphList::iterator end = paragraphs().end();
348         for (; pit != end; ++pit) {
349                 if (track_changes)
350                         pit->trackChanges();
351                 else // no-op when not tracking
352                         pit->cleanChanges();
353         }
354 }
355
356
357 void InsetText::setText(string const & data, LyXFont const & font)
358 {
359         clear();
360         Paragraph & first = paragraphs().front();
361         for (unsigned int i = 0; i < data.length(); ++i)
362                 first.insertChar(i, data[i], font);
363 }
364
365
366 void InsetText::setAutoBreakRows(bool flag)
367 {
368         if (flag == text_.autoBreakRows_)
369                 return;
370
371         text_.autoBreakRows_ = flag;
372         if (flag)
373                 return;
374
375         // remove previously existing newlines
376         ParagraphList::iterator it = paragraphs().begin();
377         ParagraphList::iterator end = paragraphs().end();
378         for (; it != end; ++it)
379                 for (int i = 0; i < it->size(); ++i)
380                         if (it->isNewline(i))
381                                 it->erase(i);
382 }
383
384
385 void InsetText::setDrawFrame(bool flag)
386 {
387         drawFrame_ = flag;
388 }
389
390
391 LColor_color InsetText::frameColor() const
392 {
393         return LColor::color(frame_color_);
394 }
395
396
397 void InsetText::setFrameColor(LColor_color col)
398 {
399         frame_color_ = col;
400 }
401
402
403 void InsetText::setViewCache(BufferView const * bv) const
404 {
405         if (bv && bv != text_.bv_owner) {
406                 //lyxerr << "setting view cache from "
407                 //      << text_.bv_owner << " to " << bv << "\n";
408                 text_.bv_owner = const_cast<BufferView *>(bv);
409         }
410 }
411
412
413 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
414 {
415 #ifdef WITH_WARNINGS
416 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
417 // And it probably does. You have to take a look at this John. (Lgb)
418 #warning John, have a look here. (Lgb)
419 #endif
420         ParagraphList & pl = paragraphs();
421
422         ParagraphList::iterator pit = plist.begin();
423         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
424         ++pit;
425         mergeParagraph(buffer->params(), pl,
426                        std::distance(pl.begin(), ins) - 1);
427
428         for_each(pit, plist.end(),
429                  bind(&ParagraphList::push_back, ref(pl), _1));
430 }
431
432
433 void InsetText::addPreview(PreviewLoader & loader) const
434 {
435         ParagraphList::const_iterator pit = paragraphs().begin();
436         ParagraphList::const_iterator pend = paragraphs().end();
437
438         for (; pit != pend; ++pit) {
439                 InsetList::const_iterator it  = pit->insetlist.begin();
440                 InsetList::const_iterator end = pit->insetlist.end();
441                 for (; it != end; ++it)
442                         it->inset->addPreview(loader);
443         }
444 }
445
446
447 //FIXME: instead of this hack, which only works by chance,
448 // cells should have their own insetcell type, which returns CELL_CODE!
449 bool InsetText::neverIndent() const
450 {
451         // this is only true for tabular cells
452         return !text_.isMainText() && lyxCode() == TEXT_CODE;
453 }
454
455
456 ParagraphList const & InsetText::paragraphs() const
457 {
458         return text_.paragraphs();
459 }
460
461
462 ParagraphList & InsetText::paragraphs()
463 {
464         return text_.paragraphs();
465 }