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