]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Fix clipboard/selection encoding
[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_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/Painter.h"
49
50 #include "support/lyxalgo.h" // lyx::count
51
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
54
55 using lyx::docstring;
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         ErrorList errorList;
167         bool res = text_.read(buf, lex, errorList);
168         init();
169
170         if (!res) {
171                 lex.printError("Missing \\end_inset at this point. "
172                                            "Read: `$$Token'");
173         }
174
175         // sanity check
176         // ensure we have at least one paragraph.
177         if (paragraphs().empty())
178                 paragraphs().push_back(oldpar);
179 }
180
181
182 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
183 {
184         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
185         setViewCache(mi.base.bv);
186         mi.base.textwidth -= 2 * border_;
187         font_ = mi.base.font;
188         // Hand font through to contained lyxtext:
189         text_.font_ = mi.base.font;
190         text_.metrics(mi, dim);
191         dim.asc += border_;
192         dim.des += border_;
193         dim.wid += 2 * border_;
194         mi.base.textwidth += 2 * border_;
195         dim_ = dim;
196 }
197
198
199 void InsetText::draw(PainterInfo & pi, int x, int y) const
200 {
201         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
202         // update our idea of where we are
203         setPosCache(pi, x, y);
204
205         text_.background_color_ = backgroundColor();
206         text_.draw(pi, x + border_, y);
207
208         if (drawFrame_) {
209                 int const w = text_.width() + 2 * border_;
210                 int const a = text_.ascent() + border_;
211                 int const h = a + text_.descent() + border_;
212                 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h, 
213                         frameColor());
214         }
215 }
216
217
218 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
219 {
220         int const w = text_.width() + 2 * border_;
221         int const a = text_.ascent() + border_;
222         int const h = a + text_.descent() + border_;
223         pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h, 
224                 backgroundColor());
225         text_.drawSelection(pi, x, y);
226 }
227
228
229 string const InsetText::editMessage() const
230 {
231         return _("Opened Text Inset");
232 }
233
234
235 void InsetText::edit(LCursor & cur, bool left)
236 {
237         //lyxerr << "InsetText: edit left/right" << endl;
238         setViewCache(&cur.bv());
239         int const pit = left ? 0 : paragraphs().size() - 1;
240         int const pos = left ? 0 : paragraphs().back().size();
241         text_.setCursor(cur.top(), pit, pos);
242         cur.clearSelection();
243         finishUndo();
244 }
245
246
247 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
248 {
249         return text_.editXY(cur, x, y);
250 }
251
252
253 void InsetText::forceParagraphsToDefault(LCursor & cur)
254 {
255         BufferParams const & bp = cur.buffer().params();
256         LyXLayout_ptr const layout =
257                 bp.getLyXTextClass().defaultLayout();
258         ParagraphList::iterator const end = paragraphs().end();
259         for (ParagraphList::iterator par = paragraphs().begin();
260                         par != end; ++par)
261                 par->layout(layout);
262 }
263
264
265 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
266 {
267         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
268                              << " [ cmd.action = "
269                              << cmd.action << ']' << endl;
270         setViewCache(&cur.bv());
271
272         switch (cmd.action) {
273
274         case LFUN_CHAR_DELETE_FORWARD: {
275                 if (!cur.selection() && cur.depth() > 1
276                     && cur.pit() == cur.lastpit()
277                     && cur.pos() == cur.lastpos())
278                         // Merge inset with owner
279                         cmd = FuncRequest(LFUN_INSET_DISSOLVE);
280                 text_.dispatch(cur, cmd);
281                 break;
282         }
283
284         case LFUN_CHAR_DELETE_BACKWARD: {
285                 if (cur.depth() > 1 && cur.pit() == 0 && cur.pos() == 0)
286                         // Merge inset with owner
287                         cmd = FuncRequest(LFUN_INSET_DISSOLVE);
288                 text_.dispatch(cur, cmd);
289                 break;
290         }
291         
292         default:
293                 text_.dispatch(cur, cmd);
294                 break;
295         }
296 }
297
298
299 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
300         FuncStatus & status) const
301 {
302         switch (cmd.action) {
303
304         case LFUN_CHAR_DELETE_FORWARD:
305         case LFUN_CHAR_DELETE_BACKWARD:
306                 status.enabled(true);
307                 return true;
308
309         default:
310                 return text_.getStatus(cur, cmd, status);
311         }
312 }
313
314
315 int InsetText::latex(Buffer const & buf, ostream & os,
316                      OutputParams const & runparams) const
317 {
318         TexRow texrow;
319         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
320         return texrow.rows();
321 }
322
323
324 int InsetText::plaintext(Buffer const & buf, ostream & os,
325                      OutputParams const & runparams) const
326 {
327         ParagraphList::const_iterator beg = paragraphs().begin();
328         ParagraphList::const_iterator end = paragraphs().end();
329         ParagraphList::const_iterator it = beg;
330         bool ref_printed = false;
331         for (; it != end; ++it)
332                 asciiParagraph(buf, *it, os, runparams, ref_printed);
333
334         // FIXME: Give the total numbers of lines
335         return 1;
336 }
337
338
339 int InsetText::docbook(Buffer const & buf, ostream & os,
340                        OutputParams const & runparams) const
341 {
342         docbookParagraphs(paragraphs(), buf, os, runparams);
343         return 0;
344 }
345
346
347 void InsetText::validate(LaTeXFeatures & features) const
348 {
349         for_each(paragraphs().begin(), paragraphs().end(),
350                  bind(&Paragraph::validate, _1, ref(features)));
351 }
352
353
354 void InsetText::cursorPos
355         (CursorSlice const & sl, bool boundary, int & x, int & y) const
356 {
357         x = text_.cursorX(sl, boundary) + border_;
358         y = text_.cursorY(sl, boundary);
359 }
360
361
362 bool InsetText::showInsetDialog(BufferView *) const
363 {
364         return false;
365 }
366
367
368 void InsetText::markNew(bool track_changes)
369 {
370         ParagraphList::iterator pit = paragraphs().begin();
371         ParagraphList::iterator end = paragraphs().end();
372         for (; pit != end; ++pit) {
373                 if (track_changes)
374                         pit->trackChanges();
375                 else // no-op when not tracking
376                         pit->cleanChanges();
377         }
378 }
379
380
381 void InsetText::setText(docstring const & data, LyXFont const & font)
382 {
383         clear();
384         Paragraph & first = paragraphs().front();
385         for (unsigned int i = 0; i < data.length(); ++i)
386                 first.insertChar(i, data[i], font);
387 }
388
389
390 void InsetText::setAutoBreakRows(bool flag)
391 {
392         if (flag == text_.autoBreakRows_)
393                 return;
394
395         text_.autoBreakRows_ = flag;
396         if (flag)
397                 return;
398
399         // remove previously existing newlines
400         ParagraphList::iterator it = paragraphs().begin();
401         ParagraphList::iterator end = paragraphs().end();
402         for (; it != end; ++it)
403                 for (int i = 0; i < it->size(); ++i)
404                         if (it->isNewline(i))
405                                 it->erase(i);
406 }
407
408
409 void InsetText::setDrawFrame(bool flag)
410 {
411         drawFrame_ = flag;
412 }
413
414
415 LColor_color InsetText::frameColor() const
416 {
417         return LColor::color(frame_color_);
418 }
419
420
421 void InsetText::setFrameColor(LColor_color col)
422 {
423         frame_color_ = col;
424 }
425
426
427 void InsetText::setViewCache(BufferView const * bv) const
428 {
429         if (bv && bv != text_.bv_owner) {
430                 //lyxerr << "setting view cache from "
431                 //      << text_.bv_owner << " to " << bv << "\n";
432                 text_.bv_owner = const_cast<BufferView *>(bv);
433         }
434 }
435
436
437 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
438 {
439 #ifdef WITH_WARNINGS
440 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
441 // And it probably does. You have to take a look at this John. (Lgb)
442 #warning John, have a look here. (Lgb)
443 #endif
444         ParagraphList & pl = paragraphs();
445
446         ParagraphList::iterator pit = plist.begin();
447         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
448         ++pit;
449         mergeParagraph(buffer->params(), pl,
450                        std::distance(pl.begin(), ins) - 1);
451
452         for_each(pit, plist.end(),
453                  bind(&ParagraphList::push_back, ref(pl), _1));
454 }
455
456
457 void InsetText::addPreview(PreviewLoader & loader) const
458 {
459         ParagraphList::const_iterator pit = paragraphs().begin();
460         ParagraphList::const_iterator pend = paragraphs().end();
461
462         for (; pit != pend; ++pit) {
463                 InsetList::const_iterator it  = pit->insetlist.begin();
464                 InsetList::const_iterator end = pit->insetlist.end();
465                 for (; it != end; ++it)
466                         it->inset->addPreview(loader);
467         }
468 }
469
470
471 //FIXME: instead of this hack, which only works by chance,
472 // cells should have their own insetcell type, which returns CELL_CODE!
473 bool InsetText::neverIndent() const
474 {
475         // this is only true for tabular cells
476         return !text_.isMainText() && lyxCode() == TEXT_CODE;
477 }
478
479
480 ParagraphList const & InsetText::paragraphs() const
481 {
482         return text_.paragraphs();
483 }
484
485
486 ParagraphList & InsetText::paragraphs()
487 {
488         return text_.paragraphs();
489 }