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