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