]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
the dispatch patch
[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.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 = -1;
115         inset_pos = 0;
116         inset_x = 0;
117         inset_y = 0;
118         no_selection = true;
119         the_locking_inset = 0;
120         old_par = -1;
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
274                   && text_.cursor.par() == inset_par && cpos() == inset_pos) {
275                 inset_x = cx() - x;
276                 inset_y = cy();
277         }
278
279         x += TEXT_TO_INSET_OFFSET;
280
281         paintTextInset(*bv, text_, x, y);
282
283         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
284                 drawFrame(pain, start_x);
285 }
286
287
288 void InsetText::drawFrame(Painter & pain, int x) const
289 {
290         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
291         int const frame_x = x + ttoD2;
292         int const frame_y = top_baseline - dim_.asc + ttoD2;
293         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
294         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
295         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
296 }
297
298
299 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
300 {
301         if (!bv)
302                 return;
303
304         if (!autoBreakRows_ && paragraphs.size() > 1)
305                 collapseParagraphs(bv);
306
307         if (!text_.selection.set())
308                 text_.selection.cursor = text_.cursor;
309
310         bv->fitCursor();
311         bv->updateInset(this);
312         bv->owner()->view_state_changed();
313         bv->owner()->updateMenubar();
314         bv->owner()->updateToolbar();
315         if (old_par != text_.cursor.par()) {
316                 bv->owner()->setLayout(cpar()->layout()->name());
317                 old_par = text_.cursor.par();
318         }
319 }
320
321
322 string const InsetText::editMessage() const
323 {
324         return _("Opened Text Inset");
325 }
326
327
328 void InsetText::insetUnlock(BufferView * bv)
329 {
330         if (the_locking_inset) {
331                 the_locking_inset->insetUnlock(bv);
332                 the_locking_inset = 0;
333                 updateLocal(bv, false);
334         }
335         no_selection = true;
336         locked = false;
337
338         if (text_.selection.set())
339                 text_.clearSelection();
340         else if (owner())
341                 bv->owner()->setLayout(owner()->getLyXText(bv)
342                                        ->cursorPar()->layout()->name());
343         else
344                 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
345         // hack for deleteEmptyParMech
346         if (!paragraphs.begin()->empty())
347                 text_.setCursor(0, 0);
348         else if (paragraphs.size() > 1)
349                 text_.setCursor(1, 0);
350 }
351
352
353 void InsetText::lockInset(BufferView * bv)
354 {
355         locked = true;
356         the_locking_inset = 0;
357         inset_pos = inset_x = inset_y = 0;
358         inset_boundary = false;
359         inset_par =  -1;
360         old_par = -1;
361         text_.setCursorIntern(0, 0);
362         text_.clearSelection();
363         finishUndo();
364         // If the inset is empty set the language of the current font to the
365         // language to the surronding text (if different).
366         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
367                 bv->getParentLanguage(this) != text_.current_font.language()) {
368                 LyXFont font(LyXFont::ALL_IGNORE);
369                 font.setLanguage(bv->getParentLanguage(this));
370                 setFont(bv, font, false);
371         }
372 }
373
374
375 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
376 {
377         the_locking_inset = inset;
378         inset_x = cx() - top_x;
379         inset_y = cy();
380         inset_pos = cpos();
381         inset_par = text_.cursor.par();
382         inset_boundary = cboundary();
383 }
384
385
386 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
387 {
388         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
389                               << inset << "): " << endl;
390         if (!inset)
391                 return false;
392         if (!the_locking_inset) {
393                 ParagraphList::iterator pit = paragraphs.begin();
394                 ParagraphList::iterator pend = paragraphs.end();
395
396                 for (; pit != pend; ++pit) {
397                         InsetList::iterator it = pit->insetlist.begin();
398                         InsetList::iterator const end = pit->insetlist.end();
399                         for (; it != end; ++it) {
400                                 if (it->inset == inset) {
401                                         lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
402                                         text_.setCursorIntern(
403                                                 std::distance(paragraphs.begin(), 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 (text_.cursor.par() == 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 = -1; // 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->dispatch(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->dispatch(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                         dispatch(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 = text_.cursor.par();
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                                 dispatch(FuncRequest(bv, LFUN_PASTE));
532                         else
533                                 dispatch(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->dispatch(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         if (!inset)
556                 return false;
557
558         // We still need to deal properly with the whole relative vs.
559         // absolute mouse co-ords thing in a realiable, sensible way
560         bool ret = inset->dispatch(cmd1);
561         updateLocal(bv, false);
562         return ret;
563 }
564
565
566 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
567 {
568         FuncRequest cmd1 = cmd;
569         cmd1.x -= inset_x;
570         cmd1.y -= inset_y;
571
572         if (the_locking_inset) {
573                 the_locking_inset->dispatch(cmd1);
574                 return;
575         }
576
577         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
578                 return;
579
580         BufferView * bv = cmd.view();
581         LyXCursor cur = text_.cursor;
582         text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
583         text_.cursor.x_fix(text_.cursor.x());
584         if (cur == text_.cursor)
585                 return;
586         text_.setSelection();
587         updateLocal(bv, false);
588 }
589
590
591 dispatch_result
592 InsetText::priv_dispatch(FuncRequest const & cmd,
593                          idx_type & idx, pos_type & pos)
594 {
595         BufferView * bv = cmd.view();
596         setViewCache(bv);
597
598         switch (cmd.action) {
599         case LFUN_INSET_EDIT: {
600                 UpdatableInset::priv_dispatch(cmd, idx, pos);
601
602                 if (!bv->lockInset(this)) {
603                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
604                         return DISPATCHED;
605                 }
606
607                 locked = true;
608                 the_locking_inset = 0;
609                 inset_pos = 0;
610                 inset_x = 0;
611                 inset_y = 0;
612                 inset_boundary = false;
613                 inset_par = -1;
614                 old_par = -1;
615
616
617                 if (cmd.argument.size()) {
618                         if (cmd.argument == "left")
619                                 text_.setCursorIntern(0, 0);
620                         else
621                                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
622                 } else {
623                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
624                         // we put here -1 and not button as now the button in the
625                         // edit call should not be needed we will fix this in 1.3.x
626                         // cycle hopefully (Jug 20020509)
627                         // FIXME: GUII I've changed this to none: probably WRONG
628                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
629                                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
630                                 text_.cursor.x(text_.cursor.x());
631                                 text_.cursor.x_fix(text_.cursor.x());
632                         }
633                 }
634
635                 text_.clearSelection();
636                 finishUndo();
637
638                 // If the inset is empty set the language of the current font to the
639                 // language to the surronding text (if different).
640                 if (paragraphs.begin()->empty() &&
641                     paragraphs.size() == 1 &&
642                     bv->getParentLanguage(this) != text_.current_font.language())
643                 {
644                         LyXFont font(LyXFont::ALL_IGNORE);
645                         font.setLanguage(bv->getParentLanguage(this));
646                         setFont(bv, font, false);
647                 }
648
649                 updateLocal(bv, false);
650                 // Tell the paragraph dialog that we've entered an insettext.
651                 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
652                 return DISPATCHED;
653         }
654
655         case LFUN_MOUSE_PRESS:
656                 lfunMousePress(cmd);
657                 return DISPATCHED;
658
659         case LFUN_MOUSE_MOTION:
660                 lfunMouseMotion(cmd);
661                 return DISPATCHED;
662
663         case LFUN_MOUSE_RELEASE:
664                 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
665
666         default:
667                 break;
668         }
669
670         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
671         no_selection = false;
672
673         dispatch_result result = UpdatableInset::priv_dispatch(cmd, idx, pos);
674         if (result != UNDISPATCHED)
675                 return DISPATCHED;
676
677         result = DISPATCHED;
678         if (cmd.action < 0 && cmd.argument.empty())
679                 return FINISHED;
680
681         if (the_locking_inset) {
682                 result = the_locking_inset->dispatch(cmd);
683                 if (result == DISPATCHED_NOUPDATE)
684                         return result;
685                 if (result == DISPATCHED) {
686                         updateLocal(bv, false);
687                         return result;
688                 }
689                 if (result >= FINISHED) {
690                         switch (result) {
691                         case FINISHED_RIGHT:
692                                 moveRightIntern(bv, false, false);
693                                 result = DISPATCHED;
694                                 break;
695                         case FINISHED_UP:
696                                 result = moveUp(bv);
697                                 if (result >= FINISHED) {
698                                         updateLocal(bv, false);
699                                         bv->unlockInset(this);
700                                 }
701                                 break;
702                         case FINISHED_DOWN:
703                                 result = moveDown(bv);
704                                 if (result >= FINISHED) {
705                                         updateLocal(bv, false);
706                                         bv->unlockInset(this);
707                                 }
708                                 break;
709                         default:
710                                 result = DISPATCHED;
711                                 break;
712                         }
713                         the_locking_inset = 0;
714                         updateLocal(bv, false);
715                         // make sure status gets reset immediately
716                         bv->owner()->clearMessage();
717                         return result;
718                 }
719         }
720         bool updflag = false;
721
722         switch (cmd.action) {
723
724         // Normal chars
725         case LFUN_SELFINSERT:
726                 if (bv->buffer()->isReadonly()) {
727 //          setErrorMessage(N_("Document is read only"));
728                         break;
729                 }
730                 if (!cmd.argument.empty()) {
731                         /* Automatically delete the currently selected
732                          * text and replace it with what is being
733                          * typed in now. Depends on lyxrc settings
734                          * "auto_region_delete", which defaults to
735                          * true (on). */
736 #if 0
737                         // This should not be needed here and is also WRONG!
738                         recordUndo(bv, Undo::INSERT, text_.cursorPar());
739 #endif
740                         bv->switchKeyMap();
741
742                         if (lyxrc.auto_region_delete && text_.selection.set())
743                                 text_.cutSelection(false, false);
744                         text_.clearSelection();
745
746                         for (string::size_type i = 0; i < cmd.argument.length(); ++i)
747                                 bv->owner()->getIntl().getTransManager().
748                                         TranslateAndInsert(cmd.argument[i], &text_);
749                 }
750                 text_.selection.cursor = text_.cursor;
751                 updflag = true;
752                 result = DISPATCHED_NOUPDATE;
753                 break;
754
755         // cursor movements that need special handling
756
757         case LFUN_RIGHT:
758                 result = moveRight(bv);
759                 finishUndo();
760                 break;
761         case LFUN_LEFT:
762                 finishUndo();
763                 result = moveLeft(bv);
764                 break;
765         case LFUN_DOWN:
766                 finishUndo();
767                 result = moveDown(bv);
768                 break;
769         case LFUN_UP:
770                 finishUndo();
771                 result = moveUp(bv);
772                 break;
773
774         case LFUN_PRIOR:
775                 if (crow() == text_.firstRow())
776                         result = FINISHED_UP;
777                 else {
778                         text_.cursorPrevious();
779                         text_.clearSelection();
780                         result = DISPATCHED_NOUPDATE;
781                 }
782                 break;
783
784         case LFUN_NEXT:
785                 if (crow() == text_.lastRow())
786                         result = FINISHED_DOWN;
787                 else {
788                         text_.cursorNext();
789                         text_.clearSelection();
790                         result = DISPATCHED_NOUPDATE;
791                 }
792                 break;
793
794         case LFUN_BACKSPACE:
795                 if (text_.selection.set())
796                         text_.cutSelection(true, false);
797                 else
798                         text_.backspace();
799                 updflag = true;
800                 break;
801
802         case LFUN_DELETE:
803                 if (text_.selection.set())
804                         text_.cutSelection(true, false);
805                 else
806                         text_.Delete();
807                 updflag = true;
808                 break;
809
810         case LFUN_PASTE:
811                 if (!autoBreakRows_) {
812                         if (CutAndPaste::nrOfParagraphs() > 1) {
813 #ifdef WITH_WARNINGS
814 #warning FIXME horrendously bad UI
815 #endif
816                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
817                         }
818                 } else {
819                         replaceSelection(bv->getLyXText());
820                         size_t sel_index = 0;
821                         string const & arg = cmd.argument;
822                         if (isStrUnsignedInt(arg)) {
823 #warning FIXME Check if the arg is in the domain of available selections.
824                                 sel_index = strToUnsignedInt(arg);
825                         }
826                         text_.pasteSelection(sel_index);
827                         // bug 393
828                         text_.clearSelection();
829                         updflag = true;
830                 }
831                 break;
832
833         case LFUN_BREAKPARAGRAPH:
834                 if (!autoBreakRows_) {
835                         result = DISPATCHED;
836                 } else {
837                         replaceSelection(bv->getLyXText());
838                         text_.breakParagraph(paragraphs, 0);
839                         updflag = true;
840                 }
841                 break;
842
843         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
844                 if (!autoBreakRows_) {
845                         result = DISPATCHED;
846                 } else {
847                         replaceSelection(bv->getLyXText());
848                         text_.breakParagraph(paragraphs, 1);
849                         updflag = true;
850                 }
851                 break;
852
853         case LFUN_BREAKLINE: {
854                 if (!autoBreakRows_) {
855                         result = DISPATCHED;
856                 } else {
857                         replaceSelection(bv->getLyXText());
858                         text_.insertInset(new InsetNewline);
859                         updflag = true;
860                 }
861                 break;
862         }
863
864         case LFUN_LAYOUT:
865                 // do not set layouts on non breakable textinsets
866                 if (autoBreakRows_) {
867                         string cur_layout = cpar()->layout()->name();
868
869                         // Derive layout number from given argument (string)
870                         // and current buffer's textclass (number).
871                         LyXTextClass const & tclass =
872                                 bv->buffer()->params().getLyXTextClass();
873                         string layout = cmd.argument;
874                         bool hasLayout = tclass.hasLayout(layout);
875
876                         // If the entry is obsolete, use the new one instead.
877                         if (hasLayout) {
878                                 string const & obs = tclass[layout]->obsoleted_by();
879                                 if (!obs.empty())
880                                         layout = obs;
881                         }
882
883                         // see if we found the layout number:
884                         if (!hasLayout) {
885                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
886                                 bv->owner()->dispatch(lf);
887                                 break;
888                         }
889
890                         if (cur_layout != layout) {
891                                 cur_layout = layout;
892                                 text_.setLayout(layout);
893                                 bv->owner()->setLayout(cpar()->layout()->name());
894                                 updflag = true;
895                         }
896                 } else {
897                         // reset the layout box
898                         bv->owner()->setLayout(cpar()->layout()->name());
899                 }
900                 break;
901
902         default:
903                 if (!bv->dispatch(cmd))
904                         result = UNDISPATCHED;
905                 break;
906         }
907
908         updateLocal(bv, updflag);
909
910         /// If the action has deleted all text in the inset, we need to change the
911         // language to the language of the surronding text.
912         if (!was_empty && paragraphs.begin()->empty() &&
913             paragraphs.size() == 1) {
914                 LyXFont font(LyXFont::ALL_IGNORE);
915                 font.setLanguage(bv->getParentLanguage(this));
916                 setFont(bv, font, false);
917         }
918
919         if (result >= FINISHED)
920                 bv->unlockInset(this);
921
922         if (result == DISPATCHED_NOUPDATE)
923                 result = DISPATCHED;
924         return result;
925 }
926
927
928 int InsetText::latex(Buffer const & buf, ostream & os,
929                      LatexRunParams const & runparams) const
930 {
931         TexRow texrow;
932         latexParagraphs(buf, paragraphs, os, texrow, runparams);
933         return texrow.rows();
934 }
935
936
937 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
938 {
939         unsigned int lines = 0;
940
941         ParagraphList::const_iterator beg = paragraphs.begin();
942         ParagraphList::const_iterator end = paragraphs.end();
943         ParagraphList::const_iterator it = beg;
944         for (; it != end; ++it) {
945                 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
946                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
947                 os << tmp;
948         }
949         return lines;
950 }
951
952
953 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
954 {
955         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
956         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
957
958         // There is a confusion between the empty paragraph and the default paragraph
959         // The default paragraph is <p></p>, the empty paragraph is *empty*
960         // Since none of the floats of linuxdoc accepts standard paragraphs
961         // I disable them. I don't expect problems. (jamatos 2003/07/27)
962         for (; pit != pend; ++pit) {
963                 const string name = pit->layout()->latexname();
964                 if (name != "p")
965                         sgml::openTag(os, 1, 0, name);
966                 buf.simpleLinuxDocOnePar(os, pit, 0);
967                 if (name != "p")
968                         sgml::closeTag(os, 1, 0, name);
969         }
970         return 0;
971 }
972
973
974 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
975 {
976         unsigned int lines = 0;
977
978         vector<string> environment_stack(10);
979         vector<string> environment_inner(10);
980
981         int const command_depth = 0;
982         string item_name;
983
984         Paragraph::depth_type depth = 0; // paragraph depth
985
986         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
987         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
988
989         for (; pit != pend; ++pit) {
990                 int desc_on = 0; // description mode
991
992                 LyXLayout_ptr const & style = pit->layout();
993
994                 // environment tag closing
995                 for (; depth > pit->params().depth(); --depth) {
996                         if (environment_inner[depth] != "!-- --") {
997                                 item_name = "listitem";
998                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
999                                 if (environment_inner[depth] == "varlistentry")
1000                                         lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1001                         }
1002                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1003                         environment_stack[depth].erase();
1004                         environment_inner[depth].erase();
1005                 }
1006
1007                 if (depth == pit->params().depth()
1008                    && environment_stack[depth] != style->latexname()
1009                    && !environment_stack[depth].empty()) {
1010                         if (environment_inner[depth] != "!-- --") {
1011                                 item_name= "listitem";
1012                                 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1013                                 if (environment_inner[depth] == "varlistentry")
1014                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1015                         }
1016
1017                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1018
1019                         environment_stack[depth].erase();
1020                         environment_inner[depth].erase();
1021                 }
1022
1023                 // Write opening SGML tags.
1024                 switch (style->latextype) {
1025                 case LATEX_PARAGRAPH:
1026                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1027                         break;
1028
1029                 case LATEX_COMMAND:
1030                         buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1031                         return -1;
1032                         break;
1033
1034                 case LATEX_ENVIRONMENT:
1035                 case LATEX_ITEM_ENVIRONMENT:
1036                         if (depth < pit->params().depth()) {
1037                                 depth = pit->params().depth();
1038                                 environment_stack[depth].erase();
1039                         }
1040
1041                         if (environment_stack[depth] != style->latexname()) {
1042                                 if (environment_stack.size() == depth + 1) {
1043                                         environment_stack.push_back("!-- --");
1044                                         environment_inner.push_back("!-- --");
1045                                 }
1046                                 environment_stack[depth] = style->latexname();
1047                                 environment_inner[depth] = "!-- --";
1048                                 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1049                         } else {
1050                                 if (environment_inner[depth] != "!-- --") {
1051                                         item_name= "listitem";
1052                                         lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1053                                         if (environment_inner[depth] == "varlistentry")
1054                                                 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1055                                 }
1056                         }
1057
1058                         if (style->latextype == LATEX_ENVIRONMENT) {
1059                                 if (!style->latexparam().empty()) {
1060                                         if (style->latexparam() == "CDATA")
1061                                                 os << "<![CDATA[";
1062                                         else
1063                                           lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1064                                 }
1065                                 break;
1066                         }
1067
1068                         desc_on = (style->labeltype == LABEL_MANUAL);
1069
1070                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1071                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1072
1073                         item_name = desc_on ? "term" : "para";
1074                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1075
1076                         break;
1077                 default:
1078                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1079                         break;
1080                 }
1081
1082                 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1083
1084                 string end_tag;
1085                 // write closing SGML tags
1086                 switch (style->latextype) {
1087                 case LATEX_ENVIRONMENT:
1088                         if (!style->latexparam().empty()) {
1089                                 if (style->latexparam() == "CDATA")
1090                                         os << "]]>";
1091                                 else
1092                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1093                         }
1094                         break;
1095                 case LATEX_ITEM_ENVIRONMENT:
1096                         if (desc_on == 1)
1097                                 break;
1098                         end_tag= "para";
1099                         lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1100                         break;
1101                 case LATEX_PARAGRAPH:
1102                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1103                         break;
1104                 default:
1105                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1106                         break;
1107                 }
1108         }
1109
1110         // Close open tags
1111         for (int d = depth; d >= 0; --d) {
1112                 if (!environment_stack[depth].empty()) {
1113                         if (environment_inner[depth] != "!-- --") {
1114                                 item_name = "listitem";
1115                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1116                                if (environment_inner[depth] == "varlistentry")
1117                                        lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1118                         }
1119
1120                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1121                 }
1122         }
1123
1124         return lines;
1125 }
1126
1127
1128 void InsetText::validate(LaTeXFeatures & features) const
1129 {
1130         for_each(paragraphs.begin(), paragraphs.end(),
1131                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1132 }
1133
1134
1135 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1136 {
1137         if (the_locking_inset) {
1138                 the_locking_inset->getCursor(bv, x, y);
1139                 return;
1140         }
1141         x = cx();
1142         y = cy() + InsetText::y();
1143 }
1144
1145
1146 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1147 {
1148         if (the_locking_inset) {
1149                 the_locking_inset->getCursorPos(bv, x, y);
1150                 return;
1151         }
1152         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1153         y = cy() - TEXT_TO_INSET_OFFSET;
1154 }
1155
1156
1157 int InsetText::insetInInsetY() const
1158 {
1159         if (!the_locking_inset)
1160                 return 0;
1161
1162         return inset_y + the_locking_inset->insetInInsetY();
1163 }
1164
1165
1166 void InsetText::fitInsetCursor(BufferView * bv) const
1167 {
1168         if (the_locking_inset) {
1169                 the_locking_inset->fitInsetCursor(bv);
1170                 return;
1171         }
1172
1173         LyXFont const font = text_.getFont(cpar(), cpos());
1174
1175         int const asc = font_metrics::maxAscent(font);
1176         int const desc = font_metrics::maxDescent(font);
1177
1178         bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1179 }
1180
1181
1182 dispatch_result InsetText::moveRight(BufferView * bv)
1183 {
1184         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1185                 return moveLeftIntern(bv, false, true, false);
1186         else
1187                 return moveRightIntern(bv, true, true, false);
1188 }
1189
1190
1191 dispatch_result InsetText::moveLeft(BufferView * bv)
1192 {
1193         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1194                 return moveRightIntern(bv, true, true, false);
1195         else
1196                 return moveLeftIntern(bv, false, true, false);
1197 }
1198
1199
1200 dispatch_result
1201 InsetText::moveRightIntern(BufferView * bv, bool front,
1202                            bool activate_inset, bool selecting)
1203 {
1204         ParagraphList::iterator c_par = cpar();
1205
1206         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1207                 return FINISHED_RIGHT;
1208         if (activate_inset && checkAndActivateInset(bv, front))
1209                 return DISPATCHED;
1210         text_.cursorRight(bv);
1211         if (!selecting)
1212                 text_.clearSelection();
1213         return DISPATCHED_NOUPDATE;
1214 }
1215
1216
1217 dispatch_result
1218 InsetText::moveLeftIntern(BufferView * bv, bool front,
1219                           bool activate_inset, bool selecting)
1220 {
1221         if (cpar() == paragraphs.begin() && cpos() <= 0)
1222                 return FINISHED;
1223         text_.cursorLeft(bv);
1224         if (!selecting)
1225                 text_.clearSelection();
1226         if (activate_inset && checkAndActivateInset(bv, front))
1227                 return DISPATCHED;
1228         return DISPATCHED_NOUPDATE;
1229 }
1230
1231
1232 dispatch_result InsetText::moveUp(BufferView * bv)
1233 {
1234         if (crow() == text_.firstRow())
1235                 return FINISHED_UP;
1236         text_.cursorUp(bv);
1237         text_.clearSelection();
1238         return DISPATCHED_NOUPDATE;
1239 }
1240
1241
1242 dispatch_result InsetText::moveDown(BufferView * bv)
1243 {
1244         if (crow() == text_.lastRow())
1245                 return FINISHED_DOWN;
1246         text_.cursorDown(bv);
1247         text_.clearSelection();
1248         return DISPATCHED_NOUPDATE;
1249 }
1250
1251
1252 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1253 {
1254         if (the_locking_inset) {
1255                 if (the_locking_inset->insetAllowed(inset))
1256                         return the_locking_inset->insertInset(bv, inset);
1257                 return false;
1258         }
1259         inset->setOwner(this);
1260         text_.insertInset(inset);
1261         bv->fitCursor();
1262         updateLocal(bv, true);
1263         return true;
1264 }
1265
1266
1267 bool InsetText::insetAllowed(InsetOld::Code code) const
1268 {
1269         // in_insetAllowed is a really gross hack,
1270         // to allow us to call the owner's insetAllowed
1271         // without stack overflow, which can happen
1272         // when the owner uses InsetCollapsable::insetAllowed()
1273         bool ret = true;
1274         if (in_insetAllowed)
1275                 return ret;
1276         in_insetAllowed = true;
1277         if (the_locking_inset)
1278                 ret = the_locking_inset->insetAllowed(code);
1279         else if (owner())
1280                 ret = owner()->insetAllowed(code);
1281         in_insetAllowed = false;
1282         return ret;
1283 }
1284
1285
1286 UpdatableInset * InsetText::getLockingInset() const
1287 {
1288         return the_locking_inset ? the_locking_inset->getLockingInset() :
1289                 const_cast<InsetText *>(this);
1290 }
1291
1292
1293 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1294 {
1295         if (c == lyxCode())
1296                 return this;
1297         if (the_locking_inset)
1298                 return the_locking_inset->getFirstLockingInsetOfType(c);
1299         return 0;
1300 }
1301
1302
1303 bool InsetText::showInsetDialog(BufferView * bv) const
1304 {
1305         if (the_locking_inset)
1306                 return the_locking_inset->showInsetDialog(bv);
1307         return false;
1308 }
1309
1310
1311 void InsetText::getLabelList(Buffer const & buffer,
1312                              std::vector<string> & list) const
1313 {
1314         ParagraphList::const_iterator pit = paragraphs.begin();
1315         ParagraphList::const_iterator pend = paragraphs.end();
1316         for (; pit != pend; ++pit) {
1317                 InsetList::const_iterator beg = pit->insetlist.begin();
1318                 InsetList::const_iterator end = pit->insetlist.end();
1319                 for (; beg != end; ++beg)
1320                         beg->inset->getLabelList(buffer, list);
1321         }
1322 }
1323
1324
1325 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1326                         bool selectall)
1327 {
1328         if (the_locking_inset) {
1329                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1330                 return;
1331         }
1332
1333         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1334             || cpar()->empty()) {
1335                 text_.setFont(font, toggleall);
1336                 return;
1337         }
1338
1339
1340         if (text_.selection.set())
1341                 text_.recUndo(text_.cursor.par());
1342
1343         if (selectall) {
1344                 text_.cursorTop();
1345                 text_.selection.cursor = text_.cursor;
1346                 text_.cursorBottom();
1347                 text_.setSelection();
1348         }
1349
1350         text_.toggleFree(font, toggleall);
1351
1352         if (selectall)
1353                 text_.clearSelection();
1354
1355         bv->fitCursor();
1356         updateLocal(bv, true);
1357 }
1358
1359
1360 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1361 {
1362         if (cpos() == cpar()->size())
1363                 return false;
1364         InsetOld * inset = cpar()->getInset(cpos());
1365         if (!isHighlyEditableInset(inset))
1366                 return false;
1367         FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1368         inset->dispatch(cmd);
1369         if (!the_locking_inset)
1370                 return false;
1371         updateLocal(bv, false);
1372         return true;
1373 }
1374
1375
1376 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1377                                       mouse_button::state button)
1378 {
1379         int dummyx = x;
1380         int dummyy = y + dim_.asc;
1381         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1382         // we only do the edit() call if the inset was hit by the mouse
1383         // or if it is a highly editable inset. So we should call this
1384         // function from our own edit with button < 0.
1385         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1386         // WRONG
1387         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1388                 return false;
1389
1390         if (!inset)
1391                 return false;
1392         if (x < 0)
1393                 x = dim_.wid;
1394         if (y < 0)
1395                 y = dim_.des;
1396         inset_x = cx() - top_x;
1397         inset_y = cy();
1398         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1399         inset->dispatch(cmd);
1400         if (!the_locking_inset)
1401                 return false;
1402         updateLocal(bv, false);
1403         return true;
1404 }
1405
1406
1407 void InsetText::markNew(bool track_changes)
1408 {
1409         ParagraphList::iterator pit = paragraphs.begin();
1410         ParagraphList::iterator end = paragraphs.end();
1411         for (; pit != end; ++pit) {
1412                 if (track_changes) {
1413                         pit->trackChanges();
1414                 } else {
1415                         // no-op when not tracking
1416                         pit->cleanChanges();
1417                 }
1418         }
1419 }
1420
1421
1422 void InsetText::setText(string const & data, LyXFont const & font)
1423 {
1424         clear(false);
1425         for (unsigned int i = 0; i < data.length(); ++i)
1426                 paragraphs.begin()->insertChar(i, data[i], font);
1427 }
1428
1429
1430 void InsetText::setAutoBreakRows(bool flag)
1431 {
1432         if (flag != autoBreakRows_) {
1433                 autoBreakRows_ = flag;
1434                 if (!flag)
1435                         removeNewlines();
1436         }
1437 }
1438
1439
1440 void InsetText::setDrawFrame(DrawFrame how)
1441 {
1442         drawFrame_ = how;
1443 }
1444
1445
1446 LColor_color InsetText::frameColor() const
1447 {
1448         return LColor::color(frame_color_);
1449 }
1450
1451
1452 void InsetText::setFrameColor(LColor_color col)
1453 {
1454         frame_color_ = col;
1455 }
1456
1457
1458 int InsetText::cx() const
1459 {
1460         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1461         if (the_locking_inset) {
1462                 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1463                 if (font.isVisibleRightToLeft())
1464                         x -= the_locking_inset->width();
1465         }
1466         return x;
1467 }
1468
1469
1470 int InsetText::cy() const
1471 {
1472         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1473 }
1474
1475
1476 pos_type InsetText::cpos() const
1477 {
1478         return text_.cursor.pos();
1479 }
1480
1481
1482 ParagraphList::iterator InsetText::cpar() const
1483 {
1484         return text_.cursorPar();
1485 }
1486
1487
1488 bool InsetText::cboundary() const
1489 {
1490         return text_.cursor.boundary();
1491 }
1492
1493
1494 RowList::iterator InsetText::crow() const
1495 {
1496         return text_.cursorRow();
1497 }
1498
1499
1500 LyXText * InsetText::getLyXText(BufferView const * bv,
1501                                 bool const recursive) const
1502 {
1503         setViewCache(bv);
1504         if (recursive && the_locking_inset)
1505                 return the_locking_inset->getLyXText(bv, true);
1506         return &text_;
1507 }
1508
1509
1510 void InsetText::setViewCache(BufferView const * bv) const
1511 {
1512         if (bv) {
1513                 if (bv != text_.bv_owner) {
1514                         //lyxerr << "setting view cache from "
1515                         //      << text_.bv_owner << " to " << bv << "\n";
1516                         text_.init(const_cast<BufferView *>(bv));
1517                 }
1518                 text_.bv_owner = const_cast<BufferView *>(bv);
1519         }
1520 }
1521
1522
1523 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1524 {
1525         if (recursive) {
1526                 /// then remove all LyXText in text-insets
1527                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1528                          const_cast<ParagraphList&>(paragraphs).end(),
1529                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1530         }
1531 }
1532
1533
1534 void InsetText::removeNewlines()
1535 {
1536         ParagraphList::iterator it = paragraphs.begin();
1537         ParagraphList::iterator end = paragraphs.end();
1538         for (; it != end; ++it)
1539                 for (int i = 0; i < it->size(); ++i)
1540                         if (it->isNewline(i))
1541                                 it->erase(i);
1542 }
1543
1544
1545 int InsetText::scroll(bool recursive) const
1546 {
1547         int sx = UpdatableInset::scroll(false);
1548
1549         if (recursive && the_locking_inset)
1550                 sx += the_locking_inset->scroll(recursive);
1551
1552         return sx;
1553 }
1554
1555
1556 void InsetText::clearSelection(BufferView *)
1557 {
1558         text_.clearSelection();
1559 }
1560
1561
1562 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1563 {
1564         Painter & pain = bv->painter();
1565         int w = dim_.wid;
1566         int h = dim_.asc + dim_.des;
1567         int ty = baseline - dim_.asc;
1568
1569         if (ty < 0) {
1570                 h += ty;
1571                 ty = 0;
1572         }
1573         if (ty + h > pain.paperHeight())
1574                 h = pain.paperHeight();
1575         if (top_x + w > pain.paperWidth())
1576                 w = pain.paperWidth();
1577         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1578 }
1579
1580
1581 ParagraphList * InsetText::getParagraphs(int i) const
1582 {
1583         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
1584 }
1585
1586
1587 LyXText * InsetText::getText(int i) const
1588 {
1589         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1590 }
1591
1592
1593 LyXCursor const & InsetText::cursor(BufferView * bv) const
1594 {
1595         if (the_locking_inset)
1596                 return the_locking_inset->cursor(bv);
1597         return getLyXText(bv)->cursor;
1598 }
1599
1600
1601 InsetOld * InsetText::getInsetFromID(int id_arg) const
1602 {
1603         if (id_arg == id())
1604                 return const_cast<InsetText *>(this);
1605
1606         ParagraphList::const_iterator pit = paragraphs.begin();
1607         ParagraphList::const_iterator pend = paragraphs.end();
1608         for (; pit != pend; ++pit) {
1609                 InsetList::const_iterator it = pit->insetlist.begin();
1610                 InsetList::const_iterator end = pit->insetlist.end();
1611                 for (; it != end; ++it) {
1612                         if (it->inset->id() == id_arg)
1613                                 return it->inset;
1614                         InsetOld * in = it->inset->getInsetFromID(id_arg);
1615                         if (in)
1616                                 return in;
1617                 }
1618         }
1619         return 0;
1620 }
1621
1622
1623 WordLangTuple const
1624 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1625 {
1626         WordLangTuple word;
1627         if (the_locking_inset) {
1628                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1629                 if (!word.word().empty()) {
1630                         value += cy();
1631                         return word;
1632                 }
1633                 // we have to go on checking so move cursor to the next char
1634                 text_.cursor.pos(text_.cursor.pos() + 1);
1635         }
1636         word = text_.selectNextWordToSpellcheck(value);
1637         if (word.word().empty())
1638                 bv->unlockInset(const_cast<InsetText *>(this));
1639         else
1640                 value = cy();
1641         return word;
1642 }
1643
1644
1645 void InsetText::selectSelectedWord(BufferView * bv)
1646 {
1647         if (the_locking_inset) {
1648                 the_locking_inset->selectSelectedWord(bv);
1649                 return;
1650         }
1651         getLyXText(bv)->selectSelectedWord();
1652         updateLocal(bv, false);
1653 }
1654
1655
1656 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1657 {
1658         if (the_locking_inset) {
1659                 if (the_locking_inset->nextChange(bv, length))
1660                         return true;
1661                 text_.cursorRight(true);
1662         }
1663         lyx::find::SearchResult result =
1664                 lyx::find::findNextChange(bv, &text_, length);
1665
1666         if (result == lyx::find::SR_FOUND) {
1667                 LyXCursor cur = text_.cursor;
1668                 bv->unlockInset(bv->theLockingInset());
1669                 if (bv->lockInset(this))
1670                         locked = true;
1671                 text_.cursor = cur;
1672                 text_.setSelectionRange(length);
1673                 updateLocal(bv, false);
1674         }
1675         return result != lyx::find::SR_NOT_FOUND;
1676 }
1677
1678
1679 bool InsetText::searchForward(BufferView * bv, string const & str,
1680                               bool cs, bool mw)
1681 {
1682         if (the_locking_inset) {
1683                 if (the_locking_inset->searchForward(bv, str, cs, mw))
1684                         return true;
1685                 text_.cursorRight(true);
1686         }
1687         lyx::find::SearchResult result =
1688                 lyx::find::find(bv, &text_, str, true, cs, mw);
1689
1690         if (result == lyx::find::SR_FOUND) {
1691                 LyXCursor cur = text_.cursor;
1692                 bv->unlockInset(bv->theLockingInset());
1693                 if (bv->lockInset(this))
1694                         locked = true;
1695                 text_.cursor = cur;
1696                 text_.setSelectionRange(str.length());
1697                 updateLocal(bv, false);
1698         }
1699         return result != lyx::find::SR_NOT_FOUND;
1700 }
1701
1702
1703 bool InsetText::searchBackward(BufferView * bv, string const & str,
1704                                bool cs, bool mw)
1705 {
1706         if (the_locking_inset) {
1707                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1708                         return true;
1709         }
1710         if (!locked) {
1711                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1712         }
1713         lyx::find::SearchResult result =
1714                 lyx::find::find(bv, &text_, str, false, cs, mw);
1715
1716         if (result == lyx::find::SR_FOUND) {
1717                 LyXCursor cur = text_.cursor;
1718                 bv->unlockInset(bv->theLockingInset());
1719                 if (bv->lockInset(this))
1720                         locked = true;
1721                 text_.cursor = cur;
1722                 text_.setSelectionRange(str.length());
1723                 updateLocal(bv, false);
1724         }
1725         return result != lyx::find::SR_NOT_FOUND;
1726 }
1727
1728
1729 bool InsetText::checkInsertChar(LyXFont & font)
1730 {
1731         return owner() ? owner()->checkInsertChar(font) : true;
1732 }
1733
1734
1735 void InsetText::collapseParagraphs(BufferView * bv)
1736 {
1737         while (paragraphs.size() > 1) {
1738                 ParagraphList::iterator const first = paragraphs.begin();
1739                 ParagraphList::iterator second = first;
1740                 advance(second, 1);
1741                 size_t const first_par_size = first->size();
1742
1743                 if (!first->empty() &&
1744                     !second->empty() &&
1745                     !first->isSeparator(first_par_size - 1)) {
1746                         first->insertChar(first_par_size, ' ');
1747                 }
1748
1749 #warning probably broken
1750                 if (text_.selection.set()) {
1751                         if (text_.selection.start.par() == 1) {
1752                                 text_.selection.start.par(1);
1753                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1754                         }
1755                         if (text_.selection.end.par() == 2) {
1756                                 text_.selection.end.par(1);
1757                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1758                         }
1759                 }
1760
1761                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1762         }
1763 }
1764
1765
1766 void InsetText::getDrawFont(LyXFont & font) const
1767 {
1768         if (!owner())
1769                 return;
1770         owner()->getDrawFont(font);
1771 }
1772
1773
1774 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1775 {
1776 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1777 // And it probably does. You have to take a look at this John. (Lgb)
1778 #warning John, have a look here. (Lgb)
1779         ParagraphList::iterator pit = plist.begin();
1780         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1781         ++pit;
1782         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1783
1784         ParagraphList::iterator pend = plist.end();
1785         for (; pit != pend; ++pit)
1786                 paragraphs.push_back(*pit);
1787 }
1788
1789
1790 void InsetText::addPreview(PreviewLoader & loader) const
1791 {
1792         ParagraphList::const_iterator pit = paragraphs.begin();
1793         ParagraphList::const_iterator pend = paragraphs.end();
1794
1795         for (; pit != pend; ++pit) {
1796                 InsetList::const_iterator it  = pit->insetlist.begin();
1797                 InsetList::const_iterator end = pit->insetlist.end();
1798                 for (; it != end; ++it)
1799                         it->inset->addPreview(loader);
1800         }
1801 }