]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
21285dbe8a5cce64a1fff4199acd62fd30cf7a31
[lyx.git] / src / insets / insettext.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *
7  *           Copyright 1998-2000 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include <fstream>
15 #include <algorithm>
16
17 #include <cstdlib>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "insettext.h"
24 #include "lyxparagraph.h"
25 #include "lyxlex.h"
26 #include "debug.h"
27 #include "lyxfont.h"
28 #include "commandtags.h"
29 #include "buffer.h"
30 #include "LyXView.h"
31 #include "BufferView.h"
32 #include "layout.h"
33 #include "LaTeXFeatures.h"
34 #include "Painter.h"
35 #include "lyx_gui_misc.h"
36 #include "lyxtext.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
39 #include "font.h"
40 #include "minibuffer.h"
41 #include "LColor.h"
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
44 #include "lyxrow.h"
45 #include "lyxrc.h"
46 #include "intl.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
49 #include "WorkArea.h"
50
51 using std::ostream;
52 using std::ifstream;
53 using std::endl;
54 using std::min;
55 using std::max;
56
57 extern unsigned char getCurrentTextClass(Buffer *);
58
59 InsetText::InsetText()
60 {
61     par = new LyXParagraph();
62     init();
63 }
64
65
66 InsetText::InsetText(InsetText const & ins)
67         : UpdatableInset()
68 {
69     par = 0;
70     init(&ins);
71     autoBreakRows = ins.autoBreakRows;
72 }
73
74
75 InsetText & InsetText::operator=(InsetText const & it)
76 {
77     init(&it);
78     autoBreakRows = it.autoBreakRows;
79     return * this;
80 }
81
82
83 void InsetText::init(InsetText const * ins)
84 {
85     top_y = 0;
86     last_width = 0;
87     last_height = 0;
88     insetAscent = 0;
89     insetDescent = 0;
90     insetWidth = 0;
91     the_locking_inset = 0;
92     cursor_visible = false;
93     interline_space = 1;
94     no_selection = false;
95     need_update = INIT;
96     drawTextXOffset = 0;
97     drawTextYOffset = 0;
98     autoBreakRows = false;
99     drawFrame = NEVER;
100     xpos = 0.0;
101     if (ins) {
102         SetParagraphData(ins->par);
103         autoBreakRows = ins->autoBreakRows;
104         drawFrame = ins->drawFrame;
105     }
106     par->SetInsetOwner(this);
107     frame_color = LColor::insetframe;
108     locked = false;
109     old_par = 0;
110 }
111
112
113 InsetText::~InsetText()
114 {
115     for(Cache::const_iterator cit = cache.begin(); cit != cache.end(); ++cit)
116         delete (*cit).second;
117 //      deleteLyXText((*cit).first);
118     LyXParagraph * p = par->next;
119     delete par;
120     while(p) {
121         par = p;
122         p = p->next;
123         delete par;
124     }
125 }
126
127
128 void InsetText::clear()
129 {
130     LyXParagraph * p = par->next;
131     delete par;
132     while(p) {
133         par = p;
134         p = p->next;
135         delete par;
136     }
137     par = new LyXParagraph();
138 }
139
140
141 Inset * InsetText::Clone(Buffer const &) const
142 {
143     InsetText * t = new InsetText(*this);
144     return t;
145 }
146
147
148 void InsetText::Write(Buffer const * buf, ostream & os) const
149 {
150     os << "Text\n";
151     WriteParagraphData(buf, os);
152 }
153
154
155 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
156 {
157     par->writeFile(buf, os, buf->params, 0, 0);
158 }
159
160
161 void InsetText::Read(Buffer const * buf, LyXLex & lex)
162 {
163     string token;
164     int pos = 0;
165     LyXParagraph * return_par = 0;
166     char depth = 0; // signed or unsigned?
167 #ifndef NEW_INSETS
168     LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
169     LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
170 #endif
171     LyXFont font(LyXFont::ALL_INHERIT);
172
173     LyXParagraph * p = par->next;
174     delete par;
175     while(p) {
176         par = p;
177         p = p->next;
178         delete par;
179     }
180     par = new LyXParagraph;
181     while (lex.IsOK()) {
182         lex.nextToken();
183         token = lex.GetString();
184         if (token.empty())
185             continue;
186         if (token == "\\end_inset")
187             break;
188         if (const_cast<Buffer*>(buf)->
189             parseSingleLyXformat2Token(lex, par, return_par,token, pos, depth,
190                                        font
191 #ifndef NEW_INSETS
192                                        , footnoteflag, footnotekind
193 #endif
194                                        ))
195         {
196             // the_end read this should NEVER happen
197             lex.printError("\\the_end read in inset! Error in document!");
198             return;
199         }
200     }
201     if (!return_par)
202             return_par = par;
203     par = return_par;
204     while(return_par) {
205         return_par->SetInsetOwner(this);
206         return_par = return_par->next;
207     }
208     
209     if (token != "\\end_inset") {
210         lex.printError("Missing \\end_inset at this point. "
211                        "Read: `$$Token'");
212     }
213     need_update = INIT;
214 }
215
216
217 int InsetText::ascent(BufferView * bv, LyXFont const &) const
218 {
219     int y_temp = 0;
220     Row * row = TEXT(bv)->GetRowNearY(y_temp);
221     insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
222     return insetAscent;
223 }
224
225
226 int InsetText::descent(BufferView * bv, LyXFont const &) const
227 {
228     int y_temp = 0;
229     Row * row = TEXT(bv)->GetRowNearY(y_temp);
230     insetDescent = TEXT(bv)->height - row->ascent_of_text() +
231         TEXT_TO_INSET_OFFSET;
232     return insetDescent;
233 }
234
235
236 int InsetText::width(BufferView * bv, LyXFont const &) const
237 {
238     insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
239     return insetWidth;
240 }
241
242
243 int InsetText::textWidth(Painter & pain) const
244 {
245     int w = getMaxWidth(pain, this);
246     return w;
247 }
248
249
250 void InsetText::draw(BufferView * bv, LyXFont const & f,
251                      int baseline, float & x, bool cleared) const
252 {
253     Painter & pain = bv->painter();
254
255     // no draw is necessary !!!
256     if ((drawFrame == LOCKED) && !locked && !par->size()) {
257         if (!cleared && (need_update == CLEAR_FRAME)) {
258             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
259                            width(bv, f) - 1,
260                            insetAscent + insetDescent - 1,
261                            LColor::background);
262         }
263         top_x = int(x);
264         top_baseline = baseline;
265         x += width(bv, f);
266         need_update = NONE;
267         return;
268     }
269
270     xpos = x;
271     UpdatableInset::draw(bv, f, baseline, x, cleared);
272
273     if (!cleared && ((need_update==FULL) || (top_x!=int(x)) ||
274                      (top_baseline!=baseline))) {
275         int w =  insetWidth;
276         int h = insetAscent + insetDescent;
277         int ty = baseline - insetAscent;
278         
279         if (ty < 0) {
280             h += ty;
281             ty = 0;
282         }
283         if ((ty + h) > pain.paperHeight())
284             h = pain.paperHeight();
285         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
286             w = pain.paperWidth();
287         pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
288         cleared = true;
289         need_update = FULL;
290     }
291     if (!cleared && (need_update == NONE))
292         return;
293
294     if (top_x != int(x)) {
295         need_update = INIT;
296         top_x = int(x);
297         bv->text->status = LyXText::CHANGED_IN_DRAW;
298         return;
299     }
300
301     top_baseline = baseline;
302     top_y = baseline - ascent(bv, f);
303     last_width = width(bv, f);
304     last_height = ascent(bv, f) + descent(bv, f);
305
306     if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
307         inset_x = cx(bv) - top_x + drawTextXOffset;
308         inset_y = cy(bv) + drawTextYOffset;
309     }
310     if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
311         x += width(bv, f);
312         need_update = NONE;
313         return;
314     }
315     x += TEXT_TO_INSET_OFFSET;
316     int y = 0;
317     Row * row = TEXT(bv)->GetRowNearY(y);
318     int y_offset = baseline - row->ascent_of_text();
319     int ph = pain.paperHeight();
320     y += y_offset;
321     while ((row != 0) && ((y+row->height()) <= 0)) {
322         y += row->height();
323         row = row->next();
324     }
325     y -= y_offset;
326     TEXT(bv)->first = y;
327     if (cleared || !locked || (need_update == FULL)) {
328         int first = y;
329         y = 0;
330         while ((row != 0) && (y < ph)) {
331             TEXT(bv)->GetVisibleRow(bv, y+first+y_offset, int(x), row,
332                                     y+first, cleared);
333             y += row->height();
334             row = row->next();
335         }
336     } else if (need_update == SELECTION) {
337         bv->screen()->ToggleToggle(TEXT(bv), y+y_offset, int(x));
338     } else {
339         locked = false;
340         if (need_update == CURSOR) {
341             bv->screen()->ToggleSelection(TEXT(bv), true, y+y_offset, int(x));
342             TEXT(bv)->ClearSelection();
343             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
344         }
345         bv->screen()->Update(TEXT(bv), y, int(x));
346         locked = true;
347     }
348     TEXT(bv)->refresh_y = 0;
349     TEXT(bv)->status = LyXText::UNCHANGED;
350     if ((need_update != CURSOR_PAR) &&
351         ((drawFrame == ALWAYS) || ((drawFrame == LOCKED) && locked)))
352     {
353             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
354                            width(bv, f) - 1, insetAscent + insetDescent - 1,
355                            frame_color);
356     } else if (need_update == CLEAR_FRAME) {
357             pain.rectangle(top_x + 1, baseline - insetAscent + 1,
358                            width(bv, f) - 1, insetAscent + insetDescent - 1,
359                            LColor::background);
360     }
361     x += width(bv, f) - TEXT_TO_INSET_OFFSET;
362     if (bv->text->status==LyXText::CHANGED_IN_DRAW)
363         need_update = INIT;
364     else if (need_update != INIT)
365         need_update = NONE;
366 }
367
368
369 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
370 {
371     if (reinit) {  // && (need_update != CURSOR)) {
372         need_update = INIT;
373         resizeLyXText(bv);
374         if (owner())
375             owner()->update(bv, font, true);
376         return;
377     }
378     if (the_locking_inset) {
379         inset_x = cx(bv) - top_x + drawTextXOffset;
380         inset_y = cy(bv) + drawTextYOffset;
381         the_locking_inset->update(bv, font, reinit);
382     }
383     if (need_update == INIT) {
384         resizeLyXText(bv);
385         need_update = FULL;
386 //      if (!owner() && bv->text)
387 //          bv->text->UpdateInset(bv, this);
388     }
389     int oldw = insetWidth;
390 #if 1
391     insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
392     // max(textWidth(bv->painter()),
393     // static_cast<int>(TEXT(bv)->width) + drawTextXOffset) +
394     // (2 * TEXT_TO_INSET_OFFSET);
395 #else
396     insetWidth = textWidth(bv->painter());
397     if (insetWidth < 0)
398             insetWidth = static_cast<int>(TEXT(bv)->width);
399 #endif
400     if (oldw != insetWidth) {
401 //          printf("TW(%p): %d-%d-%d-%d\n",this,insetWidth, oldw,
402 //                 textWidth(bv->painter()),static_cast<int>(TEXT(bv)->width));
403         resizeLyXText(bv);
404         need_update = FULL;
405 #if 0
406         if (owner()) {
407             owner()->update(bv, font, reinit);
408             return;
409         } else {
410             update(bv, font, reinit);
411         }
412 #else
413 #if 1
414         update(bv, font, reinit);
415 #else
416         UpdateLocal(bv, INIT, false);
417 #endif
418 #endif
419         return;
420     }
421     if ((need_update==CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
422         the_locking_inset)
423     {
424         TEXT(bv)->UpdateInset(bv, the_locking_inset);
425     }
426
427     if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
428         need_update = FULL;
429
430     int y_temp = 0;
431     Row * row = TEXT(bv)->GetRowNearY(y_temp);
432     insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
433     insetDescent = TEXT(bv)->height - row->ascent_of_text() +
434         TEXT_TO_INSET_OFFSET;
435 }
436
437
438 void InsetText::UpdateLocal(BufferView * bv, UpdateCodes what, bool mark_dirty)
439 {
440     TEXT(bv)->FullRebreak(bv);
441     if (need_update != INIT) {
442         if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
443             need_update = FULL;
444         else if (!the_locking_inset || (what != CURSOR))
445             need_update = what;
446     }
447     if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
448         TEXT(bv)->selection)
449             bv->updateInset(this, mark_dirty);
450     bv->owner()->showState();
451     if (old_par != cpar(bv)) {
452             bv->owner()->setLayout(cpar(bv)->GetLayout());
453             old_par = cpar(bv);
454     }
455 }
456
457
458 string const InsetText::EditMessage() const
459 {
460     return _("Opened Text Inset");
461 }
462
463
464 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
465 {
466 //    par->SetInsetOwner(this);
467     UpdatableInset::Edit(bv, x, y, button);
468
469     if (!bv->lockInset(this)) {
470         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
471         return;
472     }
473     locked = true;
474     the_locking_inset = 0;
475     inset_pos = inset_x = inset_y = 0;
476     inset_par = 0;
477     old_par = 0;
478     if (!checkAndActivateInset(bv, x, y, button))
479         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
480                                            y+insetAscent);
481     TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
482     bv->text->FinishUndo();
483     ShowInsetCursor(bv);
484     UpdateLocal(bv, FULL, false);
485 }
486
487
488 void InsetText::InsetUnlock(BufferView * bv)
489 {
490     if (the_locking_inset) {
491         the_locking_inset->InsetUnlock(bv);
492         the_locking_inset = 0;
493     }
494     HideInsetCursor(bv);
495     no_selection = false;
496     locked = false;
497     TEXT(bv)->selection = 0;
498     UpdateLocal(bv, CLEAR_FRAME, false);
499     if (owner())
500             bv->owner()->setLayout(owner()->getLyXText(bv)
501                                     ->cursor.par()->GetLayout());
502     else
503             bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
504 }
505
506
507 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
508 {
509     lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
510     if (!inset)
511         return false;
512     if (inset == cpar(bv)->GetInset(cpos(bv))) {
513         lyxerr[Debug::INSETS] << "OK" << endl;
514         the_locking_inset = inset;
515         inset_x = cx(bv) - top_x + drawTextXOffset;
516         inset_y = cy(bv) + drawTextYOffset;
517         inset_pos = cpos(bv);
518         inset_par = cpar(bv);
519         TEXT(bv)->UpdateInset(bv, the_locking_inset);
520         return true;
521     } else if (the_locking_inset && (the_locking_inset == inset)) {
522         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
523             lyxerr[Debug::INSETS] << "OK" << endl;
524             inset_x = cx(bv) - top_x + drawTextXOffset;
525             inset_y = cy(bv) + drawTextYOffset;
526         } else {
527             lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
528         }
529     } else if (the_locking_inset) {
530         lyxerr[Debug::INSETS] << "MAYBE" << endl;
531         return the_locking_inset->LockInsetInInset(bv, inset);
532     }
533     lyxerr[Debug::INSETS] << "NOT OK" << endl;
534     return false;
535 }
536
537
538 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
539                                    bool lr)
540 {
541     if (!the_locking_inset)
542         return false;
543     if (the_locking_inset == inset) {
544         the_locking_inset->InsetUnlock(bv);
545         TEXT(bv)->UpdateInset(bv, inset);
546         the_locking_inset = 0;
547         if (lr)
548             moveRight(bv, false);
549         old_par = 0; // force layout setting
550         UpdateLocal(bv, CURSOR_PAR, false);
551         return true;
552     }
553     return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
554 }
555
556
557 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
558 {
559     if (!the_locking_inset)
560         return false;
561     if (the_locking_inset != inset) {
562         TEXT(bv)->UpdateInset(bv, the_locking_inset);
563         need_update = CURSOR_PAR;
564         return the_locking_inset->UpdateInsetInInset(bv, inset);
565     }
566 //    UpdateLocal(bv, FULL, false);
567     if (TEXT(bv)->UpdateInset(bv, inset))
568         UpdateLocal(bv, CURSOR_PAR, false);
569     if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
570         inset_x = cx(bv) - top_x + drawTextXOffset;
571         inset_y = cy(bv) + drawTextYOffset;
572     }
573     return true;
574 }
575
576
577 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
578 {
579     no_selection = false;
580
581     int tmp_x = x - drawTextXOffset;
582     int tmp_y = y + insetAscent;
583     Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
584
585     HideInsetCursor(bv);
586     if (the_locking_inset) {
587         if (the_locking_inset == inset) {
588             the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
589             return;
590         } else if (inset) {
591             // otherwise unlock the_locking_inset and lock the new inset
592             the_locking_inset->InsetUnlock(bv);
593             inset_x = cx(bv) - top_x + drawTextXOffset;
594             inset_y = cy(bv) + drawTextYOffset;
595             inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
596             inset->Edit(bv, x - inset_x, y - inset_y, button);
597             if (the_locking_inset) {
598                 UpdateLocal(bv, CURSOR_PAR, false);
599             }
600             return;
601         }
602         // otherwise only unlock the_locking_inset
603         the_locking_inset->InsetUnlock(bv);
604         the_locking_inset = 0;
605     }
606     if (bv->theLockingInset()) {
607         if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
608             UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
609             inset_x = cx(bv) - top_x + drawTextXOffset;
610             inset_y = cy(bv) + drawTextYOffset;
611             inset_pos = cpos(bv);
612             inset_par = cpar(bv);
613             uinset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
614             uinset->Edit(bv, x - inset_x, y - inset_y, 0);
615             if (the_locking_inset) {
616                 UpdateLocal(bv, CURSOR_PAR, false);
617             }
618             return;
619         }
620     }
621     if (!inset) {
622         bool paste_internally = false;
623         if ((button == 2) && TEXT(bv)->selection) {
624             LocalDispatch(bv, LFUN_COPY, "");
625             paste_internally = true;
626         }
627         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
628                                            y+insetAscent);
629         TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
630         UpdateLocal(bv, CURSOR, false);
631         bv->owner()->setLayout(cpar(bv)->GetLayout());
632         old_par = cpar(bv);
633         // Insert primary selection with middle mouse
634         // if there is a local selection in the current buffer,
635         // insert this
636         if (button == 2) {
637             if (paste_internally)
638                 LocalDispatch(bv, LFUN_PASTE, "");
639             else
640                 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
641         }
642     }
643     ShowInsetCursor(bv);
644 }
645
646
647 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
648 {
649     UpdatableInset * inset = 0;
650
651     if (the_locking_inset) {
652             the_locking_inset->InsetButtonRelease(bv,
653                                                   x - inset_x, y - inset_y,
654                                                   button);
655     } else {
656         if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
657             inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
658             if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
659                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
660             } else {
661                 inset_x = cx(bv) - top_x + drawTextXOffset;
662                 inset_y = cy(bv) + drawTextYOffset;
663                 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
664                 inset->Edit(bv, x - inset_x, y - inset_y, button);
665             }
666             UpdateLocal(bv, CURSOR_PAR, false);
667         }
668     }
669     no_selection = false;
670 }
671
672
673 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
674 {
675     if (the_locking_inset) {
676         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
677                                              y - inset_y,state);
678         return;
679     }
680     if (!no_selection) {
681         HideInsetCursor(bv);
682         TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
683                                            y+insetAscent);
684         TEXT(bv)->SetSelection();
685         if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
686             TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
687             UpdateLocal(bv, SELECTION, false);
688         ShowInsetCursor(bv);
689     }
690     no_selection = false;
691 }
692
693
694 void InsetText::InsetKeyPress(XKeyEvent * xke)
695 {
696     if (the_locking_inset) {
697         the_locking_inset->InsetKeyPress(xke);
698         return;
699     }
700 }
701
702
703 UpdatableInset::RESULT
704 InsetText::LocalDispatch(BufferView * bv,
705                          int action, string const & arg)
706 {
707     no_selection = false;
708     UpdatableInset::RESULT
709         result= UpdatableInset::LocalDispatch(bv, action, arg);
710     if (result != UNDISPATCHED) {
711         return DISPATCHED;
712     }
713
714     result=DISPATCHED;
715     if ((action < 0) && arg.empty())
716         return FINISHED;
717
718     if (the_locking_inset) {
719         result = the_locking_inset->LocalDispatch(bv, action, arg);
720         if (result == DISPATCHED_NOUPDATE)
721             return result;
722         else if (result == DISPATCHED) {
723             UpdateLocal(bv, CURSOR_PAR, false);
724             return result;
725         } else if (result == FINISHED) {
726             switch(action) {
727             case -1:
728             case LFUN_RIGHT:
729                 moveRight(bv, false);
730                 break;
731             case LFUN_DOWN:
732                 moveDown(bv);
733                 break;
734             }
735             the_locking_inset = 0;
736             return DISPATCHED;
737         }
738     }
739     HideInsetCursor(bv);
740     switch (action) {
741         // Normal chars
742     case -1:
743         if (bv->buffer()->isReadonly()) {
744             LyXBell();
745 //          setErrorMessage(N_("Document is read only"));
746             break;
747         }
748         if (!arg.empty()) {
749             /* Automatically delete the currently selected
750              * text and replace it with what is being
751              * typed in now. Depends on lyxrc settings
752              * "auto_region_delete", which defaults to
753              * true (on). */
754
755             bv->text->SetUndo(bv->buffer(), Undo::INSERT,
756 #ifndef NEW_INSETS
757                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
758                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
759 #else
760                               bv->text->cursor.par()->previous,
761                               bv->text->cursor.par()->next
762 #endif
763                     );
764             // if an empty paragraph set the language to the surronding
765             // paragraph language on insertion of the first character!
766             if (!par->Last() && !par->next) {
767                 LyXText * text = 0;
768                 if (owner()) {
769                     Inset * inset = owner();
770                     while(inset && inset->getLyXText(bv) == TEXT(bv))
771                         inset = inset->owner();
772                     if (inset)
773                         text = inset->getLyXText(bv);
774                 }
775                 if (!text)
776                     text = bv->text;
777                 LyXFont font(LyXFont::ALL_IGNORE);
778                 font.setLanguage(text->cursor.par()->getParLanguage(bv->buffer()->params));
779                 SetFont(bv, font, false);
780             }
781             if (lyxrc.auto_region_delete) {
782                 if (TEXT(bv)->selection){
783                     TEXT(bv)->CutSelection(bv, false);
784                 }
785             }
786             TEXT(bv)->ClearSelection();
787             for (string::size_type i = 0; i < arg.length(); ++i) {
788                 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], TEXT(bv));
789             }
790         }
791         UpdateLocal(bv, CURSOR_PAR, true);
792         break;
793         // --- Cursor Movements ---------------------------------------------
794     case LFUN_RIGHTSEL:
795         bv->text->FinishUndo();
796         moveRight(bv, false, true);
797         TEXT(bv)->SetSelection();
798         UpdateLocal(bv, SELECTION, false);
799         break;
800     case LFUN_RIGHT:
801         result = moveRight(bv);
802         bv->text->FinishUndo();
803         TEXT(bv)->ClearSelection();
804         UpdateLocal(bv, CURSOR, false);
805         break;
806     case LFUN_LEFTSEL:
807         bv->text->FinishUndo();
808         moveLeft(bv, false, true);
809         TEXT(bv)->SetSelection();
810         UpdateLocal(bv, SELECTION, false);
811         break;
812     case LFUN_LEFT:
813         bv->text->FinishUndo();
814         result= moveLeft(bv);
815         TEXT(bv)->ClearSelection();
816         UpdateLocal(bv, CURSOR, false);
817         break;
818     case LFUN_DOWNSEL:
819         bv->text->FinishUndo();
820         moveDown(bv);
821         TEXT(bv)->SetSelection();
822         UpdateLocal(bv, SELECTION, false);
823         break;
824     case LFUN_DOWN:
825         bv->text->FinishUndo();
826         result = moveDown(bv);
827         TEXT(bv)->ClearSelection();
828         UpdateLocal(bv, CURSOR, false);
829         break;
830     case LFUN_UPSEL:
831         bv->text->FinishUndo();
832         moveUp(bv);
833         TEXT(bv)->SetSelection();
834         UpdateLocal(bv, SELECTION, false);
835         break;
836     case LFUN_UP:
837         bv->text->FinishUndo();
838         result = moveUp(bv);
839         TEXT(bv)->ClearSelection();
840         UpdateLocal(bv, CURSOR, false);
841         break;
842     case LFUN_HOME:
843         bv->text->FinishUndo();
844         TEXT(bv)->CursorHome(bv);
845         UpdateLocal(bv, CURSOR, false);
846         break;
847     case LFUN_END:
848         TEXT(bv)->CursorEnd(bv);
849         UpdateLocal(bv, CURSOR, false);
850         break;
851     case LFUN_BACKSPACE:
852         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
853 #ifndef NEW_INSETS
854           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
855           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
856 #else
857           bv->text->cursor.par()->previous,
858           bv->text->cursor.par()->next
859 #endif
860                 );
861         if (TEXT(bv)->selection)
862             TEXT(bv)->CutSelection(bv);
863         else
864             TEXT(bv)->Backspace(bv);
865         UpdateLocal(bv, CURSOR_PAR, true);
866         break;
867     case LFUN_DELETE:
868         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
869 #ifndef NEW_INSETS
870           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
871           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
872 #else
873           bv->text->cursor.par()->previous,
874           bv->text->cursor.par()->next
875 #endif
876                 );
877         if (TEXT(bv)->selection)
878             TEXT(bv)->CutSelection(bv);
879         else
880             TEXT(bv)->Delete(bv);
881         UpdateLocal(bv, CURSOR_PAR, true);
882         break;
883     case LFUN_CUT:
884         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
885 #ifndef NEW_INSETS
886           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
887           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
888 #else
889           bv->text->cursor.par()->previous,
890           bv->text->cursor.par()->next
891 #endif
892                 );
893         TEXT(bv)->CutSelection(bv);
894         UpdateLocal(bv, CURSOR_PAR, true);
895         break;
896     case LFUN_COPY:
897         bv->text->FinishUndo();
898         TEXT(bv)->CopySelection(bv);
899         UpdateLocal(bv, CURSOR_PAR, false);
900         break;
901     case LFUN_PASTESELECTION:
902     {
903         string clip(bv->workarea()->getClipboard());
904         
905         if (clip.empty())
906             break;
907         if (arg == "paragraph") {
908                 TEXT(bv)->InsertStringB(bv, clip);
909         } else {
910                 TEXT(bv)->InsertStringA(bv, clip);
911         }
912         UpdateLocal(bv, CURSOR_PAR, true);
913         break;
914     }
915     case LFUN_PASTE:
916         if (!autoBreakRows) {
917             CutAndPaste cap;
918
919             if (cap.nrOfParagraphs() > 1) {
920                 WriteAlert(_("Impossible operation"),
921                            _("Cannot include more than one paragraph!"),
922                            _("Sorry."));
923                 break;
924             }
925         }
926         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
927 #ifndef NEW_INSETS
928           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
929           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
930 #else
931           bv->text->cursor.par()->previous,
932           bv->text->cursor.par()->next
933 #endif
934                 );
935         TEXT(bv)->PasteSelection(bv);
936         UpdateLocal(bv, CURSOR_PAR, true);
937         break;
938     case LFUN_BREAKPARAGRAPH:
939         if (!autoBreakRows)
940             return DISPATCHED;
941         TEXT(bv)->BreakParagraph(bv, 0);
942         UpdateLocal(bv, FULL, true);
943         break;
944     case LFUN_BREAKLINE:
945         if (!autoBreakRows)
946             return DISPATCHED;
947         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
948 #ifndef NEW_INSETS
949             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
950             bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
951 #else
952             bv->text->cursor.par()->previous,
953             bv->text->cursor.par()->next
954 #endif
955                 );
956         TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
957         UpdateLocal(bv, CURSOR_PAR, true);
958         break;
959     case LFUN_LAYOUT:
960         // do not set layouts on non breakable textinsets
961         if (autoBreakRows) {
962             LyXTextClass::size_type cur_layout = cpar(bv)->layout;
963       
964             // Derive layout number from given argument (string)
965             // and current buffer's textclass (number). */    
966             LyXTextClassList::ClassList::size_type tclass =
967                 bv->buffer()->params.textclass;
968             std::pair <bool, LyXTextClass::size_type> layout = 
969                 textclasslist.NumberOfLayout(tclass, arg);
970
971             // If the entry is obsolete, use the new one instead.
972             if (layout.first) {
973                 string obs = textclasslist.Style(tclass,layout.second).
974                     obsoleted_by();
975                 if (!obs.empty()) 
976                     layout = textclasslist.NumberOfLayout(tclass, obs);
977             }
978
979             // see if we found the layout number:
980             if (!layout.first) {
981                 string msg = string(N_("Layout ")) + arg + N_(" not known");
982
983                 bv->owner()->getMiniBuffer()->Set(msg);
984                 break;
985             }
986
987             if (cur_layout != layout.second) {
988                 cur_layout = layout.second;
989                 TEXT(bv)->SetLayout(bv, layout.second);
990                 bv->owner()->setLayout(cpar(bv)->GetLayout());
991                 UpdateLocal(bv, CURSOR_PAR, true);
992             }
993         } else {
994             // reset the layout box
995             bv->owner()->setLayout(cpar(bv)->GetLayout());
996         }
997         break;
998     case LFUN_PARAGRAPH_SPACING:
999             // This one is absolutely not working. When fiddling with this
1000             // it also seems to me that the paragraphs inside the insettext
1001             // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1002     {
1003             LyXParagraph * par = TEXT(bv)->cursor.par();
1004             Spacing::Space cur_spacing = par->spacing.getSpace();
1005             float cur_value = 1.0;
1006             if (cur_spacing == Spacing::Other) {
1007                     cur_value = par->spacing.getValue();
1008             }
1009                         
1010             std::istringstream istr(arg.c_str());
1011             string tmp;
1012             istr >> tmp;
1013             Spacing::Space new_spacing = cur_spacing;
1014             float new_value = cur_value;
1015             if (tmp.empty()) {
1016                     lyxerr << "Missing argument to `paragraph-spacing'"
1017                            << endl;
1018             } else if (tmp == "single") {
1019                     new_spacing = Spacing::Single;
1020             } else if (tmp == "onehalf") {
1021                     new_spacing = Spacing::Onehalf;
1022             } else if (tmp == "double") {
1023                     new_spacing = Spacing::Double;
1024             } else if (tmp == "other") {
1025                     new_spacing = Spacing::Other;
1026                     float tmpval = 0.0;
1027                     istr >> tmpval;
1028                     lyxerr << "new_value = " << tmpval << endl;
1029                     if (tmpval != 0.0)
1030                             new_value = tmpval;
1031             } else if (tmp == "default") {
1032                     new_spacing = Spacing::Default;
1033             } else {
1034                     lyxerr << _("Unknown spacing argument: ")
1035                            << arg << endl;
1036             }
1037             if (cur_spacing != new_spacing || cur_value != new_value) {
1038                     par->spacing.set(new_spacing, new_value);
1039                     //TEXT(bv)->RedoParagraph(owner->view());
1040                     UpdateLocal(bv, CURSOR_PAR, true);
1041                     //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1042             }
1043     }
1044     break;
1045         
1046     default:
1047         result = UNDISPATCHED;
1048         break;
1049     }
1050     if (result != FINISHED) {
1051         ShowInsetCursor(bv);
1052     } else
1053         bv->unlockInset(this);
1054     return result;
1055 }
1056
1057
1058 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1059 {
1060     TexRow texrow;
1061     buf->latexParagraphs(os, par, 0, texrow);
1062     return texrow.rows();
1063 }
1064
1065
1066 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1067 {
1068     LyXParagraph * p = par;
1069     unsigned int lines = 0;
1070     
1071     string tmp;
1072     while (p) {
1073         tmp = buf->asciiParagraph(p, linelen);
1074         lines += countChar(tmp, '\n');
1075         os << tmp;
1076         p = p->next;
1077     }
1078     return lines;
1079 }
1080
1081
1082 void InsetText::Validate(LaTeXFeatures & features) const
1083 {
1084     LyXParagraph * p = par;
1085     while(p) {
1086         p->validate(features);
1087         p = p->next;
1088     }
1089 }
1090
1091
1092 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1093 {
1094     if (textclasslist.Style(buf->params.textclass,
1095                             p->GetLayout()).labeltype != LABEL_MANUAL)
1096         return 0;
1097     else
1098         return p->BeginningOfMainBody();
1099 }
1100
1101
1102 void InsetText::GetCursorPos(BufferView * bv,
1103                              int & x, int & y) const
1104 {
1105     x = cx(bv);
1106     y = cy(bv);
1107 }
1108
1109
1110 unsigned int InsetText::InsetInInsetY()
1111 {
1112     if (!the_locking_inset)
1113         return 0;
1114
1115     return (inset_y + the_locking_inset->InsetInInsetY());
1116 }
1117
1118
1119 void InsetText::ToggleInsetCursor(BufferView * bv)
1120 {
1121     if (the_locking_inset) {
1122         the_locking_inset->ToggleInsetCursor(bv);
1123         return;
1124     }
1125
1126     LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1127
1128     int asc = lyxfont::maxAscent(font);
1129     int desc = lyxfont::maxDescent(font);
1130   
1131     if (cursor_visible)
1132         bv->hideLockedInsetCursor();
1133     else
1134         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1135     cursor_visible = !cursor_visible;
1136 }
1137
1138
1139 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1140 {
1141     if (the_locking_inset) {
1142         the_locking_inset->ShowInsetCursor(bv);
1143         return;
1144     }
1145     if (!cursor_visible) {
1146         LyXFont font = TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1147         
1148         int asc = lyxfont::maxAscent(font);
1149         int desc = lyxfont::maxDescent(font);
1150
1151         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1152         if (show)
1153             bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1154         cursor_visible = true;
1155     }
1156 }
1157
1158
1159 void InsetText::HideInsetCursor(BufferView * bv)
1160 {
1161     if (cursor_visible) {
1162         bv->hideLockedInsetCursor();
1163         cursor_visible = false;
1164     }
1165     if (the_locking_inset)
1166         the_locking_inset->HideInsetCursor(bv);
1167 }
1168
1169
1170 UpdatableInset::RESULT
1171 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1172 {
1173     if (!cpar(bv)->next && (cpos(bv) >= cpar(bv)->Last()))
1174         return FINISHED;
1175     if (activate_inset && checkAndActivateInset(bv, false))
1176         return DISPATCHED;
1177     TEXT(bv)->CursorRight(bv, selecting);
1178     return DISPATCHED_NOUPDATE;
1179 }
1180
1181
1182 UpdatableInset::RESULT
1183 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1184 {
1185     if (!cpar(bv)->previous && (cpos(bv) <= 0))
1186         return FINISHED;
1187     TEXT(bv)->CursorLeft(bv, selecting);
1188     if (activate_inset && checkAndActivateInset(bv, true))
1189         return DISPATCHED;
1190     return DISPATCHED_NOUPDATE;
1191 }
1192
1193
1194 UpdatableInset::RESULT
1195 InsetText::moveUp(BufferView * bv)
1196 {
1197     if (!crow(bv)->previous())
1198         return FINISHED;
1199     TEXT(bv)->CursorUp(bv);
1200     return DISPATCHED_NOUPDATE;
1201 }
1202
1203
1204 UpdatableInset::RESULT
1205 InsetText::moveDown(BufferView * bv)
1206 {
1207     if (!crow(bv)->next())
1208         return FINISHED;
1209     TEXT(bv)->CursorDown(bv);
1210     return DISPATCHED_NOUPDATE;
1211 }
1212
1213
1214 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1215 {
1216     if (the_locking_inset) {
1217         if (the_locking_inset->InsertInsetAllowed(inset))
1218             return the_locking_inset->InsertInset(bv, inset);
1219         return false;
1220     }
1221     bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1222 #ifndef NEW_INSETS
1223               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1224               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1225 #else
1226               bv->text->cursor.par()->previous,
1227               bv->text->cursor.par()->next
1228 #endif
1229             );
1230     if (inset->Editable() == Inset::IS_EDITABLE) {
1231         UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1232         i->setOwner(static_cast<UpdatableInset *>(this));
1233     }
1234     HideInsetCursor(bv);
1235     TEXT(bv)->InsertInset(bv, inset);
1236     TEXT(bv)->selection = 0;
1237     bv->fitCursor(TEXT(bv));
1238     UpdateLocal(bv, CURSOR_PAR, true);
1239     static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1240     ShowInsetCursor(bv);
1241     return true;
1242 }
1243
1244
1245 UpdatableInset * InsetText::GetLockingInset()
1246 {
1247     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1248 }
1249
1250
1251 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1252 {
1253     if (c == LyxCode())
1254         return this;
1255     if (the_locking_inset)
1256         return the_locking_inset->GetFirstLockingInsetOfType(c);
1257     return 0;
1258 }
1259
1260
1261 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1262 {
1263     bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1264 #ifndef NEW_INSETS
1265               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1266               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1267 #else
1268               bv->text->cursor.par()->previous,
1269               bv->text->cursor.par()->next
1270 #endif
1271             );
1272     TEXT(bv)->SetFont(bv, font, toggleall);
1273     bv->fitCursor(TEXT(bv));
1274     UpdateLocal(bv, CURSOR_PAR, true);
1275 }
1276
1277
1278 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1279 {
1280     if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1281         unsigned int x;
1282         unsigned int y;
1283         Inset * inset =
1284             static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1285         if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1286             return false;
1287         LyXFont const font =
1288                 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1289         if (behind) {
1290             x = inset->width(bv, font);
1291             y = inset->descent(bv, font);
1292         } else {
1293             x = y = 0;
1294         }
1295         inset_x = cx(bv) - top_x + drawTextXOffset;
1296         inset_y = cy(bv) + drawTextYOffset;
1297         inset->Edit(bv, x - inset_x, y - inset_y, 0);
1298         if (!the_locking_inset)
1299             return false;
1300         UpdateLocal(bv, CURSOR_PAR, false);
1301         return true;
1302     }
1303     return false;
1304 }
1305
1306
1307 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1308                                       int button)
1309 {
1310     x = x - drawTextXOffset;
1311     y = y + insetAscent;
1312     Inset * inset = bv->checkInsetHit(TEXT(bv), x, y, button);
1313
1314     if (inset) {
1315         if (x < 0)
1316             x = insetWidth;
1317         if (y < 0)
1318             y = insetDescent;
1319         inset_x = cx(bv) - top_x + drawTextXOffset;
1320         inset_y = cy(bv) + drawTextYOffset;
1321         inset->Edit(bv, x - inset_x, y - inset_y, button);
1322         if (!the_locking_inset)
1323             return false;
1324         UpdateLocal(bv, CURSOR_PAR, false);
1325         return true;
1326     }
1327     return false;
1328 }
1329
1330
1331 int InsetText::getMaxWidth(Painter & pain, UpdatableInset const * inset) const
1332 {
1333     int w = UpdatableInset::getMaxWidth(pain, inset);
1334     if (w < 0) {
1335         return w;
1336     }
1337     if (owner()) {
1338         w = w - top_x + owner()->x();
1339         return w;
1340     }
1341     w -= (2 * TEXT_TO_INSET_OFFSET);
1342     return w - top_x;
1343 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1344 }
1345
1346
1347 void InsetText::SetParagraphData(LyXParagraph *p)
1348 {
1349     LyXParagraph * np;
1350
1351     if (par) {
1352         np = par->next;
1353         delete par;
1354         while(np) {
1355             par = np;
1356             np = np->next;
1357             delete par;
1358         }
1359     }
1360     par = p->Clone();
1361     par->SetInsetOwner(this);
1362     np = par;
1363     while(p->next) {
1364         p = p->next;
1365         np->next = p->Clone();
1366         np->next->previous = np;
1367         np = np->next;
1368         np->SetInsetOwner(this);
1369     }
1370     need_update = INIT;
1371 }
1372
1373
1374 void InsetText::SetAutoBreakRows(bool flag)
1375 {
1376     if (flag != autoBreakRows) {
1377         autoBreakRows = flag;
1378         need_update = FULL;
1379         if (!flag)
1380             removeNewlines();
1381     }
1382 }
1383
1384
1385 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1386 {
1387     if (how != drawFrame) {
1388         drawFrame = how;
1389         if (bv)
1390             UpdateLocal(bv, DRAW_FRAME, false);
1391     }
1392 }
1393
1394
1395 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1396 {
1397     if (frame_color != col) {
1398         frame_color = col;
1399         if (bv)
1400             UpdateLocal(bv, DRAW_FRAME, false);
1401     }
1402 }
1403
1404 #if 0
1405 LyXFont InsetText::GetDrawFont(BufferView * bv, LyXParagraph * p, int pos) const
1406 {
1407     return TEXT(bv)->GetFont(bv->buffer(), p, pos);
1408 }
1409 #endif
1410
1411
1412 int InsetText::cx(BufferView * bv) const
1413 {
1414     return TEXT(bv)->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1415 }
1416
1417
1418 int InsetText::cy(BufferView * bv) const
1419 {
1420     LyXFont font;
1421     return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1422 }
1423
1424
1425 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1426 {
1427     return TEXT(bv)->cursor.pos();
1428 }
1429
1430
1431 LyXParagraph * InsetText::cpar(BufferView * bv) const
1432 {
1433     return TEXT(bv)->cursor.par();
1434 }
1435
1436
1437 Row * InsetText::crow(BufferView * bv) const
1438 {
1439     return TEXT(bv)->cursor.row();
1440 }
1441
1442
1443 LyXText * InsetText::getLyXText(BufferView * bv) const
1444 {
1445     if (cache.find(bv) != cache.end())
1446         return cache[bv];
1447     LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1448     lt->init(bv);
1449     cache[bv] = lt;
1450     if (the_locking_inset) {
1451         lt->SetCursor(bv, inset_par, inset_pos);
1452     }
1453     return lt;
1454 }
1455
1456
1457 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1458 {
1459     if (cache.find(bv) == cache.end())
1460         return;
1461     delete cache[bv];
1462     cache.erase(bv);
1463     if (recursive) {
1464         /// then remove all LyXText in text-insets
1465         LyXParagraph * p = par;
1466         for(;p;p = p->next) {
1467             p->deleteInsetsLyXText(bv);
1468         }
1469     }
1470 }
1471
1472
1473 void InsetText::resizeLyXText(BufferView * bv) const
1474 {
1475     if (!par->next && !par->size()) // resize not neccessary!
1476         return;
1477     if (cache.find(bv) == cache.end())
1478         return;
1479
1480     LyXParagraph * lpar = 0;
1481     LyXParagraph * selstartpar = 0;
1482     LyXParagraph * selendpar = 0;
1483     LyXParagraph::size_type pos = 0;
1484     LyXParagraph::size_type selstartpos = 0;
1485     LyXParagraph::size_type selendpos = 0;
1486     int selection = 0;
1487     int mark_set = 0;
1488
1489 //    ProhibitInput(bv);
1490
1491     if (locked) {
1492         lpar = TEXT(bv)->cursor.par();
1493         pos = TEXT(bv)->cursor.pos();
1494         selstartpar = TEXT(bv)->sel_start_cursor.par();
1495         selstartpos = TEXT(bv)->sel_start_cursor.pos();
1496         selendpar = TEXT(bv)->sel_end_cursor.par();
1497         selendpos = TEXT(bv)->sel_end_cursor.pos();
1498         selection = TEXT(bv)->selection;
1499         mark_set = TEXT(bv)->mark_set;
1500     }
1501     deleteLyXText(bv, (the_locking_inset == 0));
1502
1503     if (lpar) {
1504         TEXT(bv)->selection = true;
1505         /* at this point just to avoid the Delete-Empty-Paragraph
1506          * Mechanism when setting the cursor */
1507         TEXT(bv)->mark_set = mark_set;
1508         if (selection) {
1509             TEXT(bv)->SetCursor(bv, selstartpar, selstartpos);
1510             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1511             TEXT(bv)->SetCursor(bv, selendpar, selendpos);
1512             TEXT(bv)->SetSelection();
1513             TEXT(bv)->SetCursor(bv, lpar, pos);
1514         } else {
1515             TEXT(bv)->SetCursor(bv, lpar, pos);
1516             TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1517             TEXT(bv)->selection = false;
1518         }
1519     }
1520     if (bv->screen())
1521             TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1522     // this will scroll the screen such that the cursor becomes visible 
1523     bv->updateScrollbar();
1524 //    AllowInput(bv);
1525     if (the_locking_inset) {
1526         /// then resize all LyXText in text-insets
1527         inset_x = cx(bv) - top_x + drawTextXOffset;
1528         inset_y = cy(bv) + drawTextYOffset;
1529         for(LyXParagraph * p = par; p; p = p->next) {
1530             p->resizeInsetsLyXText(bv);
1531         }
1532     }
1533     need_update = FULL;
1534 }
1535
1536
1537 void InsetText::removeNewlines()
1538 {
1539     for(LyXParagraph * p = par; p; p = p->next) {
1540         for(int i = 0; i < p->Last(); ++i) {
1541             if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1542                 p->Erase(i);
1543         }
1544     }
1545 }