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