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