]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
02ab563ae9a41b03621585cf3b190b0b73821a93
[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-2001 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 "LColor.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "lyxrow.h"
44 #include "lyxrc.h"
45 #include "intl.h"
46 #include "trans_mgr.h"
47 #include "lyxscreen.h"
48 #include "WorkArea.h"
49 #include "gettext.h"
50 #include "lyxfunc.h"
51
52 using std::ostream;
53 using std::ifstream;
54 using std::endl;
55 using std::min;
56 using std::max;
57
58 extern unsigned char getCurrentTextClass(Buffer *);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
61
62 InsetText::InsetText()
63 {
64         par = new LyXParagraph;
65         init();
66 }
67
68
69 InsetText::InsetText(InsetText const & ins)
70         : UpdatableInset()
71 {
72         par = 0;
73         init(&ins);
74         autoBreakRows = ins.autoBreakRows;
75 }
76
77
78 InsetText & InsetText::operator=(InsetText const & it)
79 {
80         init(&it);
81         autoBreakRows = it.autoBreakRows;
82         return * this;
83 }
84
85
86 void InsetText::init(InsetText const * ins)
87 {
88         top_y = 0;
89         last_width = 0;
90         last_height = 0;
91         insetAscent = 0;
92         insetDescent = 0;
93         insetWidth = 0;
94         the_locking_inset = 0;
95         old_max_width = 0;
96         no_selection = false;
97         need_update = INIT;
98         drawTextXOffset = 0;
99         drawTextYOffset = 0;
100         autoBreakRows = false;
101         drawFrame_ = NEVER;
102         xpos = 0.0;
103         if (ins) {
104                 SetParagraphData(ins->par);
105                 autoBreakRows = ins->autoBreakRows;
106                 drawFrame_ = ins->drawFrame_;
107         }
108         par->SetInsetOwner(this);
109         frame_color = LColor::insetframe;
110         locked = false;
111         old_par = 0;
112         last_drawn_width = -1;
113         frame_is_visible = false;
114 }
115
116
117 InsetText::~InsetText()
118 {
119         // delete all instances of LyXText before deleting the paragraps used
120         // by it.
121         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
122                 delete (*cit).second;
123                 (*cit).second = 0;
124         }
125
126         while (par) {
127                 LyXParagraph * tmp = par->next();
128                 delete par;
129                 par = tmp;
130         }
131 }
132
133
134 void InsetText::clear()
135 {
136         // delete all instances of LyXText before deleting the paragraps used
137         // by it.
138         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
139                 delete (*cit).second;
140                 (*cit).second = 0;
141         }
142
143         while (par) {
144                 LyXParagraph * tmp = par->next();
145                 delete par;
146                 par = tmp;
147         }
148         par = new LyXParagraph;
149 }
150
151
152 Inset * InsetText::Clone(Buffer const &) const
153 {
154         InsetText * t = new InsetText(*this);
155         return t;
156 }
157
158
159 void InsetText::Write(Buffer const * buf, ostream & os) const
160 {
161         os << "Text\n";
162         WriteParagraphData(buf, os);
163 }
164
165
166 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
167 {
168         par->writeFile(buf, os, buf->params, 0);
169 }
170
171
172 void InsetText::Read(Buffer const * buf, LyXLex & lex)
173 {
174         string token;
175         int pos = 0;
176         LyXParagraph * return_par = 0;
177         char depth = 0; // signed or unsigned?
178         LyXFont font(LyXFont::ALL_INHERIT);
179
180         clear();
181         
182         while (lex.IsOK()) {
183                 lex.nextToken();
184                 token = lex.GetString();
185                 if (token.empty())
186                         continue;
187                 if (token == "\\end_inset")
188                         break;
189                 if (const_cast<Buffer*>(buf)->
190                         parseSingleLyXformat2Token(lex, par, return_par,
191                                                    token, pos, depth, font)) {
192                         // the_end read this should NEVER happen
193                         lex.printError("\\the_end read in inset! Error in document!");
194                         return;
195                 }
196         }
197         if (!return_par)
198                 return_par = par;
199         par = return_par;
200         while(return_par) {
201                 return_par->SetInsetOwner(this);
202                 return_par = return_par->next();
203         }
204         
205         if (token != "\\end_inset") {
206                 lex.printError("Missing \\end_inset at this point. "
207                                            "Read: `$$Token'");
208         }
209         need_update = INIT;
210 }
211
212
213 int InsetText::ascent(BufferView * bv, LyXFont const &) const
214 {
215         int y_temp = 0;
216         Row * row = TEXT(bv)->GetRowNearY(y_temp);
217         insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
218         return insetAscent;
219 }
220
221
222 int InsetText::descent(BufferView * bv, LyXFont const &) const
223 {
224         int y_temp = 0;
225         Row * row = TEXT(bv)->GetRowNearY(y_temp);
226         insetDescent = TEXT(bv)->height - row->ascent_of_text() +
227         TEXT_TO_INSET_OFFSET;
228         return insetDescent;
229 }
230
231
232 int InsetText::width(BufferView * bv, LyXFont const &) const
233 {
234         insetWidth = max(textWidth(bv),
235                          (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
236         return insetWidth;
237 }
238
239
240 int InsetText::textWidth(BufferView * bv) const
241 {
242         int const w = getMaxWidth(bv, this);
243         return w;
244 }
245
246
247 void InsetText::draw(BufferView * bv, LyXFont const & f,
248                      int baseline, float & x, bool cleared) const
249 {
250         if (nodraw())
251                 return;
252
253         Painter & pain = bv->painter();
254
255         // no draw is necessary !!!
256         if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
257                 top_x = int(x);
258                 top_baseline = baseline;
259                 x += width(bv, f);
260                 if (!cleared && (need_update & CLEAR_FRAME))
261                         clearFrame(pain, cleared);
262                 else if (cleared)
263                         frame_is_visible = false;
264                 need_update = NONE;
265                 return;
266         }
267
268         xpos = x;
269 #if 0
270         UpdatableInset::draw(bv, f, baseline, x, cleared);
271 #else
272         if (!owner())
273                 x += static_cast<float>(scroll());
274 #endif
275 #if 0
276         // update insetWidth and insetHeight with dummy calls
277         (void)ascent(bv, f);
278         (void)descent(bv, f);
279         (void)width(bv, f);
280 #endif
281
282         // if top_x differs we have a rule down and we don't have to clear anything
283         if (!cleared && (top_x == int(x)) &&
284                 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
285                  (last_drawn_width!=insetWidth)))
286         {
287                 int w =  insetWidth;
288                 int h = insetAscent + insetDescent;
289                 int ty = baseline - insetAscent;
290         
291                 if (ty < 0) {
292                         h += ty;
293                         ty = 0;
294                 }
295                 if ((ty + h) > pain.paperHeight())
296                         h = pain.paperHeight();
297                 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
298                         w = pain.paperWidth();
299                 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
300                 cleared = true;
301                 need_update = FULL;
302         }
303         if (cleared)
304                 frame_is_visible = false;
305
306         if (!cleared && (need_update == NONE))
307                 return;
308
309         if (top_x != int(x)) {
310                 if ((getMaxWidth(bv, this) > 0) && (TEXT(bv)->width != old_max_width)){
311                         resizeLyXText(bv);
312                         need_update |= FULL;
313                         old_max_width = TEXT(bv)->width;
314                         bv->text->status = LyXText::CHANGED_IN_DRAW;
315                 }
316                 top_x = int(x);
317 #if 1
318                 cleared = true;
319 #else
320                 return;
321 #endif
322         }
323
324 //      lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
325
326         if (cleared || (last_drawn_width != insetWidth)) {
327                 need_update |= FULL;
328                 last_drawn_width = insetWidth;
329         }
330
331         top_baseline = baseline;
332         top_y = baseline - ascent(bv, f);
333         last_width = width(bv, f);
334         last_height = ascent(bv, f) + descent(bv, f);
335
336         if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
337                 inset_x = cx(bv) - top_x + drawTextXOffset;
338                 inset_y = cy(bv) + drawTextYOffset;
339         }
340         if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection.set()) {
341                 drawFrame(pain, cleared);
342                 x += width(bv, f);
343                 need_update = NONE;
344                 return;
345         }
346         x += TEXT_TO_INSET_OFFSET;
347
348 #ifdef WITH_WARNINGS
349 #warning Jürgen, why is this a block of its own? (Lgb)
350 #warning because you told me to define variables only in local context (Jug)!
351 #warning then make it a function/method of its own. (Lgb)
352 #endif
353         {
354         int y = 0;
355         Row * row = TEXT(bv)->GetRowNearY(y);
356         int y_offset = baseline - row->ascent_of_text();
357         int ph = pain.paperHeight();
358         int first = 0;
359         y = y_offset;
360         while ((row != 0) && ((y+row->height()) <= 0)) {
361                 y += row->height();
362                 first += row->height();
363                 row = row->next();
364         }
365         if (y_offset < 0)
366                 y_offset = y;
367         TEXT(bv)->first = first;
368         if (cleared) {
369                 int yf = y_offset;
370                 y = 0;
371                 while ((row != 0) && (yf < ph)) {
372                         TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
373                                                 y+first, cleared);
374                         y += row->height();
375                         yf += row->height();
376                         row = row->next();
377                 }
378         } else if (!locked) {
379                 if (need_update & CURSOR) {
380                         bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
381                         TEXT(bv)->ClearSelection(bv);
382                         TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
383                 }
384                 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
385         } else {
386                 locked = false;
387                 if (need_update & SELECTION)
388                         bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
389                 else if (need_update & CURSOR) {
390                         bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
391                         TEXT(bv)->ClearSelection(bv);
392                         TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
393                 }
394                 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
395                 locked = true;
396         }
397         }
398         TEXT(bv)->refresh_y = 0;
399         TEXT(bv)->status = LyXText::UNCHANGED;
400         if ((need_update != CURSOR_PAR) &&
401                 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
402                 drawFrame(pain, cleared);
403         else if (need_update & CLEAR_FRAME)
404                 clearFrame(pain, cleared);
405         x += width(bv, f) - TEXT_TO_INSET_OFFSET;
406         if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
407                 need_update |= INIT;
408         } else if (need_update != INIT)
409                 need_update = NONE;
410 }
411
412
413 void InsetText::drawFrame(Painter & pain, bool cleared) const
414 {
415         if (!frame_is_visible || cleared) {
416                 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
417                                insetWidth - 1, insetAscent + insetDescent - 1,
418                                frame_color);
419                 frame_is_visible = true;
420         }
421 }
422
423
424 void InsetText::clearFrame(Painter & pain, bool cleared) const
425 {
426         if (frame_is_visible) {
427                 if (!cleared) {
428                         pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
429                                        insetWidth - 1, insetAscent + insetDescent - 1,
430                                        LColor::background);
431                 }
432                 frame_is_visible = false;
433         }
434 }
435
436
437 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
438 {
439         if (reinit) {
440                 need_update |= INIT;
441                 resizeLyXText(bv);
442                 if (owner())
443                         owner()->update(bv, font, true);
444                 return;
445         }
446         if (the_locking_inset) {
447                 inset_x = cx(bv) - top_x + drawTextXOffset;
448                 inset_y = cy(bv) + drawTextYOffset;
449                 the_locking_inset->update(bv, font, reinit);
450         }
451 #if 0
452         if (need_update == INIT) {
453                 resizeLyXText(bv);
454                 need_update |= FULL;
455         }
456 #endif
457         int oldw = insetWidth;
458         insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
459         if (oldw != insetWidth) {
460                 resizeLyXText(bv);
461                 need_update |= FULL;
462 //              update(bv, font, reinit);
463                 return;
464         }
465         if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
466                 the_locking_inset)
467         {
468                 TEXT(bv)->UpdateInset(bv, the_locking_inset);
469         }
470         
471         if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
472                 need_update |= FULL;
473 #if 0
474         int y_temp = 0;
475         Row * row = TEXT(bv)->GetRowNearY(y_temp);
476         insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
477         insetDescent = TEXT(bv)->height - row->ascent_of_text() +
478                 TEXT_TO_INSET_OFFSET;
479 #endif
480 }
481
482
483 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
484 {
485         need_update |= what;
486         if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
487                 need_update |= FULL;
488         else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
489                 need_update |= CURSOR_PAR;
490
491         // this to not draw a selection when we redraw all of it!
492         if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
493                 TEXT(bv)->ClearSelection(bv);
494 }
495
496
497 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
498 {
499         TEXT(bv)->FullRebreak(bv);
500         SetUpdateStatus(bv, what);
501         if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
502                 TEXT(bv)->selection.set())
503                 bv->updateInset(this, mark_dirty);
504         bv->owner()->showState();
505         if (old_par != cpar(bv)) {
506                 bv->owner()->setLayout(cpar(bv)->GetLayout());
507                 old_par = cpar(bv);
508         }
509 }
510
511
512 string const InsetText::EditMessage() const
513 {
514         return _("Opened Text Inset");
515 }
516
517
518 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
519 {
520 //    par->SetInsetOwner(this);
521         UpdatableInset::Edit(bv, x, y, button);
522
523         if (!bv->lockInset(this)) {
524                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
525                 return;
526         }
527         locked = true;
528         the_locking_inset = 0;
529         inset_pos = inset_x = inset_y = 0;
530         inset_boundary = false;
531         inset_par = 0;
532         old_par = 0;
533         int tmp_y = (y < 0)?0:y;
534         if (!checkAndActivateInset(bv, x, tmp_y, button))
535                 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
536                                                    y + insetAscent);
537         TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
538         bv->text->FinishUndo();
539         ShowInsetCursor(bv);
540         UpdateLocal(bv, CURSOR, false);
541
542         // If the inset is empty set the language of the current font to the
543         // language to the surronding text (if different).
544         if (par->size() == 0 && !par->next() &&
545                 bv->getParentLanguage(this) != TEXT(bv)->current_font.language())
546         {
547                 LyXFont font(LyXFont::ALL_IGNORE);
548                 font.setLanguage(bv->getParentLanguage(this));
549                 SetFont(bv, font, false);
550         }
551 }
552
553
554 void InsetText::InsetUnlock(BufferView * bv)
555 {
556         if (the_locking_inset) {
557                 the_locking_inset->InsetUnlock(bv);
558                 the_locking_inset = 0;
559         }
560         HideInsetCursor(bv);
561         no_selection = false;
562         locked = false;
563         int code = CURSOR|CLEAR_FRAME;
564         if (TEXT(bv)->selection.set()) {
565                 TEXT(bv)->ClearSelection(bv);
566                 code = FULL;
567         } else if (owner())
568                 bv->owner()->setLayout(owner()->getLyXText(bv)
569                                        ->cursor.par()->GetLayout());
570         else
571                 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
572         UpdateLocal(bv, code, false);
573 }
574
575
576 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
577 {
578         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
579                               << inset << "): ";
580         if (!inset)
581                 return false;
582         if (inset == cpar(bv)->GetInset(cpos(bv))) {
583                 lyxerr[Debug::INSETS] << "OK" << endl;
584                 the_locking_inset = inset;
585                 inset_x = cx(bv) - top_x + drawTextXOffset;
586                 inset_y = cy(bv) + drawTextYOffset;
587                 inset_pos = cpos(bv);
588                 inset_par = cpar(bv);
589                 inset_boundary = cboundary(bv);
590 #if 0
591                 TEXT(bv)->ClearSelection(bv);
592                 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
593                 TEXT(bv)->UpdateInset(bv, the_locking_inset);
594 #else
595                 UpdateLocal(bv, CURSOR, false);
596 #endif
597                 return true;
598         } else if (the_locking_inset && (the_locking_inset == inset)) {
599                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
600                         lyxerr[Debug::INSETS] << "OK" << endl;
601                         inset_x = cx(bv) - top_x + drawTextXOffset;
602                         inset_y = cy(bv) + drawTextYOffset;
603                 } else {
604                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
605                 }
606         } else if (the_locking_inset) {
607                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
608                 return the_locking_inset->LockInsetInInset(bv, inset);
609         }
610         lyxerr[Debug::INSETS] << "NOT OK" << endl;
611         return false;
612 }
613
614
615 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
616                                    bool lr)
617 {
618         if (!the_locking_inset)
619                 return false;
620         if (the_locking_inset == inset) {
621                 the_locking_inset->InsetUnlock(bv);
622                 TEXT(bv)->UpdateInset(bv, inset);
623                 the_locking_inset = 0;
624                 if (lr)
625                         moveRight(bv, false);
626                 old_par = 0; // force layout setting
627                 if (scroll())
628                         scroll(bv, 0.0F);
629                 else
630                         UpdateLocal(bv, CURSOR, false);
631                 return true;
632         }
633         return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
634 }
635
636
637 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
638 {
639         if (!the_locking_inset)
640                 return false;
641         if (the_locking_inset != inset) {
642                 TEXT(bv)->UpdateInset(bv, the_locking_inset);
643                 SetUpdateStatus(bv, CURSOR_PAR);
644                 return the_locking_inset->UpdateInsetInInset(bv, inset);
645         }
646 //    UpdateLocal(bv, FULL, false);
647         if (TEXT(bv)->UpdateInset(bv, inset))
648                 UpdateLocal(bv, CURSOR_PAR, false);
649         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
650                 inset_x = cx(bv) - top_x + drawTextXOffset;
651                 inset_y = cy(bv) + drawTextYOffset;
652         }
653         return true;
654 }
655
656
657 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
658 {
659         no_selection = true;
660
661         int tmp_x = x - drawTextXOffset;
662         int tmp_y = y + insetAscent - TEXT(bv)->first;
663         Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
664
665         HideInsetCursor(bv);
666         if (the_locking_inset) {
667                 if (the_locking_inset == inset) {
668                         the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
669                         no_selection = false;
670                         return;
671                 } else if (inset) {
672                         // otherwise unlock the_locking_inset and lock the new inset
673                         the_locking_inset->InsetUnlock(bv);
674                         inset_x = cx(bv) - top_x + drawTextXOffset;
675                         inset_y = cy(bv) + drawTextYOffset;
676                         the_locking_inset = static_cast<UpdatableInset*>(inset);
677                         inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
678                         inset->Edit(bv, x - inset_x, y - inset_y, button);
679                         if (the_locking_inset)
680                                 UpdateLocal(bv, CURSOR, false);
681                         no_selection = false;
682                         return;
683                 }
684                 // otherwise only unlock the_locking_inset
685                 the_locking_inset->InsetUnlock(bv);
686                 the_locking_inset = 0;
687         }
688         if (bv->theLockingInset()) {
689                 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
690                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
691                         inset_x = cx(bv) - top_x + drawTextXOffset;
692                         inset_y = cy(bv) + drawTextYOffset;
693                         inset_pos = cpos(bv);
694                         inset_par = cpar(bv);
695                         inset_boundary = cboundary(bv);
696                         the_locking_inset = uinset;
697                         uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
698                                                  button);
699                         uinset->Edit(bv, x - inset_x, y - inset_y, 0);
700                         if (the_locking_inset)
701                                 UpdateLocal(bv, CURSOR, false);
702                         no_selection = false;
703                         return;
704                 }
705         }
706         if (!inset) { // && (button == 2)) {
707                 bool paste_internally = false;
708                 if ((button == 2) && TEXT(bv)->selection.set()) {
709                         LocalDispatch(bv, LFUN_COPY, "");
710                         paste_internally = true;
711                 }
712                 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
713                                                    y + insetAscent);
714                 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
715                 UpdateLocal(bv, CURSOR, false);
716                 bv->owner()->setLayout(cpar(bv)->GetLayout());
717                 old_par = cpar(bv);
718                 // Insert primary selection with middle mouse
719                 // if there is a local selection in the current buffer,
720                 // insert this
721                 if (button == 2) {
722                         if (paste_internally)
723                                 LocalDispatch(bv, LFUN_PASTE, "");
724                         else
725                                 LocalDispatch(bv, LFUN_PASTESELECTION,
726                                               "paragraph");
727                 }
728         }
729         ShowInsetCursor(bv);
730         no_selection = false;
731 }
732
733
734 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
735 {
736         UpdatableInset * inset = 0;
737
738         if (the_locking_inset) {
739                 the_locking_inset->InsetButtonRelease(bv,
740                                                       x - inset_x, y - inset_y,
741                                                       button);
742         } else {
743                 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
744                         inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
745                         if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
746                                 inset->InsetButtonRelease(bv,
747                                                           x - inset_x,
748                                                           y - inset_y, button);
749                         } else {
750                                 inset_x = cx(bv) - top_x + drawTextXOffset;
751                                 inset_y = cy(bv) + drawTextYOffset;
752                                 inset->InsetButtonRelease(bv,
753                                                           x - inset_x,
754                                                           y - inset_y, button);
755                                 inset->Edit(bv,
756                                             x - inset_x, y - inset_y, button);
757                         }
758                         UpdateLocal(bv, CURSOR_PAR, false);
759                 }
760         }
761         no_selection = false;
762 }
763
764
765 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
766 {
767         if (no_selection)
768                 return;
769         if (the_locking_inset) {
770                 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
771                                                      y - inset_y,state);
772                 return;
773         }
774         HideInsetCursor(bv);
775         TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
776                                            y + insetAscent);
777         TEXT(bv)->SetSelection(bv);
778         if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
779                 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
780                 UpdateLocal(bv, SELECTION, false);
781         ShowInsetCursor(bv);
782 }
783
784
785 void InsetText::InsetKeyPress(XKeyEvent * xke)
786 {
787         if (the_locking_inset) {
788                 the_locking_inset->InsetKeyPress(xke);
789                 return;
790         }
791 }
792
793
794 UpdatableInset::RESULT
795 InsetText::LocalDispatch(BufferView * bv,
796                          kb_action action, string const & arg)
797 {
798         no_selection = false;
799         UpdatableInset::RESULT
800                 result= UpdatableInset::LocalDispatch(bv, action, arg);
801         if (result != UNDISPATCHED) {
802                 return DISPATCHED;
803         }
804
805         result = DISPATCHED;
806         if ((action < 0) && arg.empty())
807                 return FINISHED;
808
809         if (the_locking_inset) {
810                 result = the_locking_inset->LocalDispatch(bv, action, arg);
811                 if (result == DISPATCHED_NOUPDATE)
812                         return result;
813                 else if (result == DISPATCHED) {
814                         UpdateLocal(bv, CURSOR_PAR, false);
815                         return result;
816                 } else if (result == FINISHED) {
817                         bool dispatched = false;
818                         switch (action) {
819                         case LFUN_UNKNOWN_ACTION:
820                         case LFUN_BREAKPARAGRAPH:
821                         case LFUN_BREAKLINE:
822                                 moveRightIntern(bv, false, false);
823                                 break;
824                         case LFUN_RIGHT:
825                                 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
826                                         moveRightIntern(bv, false, false);
827                                 dispatched = true;
828                                 break;
829                         case LFUN_LEFT:
830                                 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
831                                         moveRightIntern(bv, false, false);
832                                 dispatched = true;
833                                 break;
834                         default:
835                                 break;
836                         }
837                         the_locking_inset = 0;
838                         if (dispatched)
839                                 return DISPATCHED;
840                 }
841         }
842         HideInsetCursor(bv);
843         switch (action) {
844         // Normal chars
845         case LFUN_UNKNOWN_ACTION:
846                 if (bv->buffer()->isReadonly()) {
847 //          setErrorMessage(N_("Document is read only"));
848                         break;
849                 }
850                 if (!arg.empty()) {
851                         /* Automatically delete the currently selected
852                          * text and replace it with what is being
853                          * typed in now. Depends on lyxrc settings
854                          * "auto_region_delete", which defaults to
855                          * true (on). */
856
857                         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
858                                           bv->text->cursor.par()->previous(),
859                                           bv->text->cursor.par()->next());
860                         bv->setState();
861                         if (lyxrc.auto_region_delete) {
862                                 if (TEXT(bv)->selection.set()) {
863                                         TEXT(bv)->CutSelection(bv, false);
864                                 }
865                         }
866                         TEXT(bv)->ClearSelection(bv);
867                         for (string::size_type i = 0; i < arg.length(); ++i) {
868                                 if (greek_kb_flag) {
869                                         if (!math_insert_greek(bv, arg[i])) {
870                                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
871                                         } else if (!the_locking_inset) {
872                                                 (void)moveRight(bv, false);
873                                         }
874                                 } else {
875                                         bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
876                                 }
877                         }
878                 }
879                 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
880                 UpdateLocal(bv, CURSOR_PAR, true);
881                 result=DISPATCHED_NOUPDATE;
882                 break;
883                 // --- Cursor Movements -----------------------------------
884         case LFUN_RIGHTSEL:
885                 bv->text->FinishUndo();
886                 moveRight(bv, false, true);
887                 TEXT(bv)->SetSelection(bv);
888                 UpdateLocal(bv, SELECTION, false);
889                 break;
890         case LFUN_RIGHT:
891                 result = moveRight(bv);
892                 bv->text->FinishUndo();
893                 UpdateLocal(bv, CURSOR, false);
894                 break;
895         case LFUN_LEFTSEL:
896                 bv->text->FinishUndo();
897                 moveLeft(bv, false, true);
898                 TEXT(bv)->SetSelection(bv);
899                 UpdateLocal(bv, SELECTION, false);
900                 break;
901         case LFUN_LEFT:
902                 bv->text->FinishUndo();
903                 result = moveLeft(bv);
904                 UpdateLocal(bv, CURSOR, false);
905                 break;
906         case LFUN_DOWNSEL:
907                 bv->text->FinishUndo();
908                 moveDown(bv);
909                 TEXT(bv)->SetSelection(bv);
910                 UpdateLocal(bv, SELECTION, false);
911                 break;
912         case LFUN_DOWN:
913                 bv->text->FinishUndo();
914                 result = moveDown(bv);
915                 UpdateLocal(bv, CURSOR, false);
916                 break;
917         case LFUN_UPSEL:
918                 bv->text->FinishUndo();
919                 moveUp(bv);
920                 TEXT(bv)->SetSelection(bv);
921                 UpdateLocal(bv, SELECTION, false);
922                 break;
923         case LFUN_UP:
924                 bv->text->FinishUndo();
925                 result = moveUp(bv);
926                 UpdateLocal(bv, CURSOR, false);
927                 break;
928         case LFUN_HOME:
929                 bv->text->FinishUndo();
930                 TEXT(bv)->CursorHome(bv);
931                 UpdateLocal(bv, CURSOR, false);
932                 break;
933         case LFUN_END:
934                 TEXT(bv)->CursorEnd(bv);
935                 UpdateLocal(bv, CURSOR, false);
936                 break;
937         case LFUN_BACKSPACE:
938                 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
939                                   bv->text->cursor.par()->previous(),
940                                   bv->text->cursor.par()->next());
941                 if (TEXT(bv)->selection.set())
942                         TEXT(bv)->CutSelection(bv);
943                 else
944                         TEXT(bv)->Backspace(bv);
945                 UpdateLocal(bv, CURSOR_PAR, true);
946                 break;
947         case LFUN_DELETE:
948                 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
949                                   bv->text->cursor.par()->previous(),
950                                   bv->text->cursor.par()->next());
951                 if (TEXT(bv)->selection.set())
952                         TEXT(bv)->CutSelection(bv);
953                 else
954                         TEXT(bv)->Delete(bv);
955                 UpdateLocal(bv, CURSOR_PAR, true);
956                 break;
957         case LFUN_CUT:
958                 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
959                                   bv->text->cursor.par()->previous(),
960                                   bv->text->cursor.par()->next());
961                 TEXT(bv)->CutSelection(bv);
962                 UpdateLocal(bv, CURSOR_PAR, true);
963                 break;
964         case LFUN_COPY:
965                 bv->text->FinishUndo();
966                 TEXT(bv)->CopySelection(bv);
967                 UpdateLocal(bv, CURSOR_PAR, false);
968                 break;
969         case LFUN_PASTESELECTION:
970         {
971                 string const clip(bv->getClipboard());
972         
973                 if (clip.empty())
974                         break;
975                 if (arg == "paragraph") {
976                         TEXT(bv)->InsertStringB(bv, clip);
977                 } else {
978                         TEXT(bv)->InsertStringA(bv, clip);
979                 }
980                 UpdateLocal(bv, CURSOR_PAR, true);
981                 break;
982         }
983         case LFUN_PASTE:
984                 if (!autoBreakRows) {
985                         CutAndPaste cap;
986
987                         if (cap.nrOfParagraphs() > 1) {
988                                 WriteAlert(_("Impossible operation"),
989                                                    _("Cannot include more than one paragraph!"),
990                                                    _("Sorry."));
991                                 break;
992                         }
993                 }
994                 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
995                                   bv->text->cursor.par()->previous(),
996                                   bv->text->cursor.par()->next());
997                 TEXT(bv)->PasteSelection(bv);
998                 UpdateLocal(bv, CURSOR_PAR, true);
999                 break;
1000         case LFUN_BREAKPARAGRAPH:
1001                 if (!autoBreakRows)
1002                         return DISPATCHED;
1003                 TEXT(bv)->BreakParagraph(bv, 0);
1004                 UpdateLocal(bv, FULL, true);
1005                 break;
1006         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1007                 if (!autoBreakRows)
1008                         return DISPATCHED;
1009                 TEXT(bv)->BreakParagraph(bv, 1);
1010                 UpdateLocal(bv, FULL, true);
1011                 break;
1012         case LFUN_BREAKLINE:
1013                 if (!autoBreakRows)
1014                         return DISPATCHED;
1015                 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1016                                   bv->text->cursor.par()->previous(),
1017                                   bv->text->cursor.par()->next());
1018                 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1019                 UpdateLocal(bv, CURSOR_PAR, true);
1020                 break;
1021         case LFUN_LAYOUT:
1022                 // do not set layouts on non breakable textinsets
1023                 if (autoBreakRows) {
1024                         LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1025           
1026                         // Derive layout number from given argument (string)
1027                         // and current buffer's textclass (number). */    
1028                         LyXTextClassList::ClassList::size_type tclass =
1029                                 bv->buffer()->params.textclass;
1030                         std::pair <bool, LyXTextClass::size_type> layout = 
1031                                 textclasslist.NumberOfLayout(tclass, arg);
1032
1033                         // If the entry is obsolete, use the new one instead.
1034                         if (layout.first) {
1035                                 string obs = textclasslist.Style(tclass,layout.second).
1036                                         obsoleted_by();
1037                                 if (!obs.empty()) 
1038                                         layout = textclasslist.NumberOfLayout(tclass, obs);
1039                         }
1040
1041                         // see if we found the layout number:
1042                         if (!layout.first) {
1043                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1044                                 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1045                                 break;
1046                         }
1047
1048                         if (cur_layout != layout.second) {
1049                                 cur_layout = layout.second;
1050                                 TEXT(bv)->SetLayout(bv, layout.second);
1051                                 bv->owner()->setLayout(cpar(bv)->GetLayout());
1052                                 UpdateLocal(bv, CURSOR_PAR, true);
1053                         }
1054                 } else {
1055                         // reset the layout box
1056                         bv->owner()->setLayout(cpar(bv)->GetLayout());
1057                 }
1058                 break;
1059         case LFUN_PARAGRAPH_SPACING:
1060                 // This one is absolutely not working. When fiddling with this
1061                 // it also seems to me that the paragraphs inside the insettext
1062                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1063         {
1064                 LyXParagraph * par = TEXT(bv)->cursor.par();
1065                 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1066                 float cur_value = 1.0;
1067                 if (cur_spacing == Spacing::Other) {
1068                         cur_value = par->params.spacing().getValue();
1069                 }
1070                                 
1071                 std::istringstream istr(arg.c_str());
1072                 string tmp;
1073                 istr >> tmp;
1074                 Spacing::Space new_spacing = cur_spacing;
1075                 float new_value = cur_value;
1076                 if (tmp.empty()) {
1077                         lyxerr << "Missing argument to `paragraph-spacing'"
1078                                    << endl;
1079                 } else if (tmp == "single") {
1080                         new_spacing = Spacing::Single;
1081                 } else if (tmp == "onehalf") {
1082                         new_spacing = Spacing::Onehalf;
1083                 } else if (tmp == "double") {
1084                         new_spacing = Spacing::Double;
1085                 } else if (tmp == "other") {
1086                         new_spacing = Spacing::Other;
1087                         float tmpval = 0.0;
1088                         istr >> tmpval;
1089                         lyxerr << "new_value = " << tmpval << endl;
1090                         if (tmpval != 0.0)
1091                                 new_value = tmpval;
1092                 } else if (tmp == "default") {
1093                         new_spacing = Spacing::Default;
1094                 } else {
1095                         lyxerr << _("Unknown spacing argument: ")
1096                                    << arg << endl;
1097                 }
1098                 if (cur_spacing != new_spacing || cur_value != new_value) {
1099                         par->params.spacing(Spacing(new_spacing, new_value));
1100                         //TEXT(bv)->RedoParagraph(owner->view());
1101                         UpdateLocal(bv, CURSOR_PAR, true);
1102                         //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1103                 }
1104         }
1105         break;
1106         
1107         default:
1108                 if (!bv->Dispatch(action, arg))
1109                         result = UNDISPATCHED;
1110                 break;
1111         }
1112
1113         /// If the action has deleted all text in the inset, we need to change the
1114         // language to the language to the surronding text.
1115         if (par->size() == 0 && !par->next()) {
1116                 LyXFont font(LyXFont::ALL_IGNORE);
1117                 font.setLanguage(bv->getParentLanguage(this));
1118                 SetFont(bv, font, false);
1119         }
1120
1121         if (result != FINISHED) {
1122                 ShowInsetCursor(bv);
1123         } else
1124                 bv->unlockInset(this);
1125         return result;
1126 }
1127
1128
1129 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1130 {
1131         TexRow texrow;
1132         buf->latexParagraphs(os, par, 0, texrow);
1133         return texrow.rows();
1134 }
1135
1136
1137 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1138 {
1139         LyXParagraph * p = par;
1140         unsigned int lines = 0;
1141         
1142         while (p) {
1143                 string const tmp = buf->asciiParagraph(p, linelen);
1144                 lines += countChar(tmp, '\n');
1145                 os << tmp;
1146                 p = p->next();
1147         }
1148         return lines;
1149 }
1150
1151
1152 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1153 {
1154         LyXParagraph * p = par;
1155         unsigned int lines = 0;
1156         int desc = 0;
1157         
1158         string tmp;
1159         while (p) {
1160                 buf->SimpleDocBookOnePar(os, tmp, p, desc, 0);
1161                 p = p->next();
1162         }
1163         
1164         return lines;
1165 }
1166
1167
1168 void InsetText::Validate(LaTeXFeatures & features) const
1169 {
1170         LyXParagraph * p = par;
1171         while (p) {
1172                 p->validate(features);
1173                 p = p->next();
1174         }
1175 }
1176
1177
1178 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1179 {
1180         if (textclasslist.Style(buf->params.textclass,
1181                                 p->GetLayout()).labeltype != LABEL_MANUAL)
1182                 return 0;
1183         else
1184                 return p->BeginningOfMainBody();
1185 }
1186
1187
1188 void InsetText::GetCursorPos(BufferView * bv,
1189                              int & x, int & y) const
1190 {
1191         x = cx(bv);
1192         y = cy(bv);
1193 }
1194
1195
1196 unsigned int InsetText::InsetInInsetY()
1197 {
1198         if (!the_locking_inset)
1199                 return 0;
1200
1201         return (inset_y + the_locking_inset->InsetInInsetY());
1202 }
1203
1204
1205 void InsetText::ToggleInsetCursor(BufferView * bv)
1206 {
1207         if (the_locking_inset) {
1208                 the_locking_inset->ToggleInsetCursor(bv);
1209                 return;
1210         }
1211
1212         LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1213
1214         int const asc = lyxfont::maxAscent(font);
1215         int const desc = lyxfont::maxDescent(font);
1216   
1217         if (isCursorVisible())
1218                 bv->hideLockedInsetCursor();
1219         else
1220                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1221         toggleCursorVisible();
1222 }
1223
1224
1225 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1226 {
1227         if (the_locking_inset) {
1228                 the_locking_inset->ShowInsetCursor(bv);
1229                 return;
1230         }
1231         if (!isCursorVisible()) {
1232                 LyXFont const font =
1233                         TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1234         
1235                 int const asc = lyxfont::maxAscent(font);
1236                 int const desc = lyxfont::maxDescent(font);
1237
1238                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1239                 if (show)
1240                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1241                 setCursorVisible(true);
1242         }
1243 }
1244
1245
1246 void InsetText::HideInsetCursor(BufferView * bv)
1247 {
1248         if (isCursorVisible()) {
1249                 bv->hideLockedInsetCursor();
1250                 setCursorVisible(false);
1251         }
1252         if (the_locking_inset)
1253                 the_locking_inset->HideInsetCursor(bv);
1254 }
1255
1256
1257 UpdatableInset::RESULT
1258 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1259 {
1260         if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1261                 return moveLeftIntern(bv, false, activate_inset, selecting);
1262         else
1263                 return moveRightIntern(bv, false, activate_inset, selecting);
1264 }
1265
1266
1267 UpdatableInset::RESULT
1268 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1269 {
1270         if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1271                 return moveRightIntern(bv, true, activate_inset, selecting);
1272         else
1273                 return moveLeftIntern(bv, true, activate_inset, selecting);
1274 }
1275
1276
1277 UpdatableInset::RESULT
1278 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1279                            bool activate_inset, bool selecting)
1280 {
1281         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1282                 return FINISHED;
1283         if (activate_inset && checkAndActivateInset(bv, behind))
1284                 return DISPATCHED;
1285         TEXT(bv)->CursorRight(bv);
1286         if (!selecting)
1287                 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1288         return DISPATCHED_NOUPDATE;
1289 }
1290
1291
1292 UpdatableInset::RESULT
1293 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1294                           bool activate_inset, bool selecting)
1295 {
1296         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1297                 return FINISHED;
1298         TEXT(bv)->CursorLeft(bv);
1299         if (!selecting)
1300                 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1301         if (activate_inset && checkAndActivateInset(bv, behind))
1302                 return DISPATCHED;
1303         return DISPATCHED_NOUPDATE;
1304 }
1305
1306
1307 UpdatableInset::RESULT
1308 InsetText::moveUp(BufferView * bv)
1309 {
1310         if (!crow(bv)->previous())
1311                 return FINISHED;
1312         TEXT(bv)->CursorUp(bv);
1313         return DISPATCHED_NOUPDATE;
1314 }
1315
1316
1317 UpdatableInset::RESULT
1318 InsetText::moveDown(BufferView * bv)
1319 {
1320         if (!crow(bv)->next())
1321                 return FINISHED;
1322         TEXT(bv)->CursorDown(bv);
1323         return DISPATCHED_NOUPDATE;
1324 }
1325
1326
1327 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1328 {
1329         if (the_locking_inset) {
1330                 if (the_locking_inset->InsertInsetAllowed(inset))
1331                         return the_locking_inset->InsertInset(bv, inset);
1332                 return false;
1333         }
1334         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1335                           bv->text->cursor.par()->previous(),
1336                           bv->text->cursor.par()->next());
1337         inset->setOwner(this);
1338         HideInsetCursor(bv);
1339         TEXT(bv)->InsertInset(bv, inset);
1340 #if 0
1341         if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1342                 (cpar(bv)->GetInset(cpos(bv)) != inset))
1343                 TEXT(bv)->CursorLeft(bv);
1344 #endif
1345         bv->fitCursor(TEXT(bv));
1346         UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1347         ShowInsetCursor(bv);
1348         return true;
1349 }
1350
1351
1352 UpdatableInset * InsetText::GetLockingInset()
1353 {
1354         return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1355 }
1356
1357
1358 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1359 {
1360         if (c == LyxCode())
1361                 return this;
1362         if (the_locking_inset)
1363                 return the_locking_inset->GetFirstLockingInsetOfType(c);
1364         return 0;
1365 }
1366
1367
1368 bool InsetText::ShowInsetDialog(BufferView * bv) const
1369 {
1370         if (the_locking_inset)
1371                 return the_locking_inset->ShowInsetDialog(bv);
1372         return false;
1373 }
1374
1375
1376 std::vector<string> const InsetText::getLabelList() const 
1377 {
1378         std::vector<string> label_list;
1379
1380         LyXParagraph * tpar = par;
1381         while (tpar) {
1382                 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1383                 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1384                 for (; beg != end; ++beg) {
1385                         std::vector<string> const l = (*beg)->getLabelList();
1386                         label_list.insert(label_list.end(), l.begin(), l.end());
1387                 }
1388                 tpar = tpar->next();
1389         }
1390         return label_list;
1391 }
1392
1393
1394 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall,
1395                         bool selectall)
1396 {
1397         if (TEXT(bv)->selection.set()) {
1398                 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1399                                   bv->text->cursor.par()->previous(),
1400                                   bv->text->cursor.par()->next());
1401         }
1402         if (selectall)
1403                 selectAll(bv);
1404         TEXT(bv)->SetFont(bv, font, toggleall);
1405         if (selectall)
1406                 TEXT(bv)->ClearSelection(bv);
1407         bv->fitCursor(TEXT(bv));
1408         if (selectall || TEXT(bv)->selection.set())
1409                 UpdateLocal(bv, FULL, true);
1410         else
1411                 UpdateLocal(bv, CURSOR_PAR, true);
1412 }
1413
1414
1415 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1416 {
1417         if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1418                 unsigned int x;
1419                 unsigned int y;
1420                 Inset * inset =
1421                         static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1422                 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1423                         return false;
1424                 LyXFont const font =
1425                         TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1426                 if (behind) {
1427                         x = inset->width(bv, font);
1428                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1429                 } else {
1430                         x = 0;
1431                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1432                 }
1433                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1434                 //inset_y = cy(bv) + drawTextYOffset;
1435                 inset->Edit(bv, x, y, 0);
1436                 if (!the_locking_inset)
1437                         return false;
1438                 UpdateLocal(bv, CURSOR, false);
1439                 return true;
1440         }
1441         return false;
1442 }
1443
1444
1445 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1446                                       int button)
1447 {
1448         int dummyx, dummyy;
1449
1450         dummyx = x = x - drawTextXOffset;
1451         dummyy = y + insetAscent;
1452         Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1453
1454         if (inset) {
1455                 if (x < 0)
1456                         x = insetWidth;
1457                 if (y < 0)
1458                         y = insetDescent;
1459                 inset_x = cx(bv) - top_x + drawTextXOffset;
1460                 inset_y = cy(bv) + drawTextYOffset;
1461                 inset->Edit(bv, x - inset_x, y - inset_y, button);
1462                 if (!the_locking_inset)
1463                         return false;
1464                 UpdateLocal(bv, CURSOR, false);
1465                 return true;
1466         }
1467         return false;
1468 }
1469
1470
1471 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1472 {
1473         int w = UpdatableInset::getMaxWidth(bv, inset);
1474         if (w < 0) {
1475                 return w;
1476         }
1477         if (owner()) {
1478                 w = w - top_x + owner()->x();
1479                 return w;
1480         }
1481         w -= (2 * TEXT_TO_INSET_OFFSET);
1482         return w - top_x;
1483 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1484 }
1485
1486
1487 void InsetText::SetParagraphData(LyXParagraph * p)
1488 {
1489         // delete all instances of LyXText before deleting the paragraps used
1490         // by it.
1491         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1492                 delete (*cit).second;
1493                 (*cit).second = 0;
1494         }
1495
1496         while (par) {
1497                 LyXParagraph * tmp = par->next();
1498                 delete par;
1499                 par = tmp;
1500         }
1501
1502         par = new LyXParagraph(*p);
1503         par->SetInsetOwner(this);
1504         LyXParagraph * np = par;
1505         while (p->next()) {
1506                 p = p->next();
1507                 np->next(new LyXParagraph(*p));
1508                 np->next()->previous(np);
1509                 np = np->next();
1510                 np->SetInsetOwner(this);
1511         }
1512
1513         need_update = INIT;
1514 }
1515
1516
1517 void InsetText::SetText(string const & data)
1518 {
1519         clear();
1520         LyXFont font(LyXFont::ALL_SANE);
1521         for (unsigned int i=0; i < data.length(); ++i)
1522                 par->InsertChar(i, data[i], font);
1523 }
1524
1525
1526 void InsetText::SetAutoBreakRows(bool flag)
1527 {
1528         if (flag != autoBreakRows) {
1529                 autoBreakRows = flag;
1530                 need_update = FULL;
1531                 if (!flag)
1532                         removeNewlines();
1533         }
1534 }
1535
1536
1537 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1538 {
1539         if (how != drawFrame_) {
1540                 drawFrame_ = how;
1541                 if (bv)
1542                         UpdateLocal(bv, DRAW_FRAME, false);
1543         }
1544 }
1545
1546
1547 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1548 {
1549         if (frame_color != col) {
1550                 frame_color = col;
1551                 if (bv)
1552                         UpdateLocal(bv, DRAW_FRAME, false);
1553         }
1554 }
1555
1556
1557 int InsetText::cx(BufferView * bv) const
1558 {
1559         LyXText * text = TEXT(bv);
1560         int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1561         if (the_locking_inset) {
1562                 LyXFont font = text->GetFont(bv->buffer(),
1563                                              text->cursor.par(),
1564                                              text->cursor.pos());
1565                 if (font.isVisibleRightToLeft())
1566                         x -= the_locking_inset->width(bv, font);
1567         }
1568         return x;
1569 }
1570
1571
1572 int InsetText::cy(BufferView * bv) const
1573 {
1574         LyXFont font;
1575         return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1576 }
1577
1578
1579 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1580 {
1581         return TEXT(bv)->cursor.pos();
1582 }
1583
1584
1585 LyXParagraph * InsetText::cpar(BufferView * bv) const
1586 {
1587         return TEXT(bv)->cursor.par();
1588 }
1589
1590
1591 bool InsetText::cboundary(BufferView * bv) const
1592 {
1593         return TEXT(bv)->cursor.boundary();
1594 }
1595
1596
1597 Row * InsetText::crow(BufferView * bv) const
1598 {
1599         return TEXT(bv)->cursor.row();
1600 }
1601
1602
1603 LyXText * InsetText::getLyXText(BufferView const * lbv,
1604                                 bool const recursive) const
1605 {
1606         // Super UGLY! (Lgb)
1607         BufferView * bv = const_cast<BufferView *>(lbv);
1608         
1609         if ((cache.find(bv) != cache.end()) && cache[bv]) {
1610                 if (recursive && the_locking_inset)
1611                         return the_locking_inset->getLyXText(bv);
1612                 return cache[bv];
1613         }
1614         LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1615         lt->init(bv);
1616         cache[bv] = lt;
1617         if (the_locking_inset) {
1618                 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1619                 if (recursive)
1620                         return the_locking_inset->getLyXText(bv);
1621         }
1622         return lt;
1623 }
1624
1625
1626 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1627 {
1628         if ((cache.find(bv) == cache.end()) || !cache[bv])
1629                 return;
1630         delete cache[bv];
1631         cache.erase(bv);
1632         if (recursive) {
1633                 /// then remove all LyXText in text-insets
1634                 LyXParagraph * p = par;
1635                 for (; p; p = p->next()) {
1636                         p->deleteInsetsLyXText(bv);
1637                 }
1638         }
1639 }
1640
1641
1642 void InsetText::resizeLyXText(BufferView * bv) const
1643 {
1644         if (!par->next() && !par->size()) // no data, resize not neccessary!
1645                 return;
1646         if (getMaxWidth(bv, this) < 0) // one endless line, no resize necessary
1647                 return;
1648         if ((cache.find(bv) == cache.end()) || !cache[bv])
1649                 return;
1650
1651         LyXParagraph * lpar = 0;
1652         LyXParagraph * selstartpar = 0;
1653         LyXParagraph * selendpar = 0;
1654         LyXParagraph::size_type pos = 0;
1655         LyXParagraph::size_type selstartpos = 0;
1656         LyXParagraph::size_type selendpos = 0;
1657         bool boundary = false;
1658         bool selstartboundary = false;
1659         bool selendboundary = false;
1660         bool selection = false;
1661         bool mark_set = false;
1662
1663 //    ProhibitInput(bv);
1664
1665         if (locked) {
1666                 lpar = TEXT(bv)->cursor.par();
1667                 pos = TEXT(bv)->cursor.pos();
1668                 boundary = TEXT(bv)->cursor.boundary();
1669                 selstartpar = TEXT(bv)->selection.start.par();
1670                 selstartpos = TEXT(bv)->selection.start.pos();
1671                 selstartboundary = TEXT(bv)->selection.start.boundary();
1672                 selendpar = TEXT(bv)->selection.end.par();
1673                 selendpos = TEXT(bv)->selection.end.pos();
1674                 selendboundary = TEXT(bv)->selection.end.boundary();
1675                 selection = TEXT(bv)->selection.set();
1676                 mark_set = TEXT(bv)->selection.mark();
1677         }
1678         deleteLyXText(bv, (the_locking_inset == 0));
1679
1680         if (lpar) {
1681                 TEXT(bv)->selection.set(true);
1682                 /* at this point just to avoid the Delete-Empty-Paragraph
1683                  * Mechanism when setting the cursor */
1684                 TEXT(bv)->selection.mark(mark_set);
1685                 if (selection) {
1686                         TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1687                                             true, selstartboundary);
1688                         TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1689                         TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1690                                             true, selendboundary);
1691                         TEXT(bv)->SetSelection(bv);
1692                         TEXT(bv)->SetCursor(bv, lpar, pos);
1693                 } else {
1694                         TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1695                         TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1696                         TEXT(bv)->selection.set(false);
1697                 }
1698         }
1699         if (bv->screen())
1700                 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1701         // this will scroll the screen such that the cursor becomes visible 
1702         bv->updateScrollbar();
1703 //    AllowInput(bv);
1704         if (the_locking_inset) {
1705                 /// then resize all LyXText in text-insets
1706                 inset_x = cx(bv) - top_x + drawTextXOffset;
1707                 inset_y = cy(bv) + drawTextYOffset;
1708                 for (LyXParagraph * p = par; p; p = p->next()) {
1709                         p->resizeInsetsLyXText(bv);
1710                 }
1711         }
1712         need_update = FULL;
1713 }
1714
1715
1716 void InsetText::removeNewlines()
1717 {
1718         for (LyXParagraph * p = par; p; p = p->next()) {
1719                 for (int i = 0; i < p->size(); ++i) {
1720                         if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1721                                 p->Erase(i);
1722                 }
1723         }
1724 }
1725
1726
1727 bool InsetText::nodraw() const
1728 {
1729         if (the_locking_inset)
1730                 return the_locking_inset->nodraw();
1731         return UpdatableInset::nodraw();
1732 }
1733
1734
1735 int InsetText::scroll(bool recursive) const
1736 {
1737         int sx = UpdatableInset::scroll(false);
1738
1739         if (recursive && the_locking_inset)
1740                 sx += the_locking_inset->scroll(recursive);
1741
1742         return sx;
1743 }
1744
1745
1746 bool InsetText::doClearArea() const
1747 {
1748         return !locked || (need_update & (FULL|INIT));
1749 }
1750
1751 void InsetText::selectAll(BufferView * bv)
1752 {
1753         TEXT(bv)->CursorTop(bv);
1754         TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1755         TEXT(bv)->CursorBottom(bv);
1756         TEXT(bv)->SetSelection(bv);
1757 }
1758
1759 void InsetText::clearSelection(BufferView * bv)
1760 {
1761         TEXT(bv)->ClearSelection(bv);
1762 }
1763
1764 /* Emacs:
1765  * Local variables:
1766  * tab-width: 4
1767  * End:
1768  * vi:set tabstop=4:
1769  */