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