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