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