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