]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Fix bug 421
[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/LyXView.h"
49 #include "frontends/Painter.h"
50
51 #include "support/lyxalgo.h" // lyx::count
52
53 #include <boost/bind.hpp>
54 #include <boost/current_function.hpp>
55
56 using lyx::pos_type;
57
58 using lyx::graphics::PreviewLoader;
59
60 using lyx::support::isStrUnsignedInt;
61
62 using boost::bind;
63 using boost::ref;
64
65 using std::endl;
66 using std::for_each;
67 using std::max;
68 using std::string;
69 using std::auto_ptr;
70 using std::ostream;
71 using std::vector;
72
73
74 int InsetText::border_ = 2;
75
76
77 InsetText::InsetText(BufferParams const & bp)
78         : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
79 {
80         paragraphs().push_back(Paragraph());
81         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
82         if (bp.tracking_changes)
83                 paragraphs().back().trackChanges();
84         init();
85 }
86
87
88 InsetText::InsetText(InsetText const & in)
89         : UpdatableInset(in), text_(in.text_.bv_owner)
90 {
91         text_.autoBreakRows_ = in.text_.autoBreakRows_;
92         drawFrame_ = in.drawFrame_;
93         frame_color_ = in.frame_color_;
94         text_.paragraphs() = in.text_.paragraphs();
95         init();
96 }
97
98
99 InsetText::InsetText()
100         : text_(0)
101 {}
102
103
104 void InsetText::init()
105 {
106         for_each(paragraphs().begin(), paragraphs().end(),
107                  bind(&Paragraph::setInsetOwner, _1, this));
108         old_pit = -1;
109 }
110
111
112 void InsetText::clear(bool just_mark_erased)
113 {
114         ParagraphList & pars = paragraphs();
115         if (just_mark_erased) {
116                 for_each(pars.begin(), pars.end(),
117                          bind(&Paragraph::markErased, _1));
118                 return;
119         }
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(false);
147
148 #ifdef WITH_WARNINGS
149 #warning John, look here. Doesnt make much sense.
150 #endif
151         if (buf.params().tracking_changes)
152                 paragraphs().begin()->trackChanges();
153
154         // delete the initial paragraph
155         Paragraph oldpar = *paragraphs().begin();
156         paragraphs().clear();
157         bool res = text_.read(buf, lex);
158         init();
159
160         if (!res) {
161                 lex.printError("Missing \\end_inset at this point. "
162                                            "Read: `$$Token'");
163         }
164
165         // sanity check
166         // ensure we have at least one paragraph.
167         if (paragraphs().empty())
168                 paragraphs().push_back(oldpar);
169 }
170
171
172 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
173 {
174         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
175         setViewCache(mi.base.bv);
176         mi.base.textwidth -= 2 * border_;
177         font_ = mi.base.font;
178         text_.font_ = mi.base.font;
179         text_.metrics(mi, dim);
180         dim.asc += border_;
181         dim.des += border_;
182         dim.wid += 2 * border_;
183         mi.base.textwidth += 2 * border_;
184         dim_ = dim;
185 }
186
187
188 void InsetText::draw(PainterInfo & pi, int x, int y) const
189 {
190         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
191         // update our idea of where we are
192         setPosCache(pi, x, y);
193
194         BufferView * bv = pi.base.bv;
195         bv->hideCursor();
196
197         x += scroll();
198         text_.draw(pi, x + border_, y);
199
200         if (drawFrame_)
201                 drawFrame(pi.pain, x, y);
202 }
203
204
205 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
206 {
207         // repaint the background if needed
208         if (backgroundColor() != LColor::background)
209                 clearInset(pi.pain, x, y);
210         text_.drawSelection(pi, x, y);
211 }
212
213
214 void InsetText::drawFrame(Painter & pain, int x, int y) const
215 {
216         int const w = text_.width() + 2 * border_;
217         int const a = text_.ascent() + border_;
218         int const h = a + text_.descent() + border_;
219         pain.rectangle(x, y - a, w, h, frameColor());
220 }
221
222
223 void InsetText::clearInset(Painter & pain, int x, int y) const
224 {
225         int const w = text_.width() + 2 * border_;
226         int const a = text_.ascent() + border_;
227         int const h = a + text_.descent() + border_;
228         pain.fillRectangle(x, y - a, w, h, backgroundColor());
229 }
230
231
232 void InsetText::updateLocal(LCursor & cur)
233 {
234         if (!text_.autoBreakRows_ && paragraphs().size() > 1) {
235                 // collapse paragraphs
236                 while (paragraphs().size() > 1) {
237                         ParagraphList::iterator const first = paragraphs().begin();
238                         ParagraphList::iterator second = first;
239                         ++second;
240                         size_t const first_par_size = first->size();
241
242                         if (!first->empty() &&
243                                         !second->empty() &&
244                                         !first->isSeparator(first_par_size - 1)) {
245                                 first->insertChar(first_par_size, ' ');
246                         }
247
248                         cur.clearSelection();
249                         mergeParagraph(cur.buffer().params(), paragraphs(), 0);
250                 }
251         }
252
253         if (!cur.selection())
254                 cur.resetAnchor();
255
256         LyXView * lv = cur.bv().owner();
257         lv->view_state_changed();
258         lv->updateMenubar();
259         lv->updateToolbars();
260         if (old_pit != cur.pit()) {
261                 lv->setLayout(text_.getPar(cur.pit()).layout()->name());
262                 old_pit = cur.pit();
263         }
264 }
265
266
267 string const InsetText::editMessage() const
268 {
269         return _("Opened Text Inset");
270 }
271
272
273 void InsetText::edit(LCursor & cur, bool left)
274 {
275         //lyxerr << "InsetText: edit left/right" << endl;
276         old_pit = -1;
277         setViewCache(&cur.bv());
278         int const pit = left ? 0 : paragraphs().size() - 1;
279         int const pos = left ? 0 : paragraphs().back().size();
280         text_.setCursor(cur.top(), pit, pos);
281         cur.clearSelection();
282         finishUndo();
283 #ifdef WITH_WARNINGS
284 #warning can someone check if/when this is needed?
285 #endif
286 //Andre?
287 //      updateLocal(cur);
288 }
289
290
291 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
292 {
293         old_pit = -1;
294         return text_.editXY(cur, x, y);
295         //sanitizeEmptyText(cur.bv());
296         //updateLocal(cur);
297 }
298
299
300 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
301 {
302         lyxerr[Debug::DEBUG]
303                 << BOOST_CURRENT_FUNCTION
304                 << " [ cmd.action = " << cmd.action << ']'
305                 << endl;
306         
307         setViewCache(&cur.bv());
308
309         text_.dispatch(cur, cmd);
310
311 }
312
313
314 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
315         FuncStatus & status) const
316 {
317         return text_.getStatus(cur, cmd, status);
318 }
319
320
321 int InsetText::latex(Buffer const & buf, ostream & os,
322                      OutputParams const & runparams) const
323 {
324         TexRow texrow;
325         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
326         return texrow.rows();
327 }
328
329
330 int InsetText::plaintext(Buffer const & buf, ostream & os,
331                      OutputParams const & runparams) const
332 {
333         ParagraphList::const_iterator beg = paragraphs().begin();
334         ParagraphList::const_iterator end = paragraphs().end();
335         ParagraphList::const_iterator it = beg;
336         bool ref_printed = false;
337         for (; it != end; ++it)
338                 asciiParagraph(buf, *it, os, runparams, ref_printed);
339
340         // FIXME: Give the total numbers of lines
341         return 0;
342 }
343
344
345 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
346                         OutputParams const & runparams) const
347 {
348         linuxdocParagraphs(buf, paragraphs(), os, runparams);
349         return 0;
350 }
351
352
353 int InsetText::docbook(Buffer const & buf, ostream & os,
354                        OutputParams const & runparams) const
355 {
356         docbookParagraphs(paragraphs(), buf, os, runparams);
357         return 0;
358 }
359
360
361 void InsetText::validate(LaTeXFeatures & features) const
362 {
363         for_each(paragraphs().begin(), paragraphs().end(),
364                  bind(&Paragraph::validate, _1, ref(features)));
365 }
366
367
368 void InsetText::cursorPos
369         (CursorSlice const & sl, bool boundary, int & x, int & y) const
370 {
371         x = text_.cursorX(sl, boundary) + border_;
372         y = text_.cursorY(sl, boundary);
373 }
374
375
376 bool InsetText::showInsetDialog(BufferView *) const
377 {
378         return false;
379 }
380
381
382 void InsetText::getLabelList(Buffer const & buffer,
383                              std::vector<string> & list) const
384 {
385         ParagraphList::const_iterator pit = paragraphs().begin();
386         ParagraphList::const_iterator pend = paragraphs().end();
387         for (; pit != pend; ++pit) {
388                 InsetList::const_iterator beg = pit->insetlist.begin();
389                 InsetList::const_iterator end = pit->insetlist.end();
390                 for (; beg != end; ++beg)
391                         beg->inset->getLabelList(buffer, list);
392         }
393 }
394
395
396 void InsetText::markNew(bool track_changes)
397 {
398         ParagraphList::iterator pit = paragraphs().begin();
399         ParagraphList::iterator end = paragraphs().end();
400         for (; pit != end; ++pit) {
401                 if (track_changes) {
402                         pit->trackChanges();
403                 } else {
404                         // no-op when not tracking
405                         pit->cleanChanges();
406                 }
407         }
408 }
409
410
411 void InsetText::setText(string const & data, LyXFont const & font)
412 {
413         clear(false);
414         Paragraph & first = paragraphs().front();
415         for (unsigned int i = 0; i < data.length(); ++i)
416                 first.insertChar(i, data[i], font);
417 }
418
419
420 void InsetText::setAutoBreakRows(bool flag)
421 {
422         if (flag != text_.autoBreakRows_) {
423                 text_.autoBreakRows_ = flag;
424                 if (!flag)
425                         removeNewlines();
426         }
427 }
428
429
430 void InsetText::setDrawFrame(bool flag)
431 {
432         drawFrame_ = flag;
433 }
434
435
436 LColor_color InsetText::frameColor() const
437 {
438         return LColor::color(frame_color_);
439 }
440
441
442 void InsetText::setFrameColor(LColor_color col)
443 {
444         frame_color_ = col;
445 }
446
447
448 void InsetText::setViewCache(BufferView const * bv) const
449 {
450         if (bv && bv != text_.bv_owner) {
451                 //lyxerr << "setting view cache from "
452                 //      << text_.bv_owner << " to " << bv << "\n";
453                 text_.bv_owner = const_cast<BufferView *>(bv);
454         }
455 }
456
457
458 void InsetText::removeNewlines()
459 {
460         ParagraphList::iterator it = paragraphs().begin();
461         ParagraphList::iterator end = paragraphs().end();
462         for (; it != end; ++it)
463                 for (int i = 0; i < it->size(); ++i)
464                         if (it->isNewline(i))
465                                 it->erase(i);
466 }
467
468
469 LyXText * InsetText::getText(int i) const
470 {
471         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
472 }
473
474
475 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
476 {
477 #ifdef WITH_WARNINGS
478 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
479 // And it probably does. You have to take a look at this John. (Lgb)
480 #warning John, have a look here. (Lgb)
481 #endif
482         ParagraphList & pl = paragraphs();
483
484         ParagraphList::iterator pit = plist.begin();
485         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
486         ++pit;
487         mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
488
489         for_each(pit, plist.end(),
490                  bind(&ParagraphList::push_back, ref(pl), _1));
491 }
492
493
494 void InsetText::addPreview(PreviewLoader & loader) const
495 {
496         ParagraphList::const_iterator pit = paragraphs().begin();
497         ParagraphList::const_iterator pend = paragraphs().end();
498
499         for (; pit != pend; ++pit) {
500                 InsetList::const_iterator it  = pit->insetlist.begin();
501                 InsetList::const_iterator end = pit->insetlist.end();
502                 for (; it != end; ++it)
503                         it->inset->addPreview(loader);
504         }
505 }
506
507
508 ParagraphList const & InsetText::paragraphs() const
509 {
510         return text_.paragraphs();
511 }
512
513
514 ParagraphList & InsetText::paragraphs()
515 {
516         return text_.paragraphs();
517 }