]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
* InsetText::Wide() -> wide()
[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 void 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         dim_ = dim;
180 }
181
182
183 void InsetText::draw(PainterInfo & pi, int x, int y) const
184 {
185         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
186         // update our idea of where we are
187         setPosCache(pi, x, y);
188
189         text_.background_color_ = backgroundColor();
190         text_.draw(pi, x + border_, y);
191
192         if (drawFrame_) {
193                 int const w = text_.width() + 2 * border_;
194                 int const a = text_.ascent() + border_;
195                 int const h = a + text_.descent() + border_;
196                 pi.pain.rectangle(x, y - a, (wide() ? text_.maxwidth_ : w), h,
197                         frameColor());
198         }
199 }
200
201
202 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
203 {
204         int const w = text_.width() + 2 * border_;
205         int const a = text_.ascent() + border_;
206         int const h = a + text_.descent() + border_;
207         pi.pain.fillRectangle(x, y - a, (wide() ? text_.maxwidth_ : w), h,
208                 backgroundColor());
209         text_.drawSelection(pi, x, y);
210 }
211
212
213 bool InsetText::covers(BufferView const & bv, int x, int y) const
214 {
215         return bv.coordCache().getInsets().has(this)
216                         && x >= xo(bv)
217                         && x <= xo(bv) + width() + (wide() ? text_.maxwidth_ : 0)
218                         && y >= yo(bv) - ascent()
219                         && y <= yo(bv) + descent();
220 }
221
222
223 docstring const InsetText::editMessage() const
224 {
225         return _("Opened Text Inset");
226 }
227
228
229 void InsetText::edit(LCursor & cur, bool left)
230 {
231         //lyxerr << "InsetText: edit left/right" << endl;
232         int const pit = left ? 0 : paragraphs().size() - 1;
233         int const pos = left ? 0 : paragraphs().back().size();
234         text_.setCursor(cur.top(), pit, pos);
235         cur.clearSelection();
236         finishUndo();
237 }
238
239
240 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
241 {
242         return text_.editXY(cur, x, y);
243 }
244
245
246 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
247 {
248         lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
249                              << " [ cmd.action = "
250                              << cmd.action << ']' << endl;
251         text_.dispatch(cur, cmd);
252 }
253
254
255 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
256         FuncStatus & status) const
257 {
258         return text_.getStatus(cur, cmd, status);
259 }
260
261
262 void InsetText::setChange(Change const & change)
263 {
264         ParagraphList::iterator pit = paragraphs().begin();
265         ParagraphList::iterator end = paragraphs().end();
266         for (; pit != end; ++pit) {
267                 pit->setChange(change);
268         }
269 }
270
271
272 void InsetText::acceptChanges()
273 {
274         ParagraphList::iterator pit = paragraphs().begin();
275         ParagraphList::iterator end = paragraphs().end();
276         for (; pit != end; ++pit) {
277                 // FIXME: change tracking (MG)
278                 // we must handle end-of-par chars!
279                 pit->acceptChanges(0, pit->size() + 1);
280         }
281 }
282
283
284 void InsetText::rejectChanges()
285 {
286         ParagraphList::iterator pit = paragraphs().begin();
287         ParagraphList::iterator end = paragraphs().end();
288         for (; pit != end; ++pit) {
289                 // FIXME: change tracking (MG)
290                 // we must handle end-of-par chars!
291                 pit->rejectChanges(0, pit->size() + 1);
292         }
293 }
294
295
296 int InsetText::latex(Buffer const & buf, odocstream & os,
297                      OutputParams const & runparams) const
298 {
299         TexRow texrow;
300         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
301         return texrow.rows();
302 }
303
304
305 int InsetText::plaintext(Buffer const & buf, odocstream & os,
306                      OutputParams const & runparams) const
307 {
308         ParagraphList::const_iterator beg = paragraphs().begin();
309         ParagraphList::const_iterator end = paragraphs().end();
310         ParagraphList::const_iterator it = beg;
311         bool ref_printed = false;
312         odocstringstream oss;
313         for (; it != end; ++it)
314                 asciiParagraph(buf, *it, oss, runparams, ref_printed);
315
316         docstring const str = oss.str();
317         os << str;
318         // Return how many newlines we issued.
319         return int(lyx::count(str.begin(), str.end(), '\n'));
320 }
321
322
323 int InsetText::docbook(Buffer const & buf, odocstream & os,
324                        OutputParams const & runparams) const
325 {
326         docbookParagraphs(paragraphs(), buf, os, runparams);
327         return 0;
328 }
329
330
331 void InsetText::validate(LaTeXFeatures & features) const
332 {
333         for_each(paragraphs().begin(), paragraphs().end(),
334                  bind(&Paragraph::validate, _1, ref(features)));
335 }
336
337
338 void InsetText::cursorPos(BufferView const & bv,
339                 CursorSlice const & sl, bool boundary, int & x, int & y) const
340 {
341         x = text_.cursorX(*bv.buffer(), sl, boundary) + border_;
342         y = text_.cursorY(sl, boundary);
343 }
344
345
346 bool InsetText::showInsetDialog(BufferView *) const
347 {
348         return false;
349 }
350
351
352 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
353 {
354         clear();
355         Paragraph & first = paragraphs().front();
356         for (unsigned int i = 0; i < data.length(); ++i)
357                 first.insertChar(i, data[i], font, trackChanges);
358 }
359
360
361 void InsetText::setAutoBreakRows(bool flag)
362 {
363         if (flag == text_.autoBreakRows_)
364                 return;
365
366         text_.autoBreakRows_ = flag;
367         if (flag)
368                 return;
369
370         // remove previously existing newlines
371         ParagraphList::iterator it = paragraphs().begin();
372         ParagraphList::iterator end = paragraphs().end();
373         for (; it != end; ++it)
374                 for (int i = 0; i < it->size(); ++i)
375                         if (it->isNewline(i))
376                                 // do not track the change, because the user
377                                 // is not allowed to revert/reject it
378                                 it->eraseChar(i, false);
379 }
380
381
382 void InsetText::setDrawFrame(bool flag)
383 {
384         drawFrame_ = flag;
385 }
386
387
388 LColor_color InsetText::frameColor() const
389 {
390         return LColor::color(frame_color_);
391 }
392
393
394 void InsetText::setFrameColor(LColor_color col)
395 {
396         frame_color_ = col;
397 }
398
399
400 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
401 {
402         // There is little we can do here to keep track of changes.
403         // As of 2006/10/20, appendParagraphs is used exclusively by
404         // LyXTabular::setMultiColumn. In this context, the paragraph break
405         // is lost irreversibly and the appended text doesn't really change
406
407         ParagraphList & pl = paragraphs();
408
409         ParagraphList::iterator pit = plist.begin();
410         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
411         ++pit;
412         mergeParagraph(buffer->params(), pl,
413                        std::distance(pl.begin(), ins) - 1);
414
415         for_each(pit, plist.end(),
416                  bind(&ParagraphList::push_back, ref(pl), _1));
417 }
418
419
420 void InsetText::addPreview(PreviewLoader & loader) const
421 {
422         ParagraphList::const_iterator pit = paragraphs().begin();
423         ParagraphList::const_iterator pend = paragraphs().end();
424
425         for (; pit != pend; ++pit) {
426                 InsetList::const_iterator it  = pit->insetlist.begin();
427                 InsetList::const_iterator end = pit->insetlist.end();
428                 for (; it != end; ++it)
429                         it->inset->addPreview(loader);
430         }
431 }
432
433
434 //FIXME: instead of this hack, which only works by chance,
435 // cells should have their own insetcell type, which returns CELL_CODE!
436 bool InsetText::neverIndent(Buffer const & buffer) const
437 {
438         // this is only true for tabular cells
439         return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
440 }
441
442
443 ParagraphList const & InsetText::paragraphs() const
444 {
445         return text_.paragraphs();
446 }
447
448
449 ParagraphList & InsetText::paragraphs()
450 {
451         return text_.paragraphs();
452 }
453
454
455 } // namespace lyx