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