]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
* lyxtext.h: make the paragraphs_ a pointer instead a ref to make the
[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 "debug.h"
21 #include "errorlist.h"
22 #include "funcrequest.h"
23 #include "gettext.h"
24 #include "intl.h"
25 #include "LColor.h"
26 #include "lyxfind.h"
27 #include "lyxlex.h"
28 #include "lyxrc.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "rowpainter.h"
34 #include "sgml.h"
35 #include "texrow.h"
36 #include "undo_funcs.h"
37 #include "WordLangTuple.h"
38
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
43
44 #include "support/lyxalgo.h" // lyx::count
45
46 #include <boost/bind.hpp>
47
48 using bv_funcs::replaceSelection;
49
50 using lyx::pos_type;
51
52 using lyx::graphics::PreviewLoader;
53
54 using lyx::support::isStrUnsignedInt;
55 using lyx::support::strToUnsignedInt;
56
57 using std::endl;
58 using std::for_each;
59 using std::max;
60 using std::string;
61 using std::auto_ptr;
62 using std::ostream;
63 using std::vector;
64
65
66 InsetText::InsetText(BufferParams const & bp)
67         : UpdatableInset(),
68           paragraphs(1),
69           autoBreakRows_(false),
70           drawFrame_(NEVER),
71           frame_color_(LColor::insetframe),
72           text_(0, this, true, paragraphs)
73 {
74         textwidth_ = 0; // broken
75         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
76         if (bp.tracking_changes)
77                 paragraphs.begin()->trackChanges();
78         init();
79 }
80
81
82 InsetText::InsetText(InsetText const & in)
83         : UpdatableInset(in),
84           text_(in.text_.bv_owner, this, true, paragraphs)
85 {
86         // this is ugly...
87         operator=(in);
88 }
89
90
91 void InsetText::operator=(InsetText const & in)
92 {
93         UpdatableInset::operator=(in);
94         paragraphs = in.paragraphs;
95         autoBreakRows_ = in.autoBreakRows_;
96         drawFrame_ = in.drawFrame_;
97         frame_color_ = in.frame_color_;
98         textwidth_ = in.textwidth_;
99         text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
100         init();
101 }
102
103
104 void InsetText::init()
105 {
106         ParagraphList::iterator pit = paragraphs.begin();
107         ParagraphList::iterator end = paragraphs.end();
108         for (; pit != end; ++pit)
109                 pit->setInsetOwner(this);
110         text_.paragraphs_ = &paragraphs;
111         top_y = 0;
112
113         locked = false;
114         inset_par = paragraphs.end();
115         inset_pos = 0;
116         inset_x = 0;
117         inset_y = 0;
118         no_selection = true;
119         the_locking_inset = 0;
120         old_par = paragraphs.end();
121         in_insetAllowed = false;
122         mouse_x = 0;
123         mouse_y = 0;
124 }
125
126
127 void InsetText::clear(bool just_mark_erased)
128 {
129         if (just_mark_erased) {
130                 ParagraphList::iterator it = paragraphs.begin();
131                 ParagraphList::iterator end = paragraphs.end();
132                 for (; it != end; ++it)
133                         it->markErased();
134                 return;
135         }
136
137         // This is a gross hack...
138         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
139
140         paragraphs.clear();
141         paragraphs.push_back(Paragraph());
142         paragraphs.begin()->setInsetOwner(this);
143         paragraphs.begin()->layout(old_layout);
144 }
145
146
147 auto_ptr<InsetBase> InsetText::clone() const
148 {
149         return auto_ptr<InsetBase>(new InsetText(*this));
150 }
151
152
153 void InsetText::write(Buffer const & buf, ostream & os) const
154 {
155         os << "Text\n";
156         writeParagraphData(buf, os);
157 }
158
159
160 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
161 {
162         ParagraphList::const_iterator it = paragraphs.begin();
163         ParagraphList::const_iterator end = paragraphs.end();
164         Paragraph::depth_type dth = 0;
165         for (; it != end; ++it) {
166                 it->write(buf, os, buf.params(), dth);
167         }
168 }
169
170
171 void InsetText::read(Buffer const & buf, LyXLex & lex)
172 {
173         string token;
174         Paragraph::depth_type depth = 0;
175
176         clear(false);
177
178 #warning John, look here. Doesnt make much sense.
179         if (buf.params().tracking_changes)
180                 paragraphs.begin()->trackChanges();
181
182         // delete the initial paragraph
183         Paragraph oldpar = *paragraphs.begin();
184         paragraphs.clear();
185         ParagraphList::iterator pit = paragraphs.begin();
186
187         while (lex.isOK()) {
188                 lex.nextToken();
189                 token = lex.getString();
190                 if (token.empty())
191                         continue;
192                 if (token == "\\end_inset") {
193                         break;
194                 }
195
196                 if (token == "\\end_document") {
197                         lex.printError("\\end_document read in inset! Error in document!");
198                         return;
199                 }
200
201                 // FIXME: ugly.
202                 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
203         }
204
205         pit = paragraphs.begin();
206         ParagraphList::iterator const end = paragraphs.end();
207         for (; pit != end; ++pit)
208                 pit->setInsetOwner(this);
209
210         if (token != "\\end_inset") {
211                 lex.printError("Missing \\end_inset at this point. "
212                                            "Read: `$$Token'");
213         }
214
215         // sanity check
216         // ensure we have at least one par.
217         if (paragraphs.empty())
218                 paragraphs.push_back(oldpar);
219 }
220
221
222 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
223 {
224         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
225
226         textwidth_ = mi.base.textwidth;
227         BufferView * bv = mi.base.bv;
228         setViewCache(bv);
229         text_.metrics(mi, dim);
230         dim.asc += TEXT_TO_INSET_OFFSET;
231         dim.des += TEXT_TO_INSET_OFFSET;
232         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
233         dim.wid = max(dim.wid, 10);
234         dim_ = dim;
235 }
236
237
238 int InsetText::textWidth() const
239 {
240         return textwidth_;
241 }
242
243
244 void InsetText::draw(PainterInfo & pi, int x, int y) const
245 {
246         // update our idea of where we are. Clearly, we should
247         // not have to know this information.
248         top_x = x;
249
250         int const start_x = x;
251
252         BufferView * bv = pi.base.bv;
253         Painter & pain = pi.pain;
254
255         // repaint the background if needed
256         if (backgroundColor() != LColor::background)
257                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
258
259         // no draw is necessary !!!
260         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
261                 top_baseline = y;
262                 return;
263         }
264
265         bv->hideCursor();
266
267         if (!owner())
268                 x += scroll();
269
270         top_baseline = y;
271         top_y = y - dim_.asc;
272
273         if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
274                 inset_x = cx() - x;
275                 inset_y = cy();
276         }
277
278         x += TEXT_TO_INSET_OFFSET;
279
280         paintTextInset(*bv, text_, x, y);
281
282         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
283                 drawFrame(pain, start_x);
284 }
285
286
287 void InsetText::drawFrame(Painter & pain, int x) const
288 {
289         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
290         int const frame_x = x + ttoD2;
291         int const frame_y = top_baseline - dim_.asc + ttoD2;
292         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
293         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
294         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
295 }
296
297
298 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
299 {
300         if (!bv)
301                 return;
302
303         if (!autoBreakRows_ && paragraphs.size() > 1)
304                 collapseParagraphs(bv);
305
306         if (!text_.selection.set())
307                 text_.selection.cursor = text_.cursor;
308
309         bv->fitCursor();
310         bv->updateInset(this);
311         bv->owner()->view_state_changed();
312         bv->owner()->updateMenubar();
313         bv->owner()->updateToolbar();
314         if (old_par != cpar()) {
315                 bv->owner()->setLayout(cpar()->layout()->name());
316                 old_par = cpar();
317         }
318 }
319
320
321 string const InsetText::editMessage() const
322 {
323         return _("Opened Text Inset");
324 }
325
326
327 void InsetText::insetUnlock(BufferView * bv)
328 {
329         if (the_locking_inset) {
330                 the_locking_inset->insetUnlock(bv);
331                 the_locking_inset = 0;
332                 updateLocal(bv, false);
333         }
334         no_selection = true;
335         locked = false;
336
337         if (text_.selection.set()) {
338                 text_.clearSelection();
339         } else if (owner()) {
340                 bv->owner()->setLayout(owner()->getLyXText(bv)
341                                        ->cursor.par()->layout()->name());
342         } else
343                 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
344         // hack for deleteEmptyParMech
345         ParagraphList::iterator first_par = paragraphs.begin();
346         if (!first_par->empty()) {
347                 text_.setCursor(first_par, 0);
348         } else if (paragraphs.size() > 1) {
349                 text_.setCursor(boost::next(first_par), 0);
350         }
351 }
352
353
354 void InsetText::lockInset(BufferView * bv)
355 {
356         locked = true;
357         the_locking_inset = 0;
358         inset_pos = inset_x = inset_y = 0;
359         inset_boundary = false;
360         inset_par = paragraphs.end();
361         old_par = paragraphs.end();
362         text_.setCursorIntern(paragraphs.begin(), 0);
363         text_.clearSelection();
364         finishUndo();
365         // If the inset is empty set the language of the current font to the
366         // language to the surronding text (if different).
367         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
368                 bv->getParentLanguage(this) != text_.current_font.language()) {
369                 LyXFont font(LyXFont::ALL_IGNORE);
370                 font.setLanguage(bv->getParentLanguage(this));
371                 setFont(bv, font, false);
372         }
373 }
374
375
376 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
377 {
378         the_locking_inset = inset;
379         inset_x = cx() - top_x;
380         inset_y = cy();
381         inset_pos = cpos();
382         inset_par = cpar();
383         inset_boundary = cboundary();
384 }
385
386
387 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
388 {
389         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
390                               << inset << "): " << endl;
391         if (!inset)
392                 return false;
393         if (!the_locking_inset) {
394                 ParagraphList::iterator pit = paragraphs.begin();
395                 ParagraphList::iterator pend = paragraphs.end();
396
397                 for (; pit != pend; ++pit) {
398                         InsetList::iterator it = pit->insetlist.begin();
399                         InsetList::iterator const end = pit->insetlist.end();
400                         for (; it != end; ++it) {
401                                 if (it->inset == inset) {
402                                         lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
403                                         text_.setCursorIntern(pit, it->pos);
404                                         lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
405                                         lyxerr << "bv: " << bv << " inset: " << inset << endl;
406                                         lockInset(bv, inset);
407                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
408                                         return true;
409                                 }
410                         }
411                 }
412                 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
413                 return false;
414         }
415         if (inset == cpar()->getInset(cpos())) {
416                 lyxerr[Debug::INSETS] << "OK" << endl;
417                 lockInset(bv, inset);
418                 return true;
419         }
420
421         if (the_locking_inset && the_locking_inset == inset) {
422                 if (cpar() == inset_par && cpos() == inset_pos) {
423                         lyxerr[Debug::INSETS] << "OK" << endl;
424                         inset_x = cx() - top_x;
425                         inset_y = cy();
426                 } else {
427                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
428                 }
429         } else if (the_locking_inset) {
430                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
431                 return the_locking_inset->lockInsetInInset(bv, inset);
432         }
433         lyxerr[Debug::INSETS] << "NOT OK" << endl;
434         return false;
435 }
436
437
438 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
439                                    bool lr)
440 {
441         if (!the_locking_inset)
442                 return false;
443         if (the_locking_inset == inset) {
444                 the_locking_inset->insetUnlock(bv);
445                 the_locking_inset = 0;
446                 if (lr)
447                         moveRightIntern(bv, true, false);
448                 old_par = paragraphs.end(); // force layout setting
449                 if (scroll())
450                         scroll(bv, 0.0F);
451                 else
452                         updateLocal(bv, false);
453                 return true;
454         }
455         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
456 }
457
458
459 void InsetText::lfunMousePress(FuncRequest const & cmd)
460 {
461         no_selection = true;
462
463         // use this to check mouse motion for selection!
464         mouse_x = cmd.x;
465         mouse_y = cmd.y;
466
467         BufferView * bv = cmd.view();
468         FuncRequest cmd1 = cmd;
469         cmd1.x -= inset_x;
470         cmd1.y -= inset_y;
471         if (!locked)
472                 lockInset(bv);
473
474         int tmp_x = cmd.x;
475         int tmp_y = cmd.y + dim_.asc - bv->top_y();
476         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
477
478         if (the_locking_inset) {
479                 if (the_locking_inset == inset) {
480                         the_locking_inset->localDispatch(cmd1);
481                         return;
482                 }
483                 // otherwise only unlock the_locking_inset
484                 the_locking_inset->insetUnlock(bv);
485                 the_locking_inset = 0;
486         }
487         if (!inset)
488                 no_selection = false;
489
490         if (bv->theLockingInset()) {
491                 if (isHighlyEditableInset(inset)) {
492                         // We just have to lock the inset before calling a
493                         // PressEvent on it!
494                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
495                         if (!bv->lockInset(uinset)) {
496                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
497                         }
498                         inset->localDispatch(cmd1);
499                         if (the_locking_inset)
500                                 updateLocal(bv, false);
501                         return;
502                 }
503         }
504         if (!inset) {
505                 bool paste_internally = false;
506                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
507                         localDispatch(FuncRequest(bv, LFUN_COPY));
508                         paste_internally = true;
509                 }
510                 int old_top_y = bv->top_y();
511
512                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
513                 // set the selection cursor!
514                 text_.selection.cursor = text_.cursor;
515                 text_.cursor.x_fix(text_.cursor.x());
516
517                 text_.clearSelection();
518                 updateLocal(bv, false);
519
520                 bv->owner()->setLayout(cpar()->layout()->name());
521
522                 // we moved the view we cannot do mouse selection in this case!
523                 if (bv->top_y() != old_top_y)
524                         no_selection = true;
525                 old_par = cpar();
526                 // Insert primary selection with middle mouse
527                 // if there is a local selection in the current buffer,
528                 // insert this
529                 if (cmd.button() == mouse_button::button2) {
530                         if (paste_internally)
531                                 localDispatch(FuncRequest(bv, LFUN_PASTE));
532                         else
533                                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
534                 }
535         } else {
536                 getLyXText(bv)->clearSelection();
537         }
538 }
539
540
541 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
542 {
543         BufferView * bv = cmd.view();
544         FuncRequest cmd1 = cmd;
545         cmd1.x -= inset_x;
546         cmd1.y -= inset_y;
547
548         no_selection = true;
549         if (the_locking_inset)
550                 return the_locking_inset->localDispatch(cmd1);
551
552         int tmp_x = cmd.x;
553         int tmp_y = cmd.y + dim_.asc - bv->top_y();
554         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
555         bool ret = false;
556         if (inset) {
557 // This code should probably be removed now. Simple insets
558 // (!highlyEditable) can actually take the localDispatch,
559 // and turn it into edit() if necessary. But we still
560 // need to deal properly with the whole relative vs.
561 // absolute mouse co-ords thing in a realiable, sensible way
562 #if 0
563                 if (isHighlyEditableInset(inset))
564                         ret = inset->localDispatch(cmd1);
565                 else {
566                         inset_x = cx(bv) - top_x;
567                         inset_y = cy();
568                         cmd1.x = cmd.x - inset_x;
569                         cmd1.y = cmd.x - inset_y;
570                         inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
571                         ret = true;
572                 }
573 #endif
574                 ret = inset->localDispatch(cmd1);
575                 updateLocal(bv, false);
576
577         }
578         return ret;
579 }
580
581
582 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
583 {
584         FuncRequest cmd1 = cmd;
585         cmd1.x -= inset_x;
586         cmd1.y -= inset_y;
587
588         if (the_locking_inset) {
589                 the_locking_inset->localDispatch(cmd1);
590                 return;
591         }
592
593         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
594                 return;
595
596         BufferView * bv = cmd.view();
597         LyXCursor cur = text_.cursor;
598         text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
599         text_.cursor.x_fix(text_.cursor.x());
600         if (cur == text_.cursor)
601                 return;
602         text_.setSelection();
603         updateLocal(bv, false);
604 }
605
606
607 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
608 {
609         BufferView * bv = cmd.view();
610         setViewCache(bv);
611
612         switch (cmd.action) {
613         case LFUN_INSET_EDIT: {
614                 UpdatableInset::localDispatch(cmd);
615
616                 if (!bv->lockInset(this)) {
617                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
618                         return DISPATCHED;
619                 }
620
621                 locked = true;
622                 the_locking_inset = 0;
623                 inset_pos = 0;
624                 inset_x = 0;
625                 inset_y = 0;
626                 inset_boundary = false;
627                 inset_par = paragraphs.end();
628                 old_par = paragraphs.end();
629
630
631                 if (cmd.argument.size()) {
632                         if (cmd.argument == "left")
633                                 text_.setCursorIntern(paragraphs.begin(), 0);
634                         else {
635                                 ParagraphList::iterator it = boost::prior(paragraphs.end());
636                                 text_.setCursor(it, it->size());
637                         }
638                 } else {
639                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
640                         // we put here -1 and not button as now the button in the
641                         // edit call should not be needed we will fix this in 1.3.x
642                         // cycle hopefully (Jug 20020509)
643                         // FIXME: GUII I've changed this to none: probably WRONG
644                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
645                                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
646                                 text_.cursor.x(text_.cursor.x());
647                                 text_.cursor.x_fix(text_.cursor.x());
648                         }
649                 }
650
651                 text_.clearSelection();
652                 finishUndo();
653
654                 // If the inset is empty set the language of the current font to the
655                 // language to the surronding text (if different).
656                 if (paragraphs.begin()->empty() &&
657                     paragraphs.size() == 1 &&
658                     bv->getParentLanguage(this) != text_.current_font.language())
659                 {
660                         LyXFont font(LyXFont::ALL_IGNORE);
661                         font.setLanguage(bv->getParentLanguage(this));
662                         setFont(bv, font, false);
663                 }
664
665                 updateLocal(bv, false);
666                 // Tell the paragraph dialog that we've entered an insettext.
667                 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
668                 return DISPATCHED;
669         }
670
671         case LFUN_MOUSE_PRESS:
672                 lfunMousePress(cmd);
673                 return DISPATCHED;
674
675         case LFUN_MOUSE_MOTION:
676                 lfunMouseMotion(cmd);
677                 return DISPATCHED;
678
679         case LFUN_MOUSE_RELEASE:
680                 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
681
682         default:
683                 break;
684         }
685
686         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
687         no_selection = false;
688
689         RESULT result = UpdatableInset::localDispatch(cmd);
690         if (result != UNDISPATCHED)
691                 return DISPATCHED;
692
693         result = DISPATCHED;
694         if (cmd.action < 0 && cmd.argument.empty())
695                 return FINISHED;
696
697         if (the_locking_inset) {
698                 result = the_locking_inset->localDispatch(cmd);
699                 if (result == DISPATCHED_NOUPDATE)
700                         return result;
701                 if (result == DISPATCHED) {
702                         updateLocal(bv, false);
703                         return result;
704                 }
705                 if (result >= FINISHED) {
706                         switch (result) {
707                         case FINISHED_RIGHT:
708                                 moveRightIntern(bv, false, false);
709                                 result = DISPATCHED;
710                                 break;
711                         case FINISHED_UP:
712                                 result = moveUp(bv);
713                                 if (result >= FINISHED) {
714                                         updateLocal(bv, false);
715                                         bv->unlockInset(this);
716                                 }
717                                 break;
718                         case FINISHED_DOWN:
719                                 result = moveDown(bv);
720                                 if (result >= FINISHED) {
721                                         updateLocal(bv, false);
722                                         bv->unlockInset(this);
723                                 }
724                                 break;
725                         default:
726                                 result = DISPATCHED;
727                                 break;
728                         }
729                         the_locking_inset = 0;
730                         updateLocal(bv, false);
731                         // make sure status gets reset immediately
732                         bv->owner()->clearMessage();
733                         return result;
734                 }
735         }
736         bool updflag = false;
737
738         switch (cmd.action) {
739
740         // Normal chars
741         case LFUN_SELFINSERT:
742                 if (bv->buffer()->isReadonly()) {
743 //          setErrorMessage(N_("Document is read only"));
744                         break;
745                 }
746                 if (!cmd.argument.empty()) {
747                         /* Automatically delete the currently selected
748                          * text and replace it with what is being
749                          * typed in now. Depends on lyxrc settings
750                          * "auto_region_delete", which defaults to
751                          * true (on). */
752 #if 0
753                         // This should not be needed here and is also WRONG!
754                         recordUndo(bv, Undo::INSERT, text_.cursor.par());
755 #endif
756                         bv->switchKeyMap();
757
758                         if (lyxrc.auto_region_delete && text_.selection.set())
759                                 text_.cutSelection(false, false);
760                         text_.clearSelection();
761
762                         for (string::size_type i = 0; i < cmd.argument.length(); ++i)
763                                 bv->owner()->getIntl().getTransManager().
764                                         TranslateAndInsert(cmd.argument[i], &text_);
765                 }
766                 text_.selection.cursor = text_.cursor;
767                 updflag = true;
768                 result = DISPATCHED_NOUPDATE;
769                 break;
770
771         // cursor movements that need special handling
772
773         case LFUN_RIGHT:
774                 result = moveRight(bv);
775                 finishUndo();
776                 break;
777         case LFUN_LEFT:
778                 finishUndo();
779                 result = moveLeft(bv);
780                 break;
781         case LFUN_DOWN:
782                 finishUndo();
783                 result = moveDown(bv);
784                 break;
785         case LFUN_UP:
786                 finishUndo();
787                 result = moveUp(bv);
788                 break;
789
790         case LFUN_PRIOR:
791                 if (crow() == text_.firstRow())
792                         result = FINISHED_UP;
793                 else {
794                         text_.cursorPrevious();
795                         text_.clearSelection();
796                         result = DISPATCHED_NOUPDATE;
797                 }
798                 break;
799
800         case LFUN_NEXT:
801                 if (crow() == text_.lastRow())
802                         result = FINISHED_DOWN;
803                 else {
804                         text_.cursorNext();
805                         text_.clearSelection();
806                         result = DISPATCHED_NOUPDATE;
807                 }
808                 break;
809
810         case LFUN_BACKSPACE:
811                 if (text_.selection.set())
812                         text_.cutSelection(true, false);
813                 else
814                         text_.backspace();
815                 updflag = true;
816                 break;
817
818         case LFUN_DELETE:
819                 if (text_.selection.set())
820                         text_.cutSelection(true, false);
821                 else
822                         text_.Delete();
823                 updflag = true;
824                 break;
825
826         case LFUN_PASTE: {
827                 if (!autoBreakRows_) {
828                         if (CutAndPaste::nrOfParagraphs() > 1) {
829 #ifdef WITH_WARNINGS
830 #warning FIXME horrendously bad UI
831 #endif
832                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
833                                 break;
834                         }
835                 }
836
837                 replaceSelection(bv->getLyXText());
838                 size_t sel_index = 0;
839                 string const & arg = cmd.argument;
840                 if (isStrUnsignedInt(arg)) {
841                         size_t const paste_arg = strToUnsignedInt(arg);
842 #warning FIXME Check if the arg is in the domain of available selections.
843                         sel_index = paste_arg;
844                 }
845                 text_.pasteSelection(sel_index);
846                 // bug 393
847                 text_.clearSelection();
848                 updflag = true;
849                 break;
850         }
851
852         case LFUN_BREAKPARAGRAPH:
853                 if (!autoBreakRows_) {
854                         result = DISPATCHED;
855                         break;
856                 }
857                 replaceSelection(bv->getLyXText());
858                 text_.breakParagraph(paragraphs, 0);
859                 updflag = true;
860                 break;
861
862         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
863                 if (!autoBreakRows_) {
864                         result = DISPATCHED;
865                         break;
866                 }
867                 replaceSelection(bv->getLyXText());
868                 text_.breakParagraph(paragraphs, 1);
869                 updflag = true;
870                 break;
871
872         case LFUN_BREAKLINE: {
873                 if (!autoBreakRows_) {
874                         result = DISPATCHED;
875                         break;
876                 }
877
878                 replaceSelection(bv->getLyXText());
879                 text_.insertInset(new InsetNewline);
880                 updflag = true;
881                 break;
882         }
883
884         case LFUN_LAYOUT:
885                 // do not set layouts on non breakable textinsets
886                 if (autoBreakRows_) {
887                         string cur_layout = cpar()->layout()->name();
888
889                         // Derive layout number from given argument (string)
890                         // and current buffer's textclass (number).
891                         LyXTextClass const & tclass =
892                                 bv->buffer()->params().getLyXTextClass();
893                         string layout = cmd.argument;
894                         bool hasLayout = tclass.hasLayout(layout);
895
896                         // If the entry is obsolete, use the new one instead.
897                         if (hasLayout) {
898                                 string const & obs = tclass[layout]->obsoleted_by();
899                                 if (!obs.empty())
900                                         layout = obs;
901                         }
902
903                         // see if we found the layout number:
904                         if (!hasLayout) {
905                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
906                                 bv->owner()->dispatch(lf);
907                                 break;
908                         }
909
910                         if (cur_layout != layout) {
911                                 cur_layout = layout;
912                                 text_.setLayout(layout);
913                                 bv->owner()->setLayout(cpar()->layout()->name());
914                                 updflag = true;
915                         }
916                 } else {
917                         // reset the layout box
918                         bv->owner()->setLayout(cpar()->layout()->name());
919                 }
920                 break;
921
922         default:
923                 if (!bv->dispatch(cmd))
924                         result = UNDISPATCHED;
925                 break;
926         }
927
928         updateLocal(bv, updflag);
929
930         /// If the action has deleted all text in the inset, we need to change the
931         // language to the language of the surronding text.
932         if (!was_empty && paragraphs.begin()->empty() &&
933             paragraphs.size() == 1) {
934                 LyXFont font(LyXFont::ALL_IGNORE);
935                 font.setLanguage(bv->getParentLanguage(this));
936                 setFont(bv, font, false);
937         }
938
939         if (result >= FINISHED)
940                 bv->unlockInset(this);
941
942         if (result == DISPATCHED_NOUPDATE)
943                 result = DISPATCHED;
944         return result;
945 }
946
947
948 int InsetText::latex(Buffer const & buf, ostream & os,
949                      LatexRunParams const & runparams) const
950 {
951         TexRow texrow;
952         latexParagraphs(buf, paragraphs, os, texrow, runparams);
953         return texrow.rows();
954 }
955
956
957 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
958 {
959         unsigned int lines = 0;
960
961         ParagraphList::const_iterator beg = paragraphs.begin();
962         ParagraphList::const_iterator end = paragraphs.end();
963         ParagraphList::const_iterator it = beg;
964         for (; it != end; ++it) {
965                 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
966                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
967                 os << tmp;
968         }
969         return lines;
970 }
971
972
973 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
974 {
975         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
976         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
977
978         // There is a confusion between the empty paragraph and the default paragraph
979         // The default paragraph is <p></p>, the empty paragraph is *empty*
980         // Since none of the floats of linuxdoc accepts standard paragraphs
981         // I disable them. I don't expect problems. (jamatos 2003/07/27)
982         for (; pit != pend; ++pit) {
983                 const string name = pit->layout()->latexname();
984                 if (name != "p")
985                         sgml::openTag(os, 1, 0, name);
986                 buf.simpleLinuxDocOnePar(os, pit, 0);
987                 if (name != "p")
988                         sgml::closeTag(os, 1, 0, name);
989         }
990         return 0;
991 }
992
993
994 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
995 {
996         unsigned int lines = 0;
997
998         vector<string> environment_stack(10);
999         vector<string> environment_inner(10);
1000
1001         int const command_depth = 0;
1002         string item_name;
1003
1004         Paragraph::depth_type depth = 0; // paragraph depth
1005
1006         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1007         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1008
1009         for (; pit != pend; ++pit) {
1010                 int desc_on = 0; // description mode
1011
1012                 LyXLayout_ptr const & style = pit->layout();
1013
1014                 // environment tag closing
1015                 for (; depth > pit->params().depth(); --depth) {
1016                         if (environment_inner[depth] != "!-- --") {
1017                                 item_name = "listitem";
1018                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1019                                 if (environment_inner[depth] == "varlistentry")
1020                                         lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1021                         }
1022                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1023                         environment_stack[depth].erase();
1024                         environment_inner[depth].erase();
1025                 }
1026
1027                 if (depth == pit->params().depth()
1028                    && environment_stack[depth] != style->latexname()
1029                    && !environment_stack[depth].empty()) {
1030                         if (environment_inner[depth] != "!-- --") {
1031                                 item_name= "listitem";
1032                                 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1033                                 if (environment_inner[depth] == "varlistentry")
1034                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1035                         }
1036
1037                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1038
1039                         environment_stack[depth].erase();
1040                         environment_inner[depth].erase();
1041                 }
1042
1043                 // Write opening SGML tags.
1044                 switch (style->latextype) {
1045                 case LATEX_PARAGRAPH:
1046                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1047                         break;
1048
1049                 case LATEX_COMMAND:
1050                         buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1051                         return -1;
1052                         break;
1053
1054                 case LATEX_ENVIRONMENT:
1055                 case LATEX_ITEM_ENVIRONMENT:
1056                         if (depth < pit->params().depth()) {
1057                                 depth = pit->params().depth();
1058                                 environment_stack[depth].erase();
1059                         }
1060
1061                         if (environment_stack[depth] != style->latexname()) {
1062                                 if (environment_stack.size() == depth + 1) {
1063                                         environment_stack.push_back("!-- --");
1064                                         environment_inner.push_back("!-- --");
1065                                 }
1066                                 environment_stack[depth] = style->latexname();
1067                                 environment_inner[depth] = "!-- --";
1068                                 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1069                         } else {
1070                                 if (environment_inner[depth] != "!-- --") {
1071                                         item_name= "listitem";
1072                                         lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1073                                         if (environment_inner[depth] == "varlistentry")
1074                                                 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1075                                 }
1076                         }
1077
1078                         if (style->latextype == LATEX_ENVIRONMENT) {
1079                                 if (!style->latexparam().empty()) {
1080                                         if (style->latexparam() == "CDATA")
1081                                                 os << "<![CDATA[";
1082                                         else
1083                                           lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1084                                 }
1085                                 break;
1086                         }
1087
1088                         desc_on = (style->labeltype == LABEL_MANUAL);
1089
1090                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1091                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1092
1093                         item_name = desc_on ? "term" : "para";
1094                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1095
1096                         break;
1097                 default:
1098                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1099                         break;
1100                 }
1101
1102                 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1103
1104                 string end_tag;
1105                 // write closing SGML tags
1106                 switch (style->latextype) {
1107                 case LATEX_ENVIRONMENT:
1108                         if (!style->latexparam().empty()) {
1109                                 if (style->latexparam() == "CDATA")
1110                                         os << "]]>";
1111                                 else
1112                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1113                         }
1114                         break;
1115                 case LATEX_ITEM_ENVIRONMENT:
1116                         if (desc_on == 1)
1117                                 break;
1118                         end_tag= "para";
1119                         lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1120                         break;
1121                 case LATEX_PARAGRAPH:
1122                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1123                         break;
1124                 default:
1125                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1126                         break;
1127                 }
1128         }
1129
1130         // Close open tags
1131         for (int d = depth; d >= 0; --d) {
1132                 if (!environment_stack[depth].empty()) {
1133                         if (environment_inner[depth] != "!-- --") {
1134                                 item_name = "listitem";
1135                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1136                                if (environment_inner[depth] == "varlistentry")
1137                                        lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1138                         }
1139
1140                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1141                 }
1142         }
1143
1144         return lines;
1145 }
1146
1147
1148 void InsetText::validate(LaTeXFeatures & features) const
1149 {
1150         for_each(paragraphs.begin(), paragraphs.end(),
1151                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1152 }
1153
1154
1155 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1156 {
1157         if (the_locking_inset) {
1158                 the_locking_inset->getCursor(bv, x, y);
1159                 return;
1160         }
1161         x = cx();
1162         y = cy() + InsetText::y();
1163 }
1164
1165
1166 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1167 {
1168         if (the_locking_inset) {
1169                 the_locking_inset->getCursorPos(bv, x, y);
1170                 return;
1171         }
1172         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1173         y = cy() - TEXT_TO_INSET_OFFSET;
1174 }
1175
1176
1177 int InsetText::insetInInsetY() const
1178 {
1179         if (!the_locking_inset)
1180                 return 0;
1181
1182         return inset_y + the_locking_inset->insetInInsetY();
1183 }
1184
1185
1186 void InsetText::fitInsetCursor(BufferView * bv) const
1187 {
1188         if (the_locking_inset) {
1189                 the_locking_inset->fitInsetCursor(bv);
1190                 return;
1191         }
1192
1193         LyXFont const font = text_.getFont(cpar(), cpos());
1194
1195         int const asc = font_metrics::maxAscent(font);
1196         int const desc = font_metrics::maxDescent(font);
1197
1198         bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1199 }
1200
1201
1202 InsetOld::RESULT InsetText::moveRight(BufferView * bv)
1203 {
1204         if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1205                 return moveLeftIntern(bv, false, true, false);
1206         else
1207                 return moveRightIntern(bv, true, true, false);
1208 }
1209
1210
1211 InsetOld::RESULT InsetText::moveLeft(BufferView * bv)
1212 {
1213         if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params()))
1214                 return moveRightIntern(bv, true, true, false);
1215         else
1216                 return moveLeftIntern(bv, false, true, false);
1217 }
1218
1219
1220 InsetOld::RESULT
1221 InsetText::moveRightIntern(BufferView * bv, bool front,
1222                            bool activate_inset, bool selecting)
1223 {
1224         ParagraphList::iterator c_par = cpar();
1225
1226         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1227                 return FINISHED_RIGHT;
1228         if (activate_inset && checkAndActivateInset(bv, front))
1229                 return DISPATCHED;
1230         text_.cursorRight(bv);
1231         if (!selecting)
1232                 text_.clearSelection();
1233         return DISPATCHED_NOUPDATE;
1234 }
1235
1236
1237 InsetOld::RESULT
1238 InsetText::moveLeftIntern(BufferView * bv, bool front,
1239                           bool activate_inset, bool selecting)
1240 {
1241         if (cpar() == paragraphs.begin() && cpos() <= 0)
1242                 return FINISHED;
1243         text_.cursorLeft(bv);
1244         if (!selecting)
1245                 text_.clearSelection();
1246         if (activate_inset && checkAndActivateInset(bv, front))
1247                 return DISPATCHED;
1248         return DISPATCHED_NOUPDATE;
1249 }
1250
1251
1252 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1253 {
1254         if (crow() == text_.firstRow())
1255                 return FINISHED_UP;
1256         text_.cursorUp(bv);
1257         text_.clearSelection();
1258         return DISPATCHED_NOUPDATE;
1259 }
1260
1261
1262 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1263 {
1264         if (crow() == text_.lastRow())
1265                 return FINISHED_DOWN;
1266         text_.cursorDown(bv);
1267         text_.clearSelection();
1268         return DISPATCHED_NOUPDATE;
1269 }
1270
1271
1272 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1273 {
1274         if (the_locking_inset) {
1275                 if (the_locking_inset->insetAllowed(inset))
1276                         return the_locking_inset->insertInset(bv, inset);
1277                 return false;
1278         }
1279         inset->setOwner(this);
1280         text_.insertInset(inset);
1281         bv->fitCursor();
1282         updateLocal(bv, true);
1283         return true;
1284 }
1285
1286
1287 bool InsetText::insetAllowed(InsetOld::Code code) const
1288 {
1289         // in_insetAllowed is a really gross hack,
1290         // to allow us to call the owner's insetAllowed
1291         // without stack overflow, which can happen
1292         // when the owner uses InsetCollapsable::insetAllowed()
1293         bool ret = true;
1294         if (in_insetAllowed)
1295                 return ret;
1296         in_insetAllowed = true;
1297         if (the_locking_inset)
1298                 ret = the_locking_inset->insetAllowed(code);
1299         else if (owner())
1300                 ret = owner()->insetAllowed(code);
1301         in_insetAllowed = false;
1302         return ret;
1303 }
1304
1305
1306 UpdatableInset * InsetText::getLockingInset() const
1307 {
1308         return the_locking_inset ? the_locking_inset->getLockingInset() :
1309                 const_cast<InsetText *>(this);
1310 }
1311
1312
1313 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1314 {
1315         if (c == lyxCode())
1316                 return this;
1317         if (the_locking_inset)
1318                 return the_locking_inset->getFirstLockingInsetOfType(c);
1319         return 0;
1320 }
1321
1322
1323 bool InsetText::showInsetDialog(BufferView * bv) const
1324 {
1325         if (the_locking_inset)
1326                 return the_locking_inset->showInsetDialog(bv);
1327         return false;
1328 }
1329
1330
1331 void InsetText::getLabelList(Buffer const & buffer,
1332                              std::vector<string> & list) const
1333 {
1334         ParagraphList::const_iterator pit = paragraphs.begin();
1335         ParagraphList::const_iterator pend = paragraphs.end();
1336         for (; pit != pend; ++pit) {
1337                 InsetList::const_iterator beg = pit->insetlist.begin();
1338                 InsetList::const_iterator end = pit->insetlist.end();
1339                 for (; beg != end; ++beg)
1340                         beg->inset->getLabelList(buffer, list);
1341         }
1342 }
1343
1344
1345 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1346                         bool selectall)
1347 {
1348         if (the_locking_inset) {
1349                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1350                 return;
1351         }
1352
1353         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1354             || cpar()->empty()) {
1355                 text_.setFont(font, toggleall);
1356                 return;
1357         }
1358
1359
1360         if (text_.selection.set())
1361                 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1362
1363         if (selectall) {
1364                 text_.cursorTop();
1365                 text_.selection.cursor = text_.cursor;
1366                 text_.cursorBottom();
1367                 text_.setSelection();
1368         }
1369
1370         text_.toggleFree(font, toggleall);
1371
1372         if (selectall)
1373                 text_.clearSelection();
1374
1375         bv->fitCursor();
1376         updateLocal(bv, true);
1377 }
1378
1379
1380 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1381 {
1382         if (cpos() == cpar()->size())
1383                 return false;
1384         InsetOld * inset = cpar()->getInset(cpos());
1385         if (!isHighlyEditableInset(inset))
1386                 return false;
1387         FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1388         inset->localDispatch(cmd);
1389         if (!the_locking_inset)
1390                 return false;
1391         updateLocal(bv, false);
1392         return true;
1393 }
1394
1395
1396 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1397                                       mouse_button::state button)
1398 {
1399         int dummyx = x;
1400         int dummyy = y + dim_.asc;
1401         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1402         // we only do the edit() call if the inset was hit by the mouse
1403         // or if it is a highly editable inset. So we should call this
1404         // function from our own edit with button < 0.
1405         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1406         // WRONG
1407         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1408                 return false;
1409
1410         if (!inset)
1411                 return false;
1412         if (x < 0)
1413                 x = dim_.wid;
1414         if (y < 0)
1415                 y = dim_.des;
1416         inset_x = cx() - top_x;
1417         inset_y = cy();
1418         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1419         inset->localDispatch(cmd);
1420         if (!the_locking_inset)
1421                 return false;
1422         updateLocal(bv, false);
1423         return true;
1424 }
1425
1426
1427 void InsetText::markNew(bool track_changes)
1428 {
1429         ParagraphList::iterator pit = paragraphs.begin();
1430         ParagraphList::iterator end = paragraphs.end();
1431         for (; pit != end; ++pit) {
1432                 if (track_changes) {
1433                         pit->trackChanges();
1434                 } else {
1435                         // no-op when not tracking
1436                         pit->cleanChanges();
1437                 }
1438         }
1439 }
1440
1441
1442 void InsetText::setText(string const & data, LyXFont const & font)
1443 {
1444         clear(false);
1445         for (unsigned int i = 0; i < data.length(); ++i)
1446                 paragraphs.begin()->insertChar(i, data[i], font);
1447 }
1448
1449
1450 void InsetText::setAutoBreakRows(bool flag)
1451 {
1452         if (flag != autoBreakRows_) {
1453                 autoBreakRows_ = flag;
1454                 if (!flag)
1455                         removeNewlines();
1456         }
1457 }
1458
1459
1460 void InsetText::setDrawFrame(DrawFrame how)
1461 {
1462         drawFrame_ = how;
1463 }
1464
1465
1466 LColor_color InsetText::frameColor() const
1467 {
1468         return LColor::color(frame_color_);
1469 }
1470
1471
1472 void InsetText::setFrameColor(LColor_color col)
1473 {
1474         frame_color_ = col;
1475 }
1476
1477
1478 int InsetText::cx() const
1479 {
1480         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1481         if (the_locking_inset) {
1482                 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1483                 if (font.isVisibleRightToLeft())
1484                         x -= the_locking_inset->width();
1485         }
1486         return x;
1487 }
1488
1489
1490 int InsetText::cy() const
1491 {
1492         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1493 }
1494
1495
1496 pos_type InsetText::cpos() const
1497 {
1498         return text_.cursor.pos();
1499 }
1500
1501
1502 ParagraphList::iterator InsetText::cpar() const
1503 {
1504         return text_.cursor.par();
1505 }
1506
1507
1508 bool InsetText::cboundary() const
1509 {
1510         return text_.cursor.boundary();
1511 }
1512
1513
1514 RowList::iterator InsetText::crow() const
1515 {
1516         return text_.cursorRow();
1517 }
1518
1519
1520 LyXText * InsetText::getLyXText(BufferView const * bv,
1521                                 bool const recursive) const
1522 {
1523         setViewCache(bv);
1524         if (recursive && the_locking_inset)
1525                 return the_locking_inset->getLyXText(bv, true);
1526         return &text_;
1527 }
1528
1529
1530 void InsetText::setViewCache(BufferView const * bv) const
1531 {
1532         if (bv) {
1533                 if (bv != text_.bv_owner) {
1534                         //lyxerr << "setting view cache from "
1535                         //      << text_.bv_owner << " to " << bv << "\n";
1536                         text_.init(const_cast<BufferView *>(bv));
1537                 }
1538                 text_.bv_owner = const_cast<BufferView *>(bv);
1539         }
1540 }
1541
1542
1543 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1544 {
1545         if (recursive) {
1546                 /// then remove all LyXText in text-insets
1547                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1548                          const_cast<ParagraphList&>(paragraphs).end(),
1549                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1550         }
1551 }
1552
1553
1554 void InsetText::removeNewlines()
1555 {
1556         ParagraphList::iterator it = paragraphs.begin();
1557         ParagraphList::iterator end = paragraphs.end();
1558         for (; it != end; ++it)
1559                 for (int i = 0; i < it->size(); ++i)
1560                         if (it->isNewline(i))
1561                                 it->erase(i);
1562 }
1563
1564
1565 int InsetText::scroll(bool recursive) const
1566 {
1567         int sx = UpdatableInset::scroll(false);
1568
1569         if (recursive && the_locking_inset)
1570                 sx += the_locking_inset->scroll(recursive);
1571
1572         return sx;
1573 }
1574
1575
1576 void InsetText::clearSelection(BufferView *)
1577 {
1578         text_.clearSelection();
1579 }
1580
1581
1582 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1583 {
1584         Painter & pain = bv->painter();
1585         int w = dim_.wid;
1586         int h = dim_.asc + dim_.des;
1587         int ty = baseline - dim_.asc;
1588
1589         if (ty < 0) {
1590                 h += ty;
1591                 ty = 0;
1592         }
1593         if (ty + h > pain.paperHeight())
1594                 h = pain.paperHeight();
1595         if (top_x + w > pain.paperWidth())
1596                 w = pain.paperWidth();
1597         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1598 }
1599
1600
1601 ParagraphList * InsetText::getParagraphs(int i) const
1602 {
1603         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
1604 }
1605
1606
1607 LyXCursor const & InsetText::cursor(BufferView * bv) const
1608 {
1609         if (the_locking_inset)
1610                 return the_locking_inset->cursor(bv);
1611         return getLyXText(bv)->cursor;
1612 }
1613
1614
1615 InsetOld * InsetText::getInsetFromID(int id_arg) const
1616 {
1617         if (id_arg == id())
1618                 return const_cast<InsetText *>(this);
1619
1620         ParagraphList::const_iterator pit = paragraphs.begin();
1621         ParagraphList::const_iterator pend = paragraphs.end();
1622         for (; pit != pend; ++pit) {
1623                 InsetList::const_iterator it = pit->insetlist.begin();
1624                 InsetList::const_iterator end = pit->insetlist.end();
1625                 for (; it != end; ++it) {
1626                         if (it->inset->id() == id_arg)
1627                                 return it->inset;
1628                         InsetOld * in = it->inset->getInsetFromID(id_arg);
1629                         if (in)
1630                                 return in;
1631                 }
1632         }
1633         return 0;
1634 }
1635
1636
1637 WordLangTuple const
1638 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1639 {
1640         WordLangTuple word;
1641         if (the_locking_inset) {
1642                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1643                 if (!word.word().empty()) {
1644                         value += cy();
1645                         return word;
1646                 }
1647                 // we have to go on checking so move cursor to the next char
1648                 text_.cursor.pos(text_.cursor.pos() + 1);
1649         }
1650         word = text_.selectNextWordToSpellcheck(value);
1651         if (word.word().empty())
1652                 bv->unlockInset(const_cast<InsetText *>(this));
1653         else
1654                 value = cy();
1655         return word;
1656 }
1657
1658
1659 void InsetText::selectSelectedWord(BufferView * bv)
1660 {
1661         if (the_locking_inset) {
1662                 the_locking_inset->selectSelectedWord(bv);
1663                 return;
1664         }
1665         getLyXText(bv)->selectSelectedWord();
1666         updateLocal(bv, false);
1667 }
1668
1669
1670 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1671 {
1672         if (the_locking_inset) {
1673                 if (the_locking_inset->nextChange(bv, length))
1674                         return true;
1675                 text_.cursorRight(true);
1676         }
1677         lyx::find::SearchResult result =
1678                 lyx::find::findNextChange(bv, &text_, length);
1679
1680         if (result == lyx::find::SR_FOUND) {
1681                 LyXCursor cur = text_.cursor;
1682                 bv->unlockInset(bv->theLockingInset());
1683                 if (bv->lockInset(this))
1684                         locked = true;
1685                 text_.cursor = cur;
1686                 text_.setSelectionRange(length);
1687                 updateLocal(bv, false);
1688         }
1689         return result != lyx::find::SR_NOT_FOUND;
1690 }
1691
1692
1693 bool InsetText::searchForward(BufferView * bv, string const & str,
1694                               bool cs, bool mw)
1695 {
1696         if (the_locking_inset) {
1697                 if (the_locking_inset->searchForward(bv, str, cs, mw))
1698                         return true;
1699                 text_.cursorRight(true);
1700         }
1701         lyx::find::SearchResult result =
1702                 lyx::find::find(bv, &text_, str, true, cs, mw);
1703
1704         if (result == lyx::find::SR_FOUND) {
1705                 LyXCursor cur = text_.cursor;
1706                 bv->unlockInset(bv->theLockingInset());
1707                 if (bv->lockInset(this))
1708                         locked = true;
1709                 text_.cursor = cur;
1710                 text_.setSelectionRange(str.length());
1711                 updateLocal(bv, false);
1712         }
1713         return result != lyx::find::SR_NOT_FOUND;
1714 }
1715
1716
1717 bool InsetText::searchBackward(BufferView * bv, string const & str,
1718                                bool cs, bool mw)
1719 {
1720         if (the_locking_inset) {
1721                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1722                         return true;
1723         }
1724         if (!locked) {
1725                 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1726                 text_.setCursor(pit, pit->size());
1727         }
1728         lyx::find::SearchResult result =
1729                 lyx::find::find(bv, &text_, str, false, cs, mw);
1730
1731         if (result == lyx::find::SR_FOUND) {
1732                 LyXCursor cur = text_.cursor;
1733                 bv->unlockInset(bv->theLockingInset());
1734                 if (bv->lockInset(this))
1735                         locked = true;
1736                 text_.cursor = cur;
1737                 text_.setSelectionRange(str.length());
1738                 updateLocal(bv, false);
1739         }
1740         return result != lyx::find::SR_NOT_FOUND;
1741 }
1742
1743
1744 bool InsetText::checkInsertChar(LyXFont & font)
1745 {
1746         return owner() ? owner()->checkInsertChar(font) : true;
1747 }
1748
1749
1750 void InsetText::collapseParagraphs(BufferView * bv)
1751 {
1752         while (paragraphs.size() > 1) {
1753                 ParagraphList::iterator first_par = paragraphs.begin();
1754                 ParagraphList::iterator next_par = boost::next(first_par);
1755                 size_t const first_par_size = first_par->size();
1756
1757                 if (!first_par->empty() &&
1758                     !next_par->empty() &&
1759                     !first_par->isSeparator(first_par_size - 1)) {
1760                         first_par->insertChar(first_par_size, ' ');
1761                 }
1762
1763                 if (text_.selection.set()) {
1764                         if (text_.selection.start.par() == next_par) {
1765                                 text_.selection.start.par(first_par);
1766                                 text_.selection.start.pos(
1767                                         text_.selection.start.pos() + first_par_size);
1768                         }
1769                         if (text_.selection.end.par() == next_par) {
1770                                 text_.selection.end.par(first_par);
1771                                 text_.selection.end.pos(
1772                                         text_.selection.end.pos() + first_par_size);
1773                         }
1774                 }
1775
1776                 mergeParagraph(bv->buffer()->params(), paragraphs, first_par);
1777         }
1778 }
1779
1780
1781 void InsetText::getDrawFont(LyXFont & font) const
1782 {
1783         if (!owner())
1784                 return;
1785         owner()->getDrawFont(font);
1786 }
1787
1788
1789 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1790 {
1791 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1792 // And it probably does. You have to take a look at this John. (Lgb)
1793 #warning John, have a look here. (Lgb)
1794         ParagraphList::iterator pit = plist.begin();
1795         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1796         ++pit;
1797         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1798
1799         ParagraphList::iterator pend = plist.end();
1800         for (; pit != pend; ++pit)
1801                 paragraphs.push_back(*pit);
1802 }
1803
1804
1805 void InsetText::addPreview(PreviewLoader & loader) const
1806 {
1807         ParagraphList::const_iterator pit = paragraphs.begin();
1808         ParagraphList::const_iterator pend = paragraphs.end();
1809
1810         for (; pit != pend; ++pit) {
1811                 InsetList::const_iterator it  = pit->insetlist.begin();
1812                 InsetList::const_iterator end = pit->insetlist.end();
1813                 for (; it != end; ++it)
1814                         it->inset->addPreview(loader);
1815         }
1816 }