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