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