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