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