]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Implement LFUN_INSET_DISSOLVE (bug 2201):
[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 "FuncStatus.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_linuxdoc.h"
37 #include "output_plaintext.h"
38 #include "paragraph.h"
39 #include "paragraph_funcs.h"
40 #include "ParagraphParameters.h"
41 #include "rowpainter.h"
42 #include "lyxrow.h"
43 #include "sgml.h"
44 #include "texrow.h"
45 #include "undo.h"
46
47 #include "frontends/Alert.h"
48 #include "frontends/font_metrics.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         // Dispose of the infamous L-shaped cursor.
85         text_.current_font.setLanguage(bp.language);
86         text_.real_current_font.setLanguage(bp.language);
87         init();
88 }
89
90
91 InsetText::InsetText(InsetText const & in)
92         : InsetOld(in), text_(in.text_.bv_owner)
93 {
94         text_.autoBreakRows_ = in.text_.autoBreakRows_;
95         drawFrame_ = in.drawFrame_;
96         frame_color_ = in.frame_color_;
97         text_.paragraphs() = in.text_.paragraphs();
98         // Hand current buffer language down to "cloned" textinsets
99         // e.g. tabular cells
100         text_.current_font = in.text_.current_font;
101         text_.real_current_font = in.text_.real_current_font;
102         init();
103 }
104
105
106 InsetText::InsetText()
107         : text_(0)
108 {}
109
110
111 void InsetText::init()
112 {
113         for_each(paragraphs().begin(), paragraphs().end(),
114                  bind(&Paragraph::setInsetOwner, _1, this));
115 }
116
117
118 void InsetText::markErased(bool erased)
119 {
120         ParagraphList & pars = paragraphs();
121         for_each(pars.begin(), pars.end(),
122                  bind(&Paragraph::markErased, _1, erased));
123 }
124
125
126 void InsetText::clear()
127 {
128         ParagraphList & pars = paragraphs();
129
130         // This is a gross hack...
131         LyXLayout_ptr old_layout = pars.begin()->layout();
132
133         pars.clear();
134         pars.push_back(Paragraph());
135         pars.begin()->setInsetOwner(this);
136         pars.begin()->layout(old_layout);
137 }
138
139
140 auto_ptr<InsetBase> InsetText::doClone() const
141 {
142         return auto_ptr<InsetBase>(new InsetText(*this));
143 }
144
145
146 void InsetText::write(Buffer const & buf, ostream & os) const
147 {
148         os << "Text\n";
149         text_.write(buf, os);
150 }
151
152
153 void InsetText::read(Buffer const & buf, LyXLex & lex)
154 {
155         clear();
156
157 #ifdef WITH_WARNINGS
158 #warning John, look here. Doesnt make much sense.
159 #endif
160         if (buf.params().tracking_changes)
161                 paragraphs().begin()->trackChanges();
162
163         // delete the initial paragraph
164         Paragraph oldpar = *paragraphs().begin();
165         paragraphs().clear();
166         bool res = text_.read(buf, lex);
167         init();
168
169         if (!res) {
170                 lex.printError("Missing \\end_inset at this point. "
171                                            "Read: `$$Token'");
172         }
173
174         // sanity check
175         // ensure we have at least one paragraph.
176         if (paragraphs().empty())
177                 paragraphs().push_back(oldpar);
178 }
179
180
181 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
182 {
183         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
184         setViewCache(mi.base.bv);
185         mi.base.textwidth -= 2 * border_;
186         font_ = mi.base.font;
187         // Hand font through to contained lyxtext:
188         text_.font_ = mi.base.font;
189         text_.metrics(mi, dim);
190         dim.asc += border_;
191         dim.des += border_;
192         dim.wid += 2 * border_;
193         mi.base.textwidth += 2 * border_;
194         dim_ = dim;
195 }
196
197
198 void InsetText::draw(PainterInfo & pi, int x, int y) const
199 {
200         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
201         // update our idea of where we are
202         setPosCache(pi, x, y);
203
204         text_.background_color_ = backgroundColor();
205         text_.draw(pi, x + border_, y);
206
207         if (drawFrame_) {
208                 int const w = text_.width() + 2 * border_;
209                 int const a = text_.ascent() + border_;
210                 int const h = a + text_.descent() + border_;
211                 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h, 
212                         frameColor());
213         }
214 }
215
216
217 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
218 {
219         int const w = text_.width() + 2 * border_;
220         int const a = text_.ascent() + border_;
221         int const h = a + text_.descent() + border_;
222         pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h, 
223                 backgroundColor());
224         text_.drawSelection(pi, x, y);
225 }
226
227
228 string const InsetText::editMessage() const
229 {
230         return _("Opened Text Inset");
231 }
232
233
234 void InsetText::edit(LCursor & cur, bool left)
235 {
236         //lyxerr << "InsetText: edit left/right" << endl;
237         setViewCache(&cur.bv());
238         int const pit = left ? 0 : paragraphs().size() - 1;
239         int const pos = left ? 0 : paragraphs().back().size();
240         text_.setCursor(cur.top(), pit, pos);
241         cur.clearSelection();
242         finishUndo();
243 }
244
245
246 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
247 {
248         return text_.editXY(cur, x, y);
249 }
250
251
252 void InsetText::forceParagraphsToDefault(LCursor & cur)
253 {
254         BufferParams const & bp = cur.buffer().params();
255         LyXLayout_ptr const layout =
256                 bp.getLyXTextClass().defaultLayout();
257         ParagraphList::iterator const end = paragraphs().end();
258         for (ParagraphList::iterator par = paragraphs().begin();
259                         par != end; ++par)
260                 par->layout(layout);
261 }
262
263
264 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
265 {
266         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
267                              << " [ cmd.action = "
268                              << cmd.action << ']' << endl;
269         setViewCache(&cur.bv());
270
271         switch (cmd.action) {
272
273         case LFUN_CHAR_DELETE_FORWARD: {
274                 if (!cur.selection() && cur.depth() > 1
275                     && cur.pit() == cur.lastpit()
276                     && cur.pos() == cur.lastpos())
277                         // Merge inset with owner
278                         cmd = FuncRequest(LFUN_INSET_DISSOLVE);
279                 text_.dispatch(cur, cmd);
280                 break;
281         }
282
283         case LFUN_CHAR_DELETE_BACKWARD: {
284                 if (cur.depth() > 1 && cur.pit() == 0 && cur.pos() == 0)
285                         // Merge inset with owner
286                         cmd = FuncRequest(LFUN_INSET_DISSOLVE);
287                 text_.dispatch(cur, cmd);
288                 break;
289         }
290         
291         default:
292                 text_.dispatch(cur, cmd);
293                 break;
294         }
295 }
296
297
298 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
299         FuncStatus & status) const
300 {
301         switch (cmd.action) {
302
303         case LFUN_CHAR_DELETE_FORWARD:
304         case LFUN_CHAR_DELETE_BACKWARD:
305                 status.enabled(true);
306                 return true;
307
308         default:
309                 return text_.getStatus(cur, cmd, status);
310         }
311 }
312
313
314 int InsetText::latex(Buffer const & buf, ostream & os,
315                      OutputParams const & runparams) const
316 {
317         TexRow texrow;
318         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
319         return texrow.rows();
320 }
321
322
323 int InsetText::plaintext(Buffer const & buf, ostream & os,
324                      OutputParams const & runparams) const
325 {
326         ParagraphList::const_iterator beg = paragraphs().begin();
327         ParagraphList::const_iterator end = paragraphs().end();
328         ParagraphList::const_iterator it = beg;
329         bool ref_printed = false;
330         for (; it != end; ++it)
331                 asciiParagraph(buf, *it, os, runparams, ref_printed);
332
333         // FIXME: Give the total numbers of lines
334         return 1;
335 }
336
337
338 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
339                         OutputParams const & runparams) const
340 {
341         linuxdocParagraphs(buf, paragraphs(), os, runparams);
342         return 0;
343 }
344
345
346 int InsetText::docbook(Buffer const & buf, ostream & os,
347                        OutputParams const & runparams) const
348 {
349         docbookParagraphs(paragraphs(), buf, os, runparams);
350         return 0;
351 }
352
353
354 void InsetText::validate(LaTeXFeatures & features) const
355 {
356         for_each(paragraphs().begin(), paragraphs().end(),
357                  bind(&Paragraph::validate, _1, ref(features)));
358 }
359
360
361 void InsetText::cursorPos
362         (CursorSlice const & sl, bool boundary, int & x, int & y) const
363 {
364         x = text_.cursorX(sl, boundary) + border_;
365         y = text_.cursorY(sl, boundary);
366 }
367
368
369 bool InsetText::showInsetDialog(BufferView *) const
370 {
371         return false;
372 }
373
374
375 void InsetText::markNew(bool track_changes)
376 {
377         ParagraphList::iterator pit = paragraphs().begin();
378         ParagraphList::iterator end = paragraphs().end();
379         for (; pit != end; ++pit) {
380                 if (track_changes)
381                         pit->trackChanges();
382                 else // no-op when not tracking
383                         pit->cleanChanges();
384         }
385 }
386
387
388 void InsetText::setText(string const & data, LyXFont const & font)
389 {
390         clear();
391         Paragraph & first = paragraphs().front();
392         for (unsigned int i = 0; i < data.length(); ++i)
393                 first.insertChar(i, data[i], font);
394 }
395
396
397 void InsetText::setAutoBreakRows(bool flag)
398 {
399         if (flag == text_.autoBreakRows_)
400                 return;
401
402         text_.autoBreakRows_ = flag;
403         if (flag)
404                 return;
405
406         // remove previously existing newlines
407         ParagraphList::iterator it = paragraphs().begin();
408         ParagraphList::iterator end = paragraphs().end();
409         for (; it != end; ++it)
410                 for (int i = 0; i < it->size(); ++i)
411                         if (it->isNewline(i))
412                                 it->erase(i);
413 }
414
415
416 void InsetText::setDrawFrame(bool flag)
417 {
418         drawFrame_ = flag;
419 }
420
421
422 LColor_color InsetText::frameColor() const
423 {
424         return LColor::color(frame_color_);
425 }
426
427
428 void InsetText::setFrameColor(LColor_color col)
429 {
430         frame_color_ = col;
431 }
432
433
434 void InsetText::setViewCache(BufferView const * bv) const
435 {
436         if (bv && bv != text_.bv_owner) {
437                 //lyxerr << "setting view cache from "
438                 //      << text_.bv_owner << " to " << bv << "\n";
439                 text_.bv_owner = const_cast<BufferView *>(bv);
440         }
441 }
442
443
444 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
445 {
446 #ifdef WITH_WARNINGS
447 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
448 // And it probably does. You have to take a look at this John. (Lgb)
449 #warning John, have a look here. (Lgb)
450 #endif
451         ParagraphList & pl = paragraphs();
452
453         ParagraphList::iterator pit = plist.begin();
454         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
455         ++pit;
456         mergeParagraph(buffer->params(), pl,
457                        std::distance(pl.begin(), ins) - 1);
458
459         for_each(pit, plist.end(),
460                  bind(&ParagraphList::push_back, ref(pl), _1));
461 }
462
463
464 void InsetText::addPreview(PreviewLoader & loader) const
465 {
466         ParagraphList::const_iterator pit = paragraphs().begin();
467         ParagraphList::const_iterator pend = paragraphs().end();
468
469         for (; pit != pend; ++pit) {
470                 InsetList::const_iterator it  = pit->insetlist.begin();
471                 InsetList::const_iterator end = pit->insetlist.end();
472                 for (; it != end; ++it)
473                         it->inset->addPreview(loader);
474         }
475 }
476
477
478 //FIXME: instead of this hack, which only works by chance,
479 // cells should have their own insetcell type, which returns CELL_CODE!
480 bool InsetText::neverIndent() const
481 {
482         // this is only true for tabular cells
483         return !text_.isMainText() && lyxCode() == TEXT_CODE;
484 }
485
486
487 ParagraphList const & InsetText::paragraphs() const
488 {
489         return text_.paragraphs();
490 }
491
492
493 ParagraphList & InsetText::paragraphs()
494 {
495         return text_.paragraphs();
496 }