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