2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
32 #include "support/textutils.h"
34 #include "insetlatexaccent.h"
35 #include "insetquotes.h"
36 #include "mathed/formulamacro.h"
38 #include "insetinfo.h"
39 #include "insetinclude.h"
41 #include "insetcommand.h"
42 #include "insetindex.h"
43 #include "insetlabel.h"
45 //#include "insettabular.h"
47 #include "insetspecialchar.h"
48 #include "LaTeXFeatures.h"
50 #include "lyx_gui_misc.h"
51 #include "support/LAssert.h"
53 extern unsigned char getCurrentTextClass(Buffer *);
55 InsetText::InsetText(Buffer * buf)
57 par = new LyXParagraph();
58 the_locking_inset = 0;
60 cursor_visible = false;
61 maxWidth = old_x = -1;
62 actpos = selection_start = selection_end = 0;
69 InsetText::InsetText(InsetText const & ins, Buffer * buf)
71 par = new LyXParagraph(ins.par);
72 the_locking_inset = 0;
74 cursor_visible = false;
75 maxWidth = old_x = -1;
76 actpos = selection_start = selection_end = 0;
83 InsetText::~InsetText()
89 InsetText * InsetText::Clone() const
91 InsetText * t = new InsetText(*this, buffer);
96 void InsetText::Write(ostream & os) const
99 WriteParagraphData(os);
103 void InsetText::WriteParagraphData(ostream & os) const
105 LyXFont font1 = LyXFont(LyXFont::ALL_INHERIT);
110 for (int i = 0; i < par->Last(); ++i) {
111 // Write font changes
112 font2 = par->GetFontSettings(i);
113 if (font2 != font1) {
114 font2.lyxWriteChanges(font1, os);
119 // A newline before tags
121 (c == LyXParagraph::META_INSET ||
122 c == LyXParagraph::META_NEWLINE ||
129 case LyXParagraph::META_INSET: {
130 Inset * inset = par->GetInset(i);
132 os << "\n\\begin_inset ";
134 os << "\n\\end_inset \n\n";
139 case LyXParagraph::META_NEWLINE:
144 os << "\\backslash\n";
148 if (column > 65 && c==' ') {
152 // this check is to amend a bug. LyX sometimes
153 // inserts '\0' this could cause problems.
157 lyxerr << "ERROR (InsetText::writeFile):"
158 " '\\0' char in structure.\n";
163 // A newline if the last c was not a tag.
165 (c != LyXParagraph::META_INSET &&
166 c != LyXParagraph::META_NEWLINE &&
174 void InsetText::Read(LyXLex & lex)
176 string token, tmptok;
177 LyXFont font = LyXFont(LyXFont::ALL_INHERIT);
181 par = new LyXParagraph;
185 token = lex.GetString();
188 else if (token[0] != '\\') {
189 int n = token.length();
190 for (int i = 0; i < n; ++i) {
191 par->InsertChar(pos, token[i]);
192 par->SetFont(pos, font);
195 } else if (token == "\\newline") {
196 par->InsertChar(pos, LyXParagraph::META_NEWLINE);
197 par->SetFont(pos, font);
199 } else if (token == "\\family") {
201 font.setLyXFamily(lex.GetString());
202 } else if (token == "\\series") {
204 font.setLyXSeries(lex.GetString());
205 } else if (token == "\\shape") {
207 font.setLyXShape(lex.GetString());
208 } else if (token == "\\size") {
210 font.setLyXSize(lex.GetString());
211 } else if (token == "\\latex") {
213 string tok = lex.GetString();
214 // This is dirty, but gone with LyX3. (Asger)
215 if (tok == "no_latex")
216 font.setLatex(LyXFont::OFF);
217 else if (tok == "latex")
218 font.setLatex(LyXFont::ON);
219 else if (tok == "default")
220 font.setLatex(LyXFont::INHERIT);
222 lex.printError("Unknown LaTeX font flag "
224 } else if (token == "\\direction") {
226 string tok = lex.GetString();
228 font.setDirection(LyXFont::LTR_DIR);
229 else if (tok == "rtl")
230 font.setDirection(LyXFont::RTL_DIR);
231 else if (tok == "default")
232 font.setDirection(LyXFont::INHERIT_DIR);
234 lex.printError("Unknown font flag "
236 } else if (token == "\\emph") {
238 font.setEmph(font.setLyXMisc(lex.GetString()));
239 } else if (token == "\\bar") {
241 string tok = lex.GetString();
242 // This is dirty, but gone with LyX3. (Asger)
244 font.setUnderbar(LyXFont::ON);
245 else if (tok == "no")
246 font.setUnderbar(LyXFont::OFF);
247 else if (tok == "default")
248 font.setUnderbar(LyXFont::INHERIT);
250 lex.printError("Unknown bar font flag "
252 } else if (token == "\\noun") {
254 font.setNoun(font.setLyXMisc(lex.GetString()));
255 } else if (token == "\\color") {
257 font.setLyXColor(lex.GetString());
258 } else if (token == "\\begin_inset") {
261 tmptok = lex.GetString();
262 // Test the different insets.
263 if (tmptok == "Quotes") {
264 inset = new InsetQuotes(string());
266 } else if (tmptok == "LaTeXAccent" || tmptok == "\\i") {
267 inset = new InsetLatexAccent;
269 } else if (tmptok == "FormulaMacro") {
270 inset = new InsetFormulaMacro;
272 } else if (tmptok == "Formula") {
273 inset = new InsetFormula;
275 } else if (tmptok == "Figure") {
276 inset = new InsetFig(100,100, buffer);
279 } else if (tmptok == "Tabular") {
280 inset = new InsetTabular(buffer);
283 } else if (tmptok == "Text") {
284 inset = new InsetText(buffer);
286 } else if (tmptok == "ERT") {
287 inset = new InsetERT(buffer);
289 } else if (tmptok == "Info") {
290 inset = new InsetInfo;
292 } else if (tmptok == "Include") {
293 inset = new InsetInclude(string(), buffer);
295 } else if (tmptok == "LatexCommand") {
298 if (inscmd.getCmdName()=="cite") {
299 inset = new InsetCitation(inscmd.getContents(),
300 inscmd.getOptions());
301 } else if (inscmd.getCmdName()=="bibitem") {
302 lex.printError("Wrong place for bibitem");
303 inset = inscmd.Clone();
304 } else if (inscmd.getCmdName()=="BibTeX") {
305 inset = new InsetBibtex(inscmd.getContents(),
306 inscmd.getOptions(), buffer);
307 } else if (inscmd.getCmdName()=="index") {
308 inset = new InsetIndex(inscmd.getContents());
309 } else if (inscmd.getCmdName()=="include") {
310 inset = new InsetInclude(inscmd.getContents(), buffer);
311 } else if (inscmd.getCmdName()=="label") {
312 inset = new InsetLabel(inscmd.getCommand());
313 } else if (inscmd.getCmdName() == "ref" ||
314 inscmd.getCmdName() == "pageref") {
315 inset = new InsetRef(inscmd, buffer);
317 #if 0 // Is this compatibility code needed (Lgb)
319 // The following three are only for compatibility
320 if (inscmd.getCmdName()=="-") {
321 inset = new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
322 } else if (inscmd.getCmdName()=="@.") {
323 inset = new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
324 } else if (inscmd.getCmdName()=="ldots") {
325 inset = new InsetSpecialChar(InsetSpecialChar::LDOTS);
327 inset = inscmd.Clone();
331 par->InsertChar(pos, LyXParagraph::META_INSET);
332 par->InsertInset(pos, inset);
333 par->SetFont(pos, font);
336 lex.printError("Unknown inset `$$Token'. "
337 "Inserting as text.");
339 #ifndef NO_COMPABILITY
340 } else if (token == "\\hfill") {
341 // now obsolete, but we have a bak compability
342 // Inset * inset = new InsetSpecialChar(LyXParagraph::META_HFILL);
343 // par->InsertChar(pos, LyXParagraph::META_INSET);
344 // par->InsertInset(pos, inset);
345 par->InsertChar(pos, LyXParagraph::META_HFILL);
346 par->SetFont(pos, font);
348 } else if (token == "\\protected_separator") {
349 // now obsolete, but we have a back compability
350 par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
351 //Inset * inset = new InsetSpecialChar(LyXParagraph::META_PROTECTED_SEPARATOR);
352 // par->InsertChar(pos, LyXParagraph::META_INSET);
353 // par->InsertInset(pos, inset);
354 par->SetFont(pos, font);
357 } else if (token == "\\bibitem") { // ale970302
359 par->bibkey = new InsetBibKey;
360 par->bibkey->Read(lex);
361 }else if (token == "\\backslash") {
362 par->InsertChar(pos, '\\');
363 par->SetFont(pos, font);
365 } else if (token == "\\end_inset") {
368 lex.printError("Unknown tabular token `$$Token'. Not handled!");
372 if (token != "\\end_inset") {
373 lex.printError("Missing \\end_inset at this point. "
380 int InsetText::ascent(Painter &, LyXFont const & font) const
384 return font.maxAscent();
388 int InsetText::descent(Painter &, LyXFont const & font) const
392 return font.maxDescent();
396 int InsetText::width(Painter &, LyXFont const &) const
402 int InsetText::getMaxWidth(UpdatableInset * inset) const
404 if (!the_locking_inset) {
405 lyxerr << "Text: No locking inset in this inset.\n";
409 if (the_locking_inset==inset)
412 return the_locking_inset->getMaxWidth(inset);
416 void InsetText::draw(Painter & pain, LyXFont const & f,
417 int baseline, float & x) const
419 UpdatableInset::draw(pain, f, baseline, x);
421 bool do_reset_pos = (x != top_x) || (baseline != top_baseline);
423 top_baseline = baseline;
424 computeBaselines(baseline);
425 for(unsigned int r = 0; r < rows.size() - 1; ++r) {
426 drawRowSelection(pain, rows[r].pos, rows[r+1].pos, r,
427 rows[r].baseline, x);
428 drawRowText(pain, rows[r].pos, rows[r+1].pos, rows[r].baseline, x);
431 if (!the_locking_inset && do_reset_pos) {
432 // HideInsetCursor(bv);
434 // ShowInsetCursor(bv);
439 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
440 int row, int baseline, float x) const
446 if (selection_start > selection_end) {
447 s_start = selection_end;
448 s_end = selection_start;
450 s_start = selection_start;
451 s_end = selection_end;
453 if ((s_start > endpos) || (s_end < startpos))
457 int ssel_x = esel_x = int(x);
460 for(; p < endpos; ++p) {
463 if ((p >= s_start) && (p <= s_end))
465 char ch = par->GetChar(p);
466 font = GetFont(par,p);
467 if (IsFloatChar(ch)) {
469 } else if (ch == LyXParagraph::META_INSET) {
470 Inset const * tmpinset = par->GetInset(p);
471 x += tmpinset->width(pain, font);
473 x += pain.width(ch,font);
478 if ((p >= s_start) && (p <= s_end))
480 if (ssel_x < esel_x) {
481 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
482 int(esel_x - ssel_x),
483 rows[row].asc + rows[row].desc,
489 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
490 int baseline, float x) const
492 Assert(endpos <= par->Last());
494 for(int p = startpos; p < endpos; ++p) {
495 char ch = par->GetChar(p);
496 LyXFont font = GetFont(par,p);
497 if (IsFloatChar(ch)) {
499 } else if (ch == LyXParagraph::META_INSET) {
500 Inset * tmpinset = par->GetInset(p);
502 tmpinset->draw(pain, font, baseline, x);
504 pain.text(int(x), baseline, ch, font);
505 x += pain.width(ch,font);
511 char const * InsetText::EditMessage() const
513 return _("Opened Text Inset");
517 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
519 UpdatableInset::Edit(bv, x, y, button);
522 the_locking_inset = 0;
523 inset_pos = inset_x = inset_y = 0;
526 selection_start = selection_end = actpos;
527 current_font = real_current_font = GetFont(par, actpos);
531 void InsetText::InsetUnlock(BufferView * bv)
533 if (the_locking_inset)
534 the_locking_inset->InsetUnlock(bv);
536 if (hasSelection()) {
537 selection_start = selection_end = actpos;
538 bv->updateInset(this, false);
540 the_locking_inset = 0;
541 no_selection = false;
545 bool InsetText::UnlockInsetInInset(BufferView * bv, Inset * inset, bool lr)
547 if (!the_locking_inset)
549 if (the_locking_inset == inset) {
550 the_locking_inset->InsetUnlock(bv);
551 the_locking_inset = 0;
553 moveRight(bv, false);
556 return the_locking_inset->UnlockInsetInInset(bv, inset,lr);
560 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
562 if (!the_locking_inset)
564 if (the_locking_inset != inset)
565 return the_locking_inset->UpdateInsetInInset(bv, inset);
567 inset->draw(bv->getPainter(), real_current_font, inset_y, x);
568 bv->updateInset(this, true);
573 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
575 if (the_locking_inset) {
576 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
579 no_selection = false;
583 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
585 if (hasSelection()) {
586 selection_start = selection_end = actpos;
587 bv->updateInset(this, false);
589 no_selection = false;
590 if (the_locking_inset) {
591 setPos(bv, x,y,false);
594 if (par->GetChar(actpos)==LyXParagraph::META_INSET)
595 inset=(UpdatableInset*)par->GetInset(actpos);
596 if (the_locking_inset == inset) {
597 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
600 // otherwise unlock the_locking_inset and lock the new inset
604 the_locking_inset->InsetUnlock(bv);
605 the_locking_inset = inset;
606 the_locking_inset->Edit(bv, x - inset_x, y - inset_y, button);
609 // otherwise only unlock the_locking_inset
610 the_locking_inset->InsetUnlock(bv);
613 the_locking_inset = 0;
615 selection_start = selection_end = actpos;
616 if (!the_locking_inset)
621 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int button)
623 if (the_locking_inset) {
624 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
629 int old = selection_end;
630 setPos(bv, x, y, false);
631 selection_end = actpos;
632 if (old != selection_end)
633 bv->updateInset(this, false);
635 no_selection = false;
639 void InsetText::InsetKeyPress(XKeyEvent * xke)
641 if (the_locking_inset) {
642 the_locking_inset->InsetKeyPress(xke);
648 UpdatableInset::RESULT InsetText::LocalDispatch(BufferView * bv,
649 int action, string const & arg)
651 no_selection = false;
652 if (UpdatableInset::LocalDispatch(bv, action, arg)) {
657 UpdatableInset::RESULT
660 if ((action < 0) && arg.empty())
663 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
664 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
666 if (the_locking_inset) {
667 result = the_locking_inset->LocalDispatch(bv, action, arg);
668 if (result == DISPATCHED) {
669 the_locking_inset->ToggleInsetCursor(bv);
670 bv->updateInset(this, false);
671 the_locking_inset->ToggleInsetCursor(bv);
673 } else if (result == FINISHED) {
674 if ((action == LFUN_RIGHT) || (action == -1)) {
675 actpos = inset_pos + 1;
678 the_locking_inset = 0;
686 par->InsertChar(actpos,arg[0]);
687 par->SetFont(actpos,real_current_font);
689 bv->updateInset(this, true);
691 selection_start = selection_end = actpos;
694 // --- Cursor Movements ---------------------------------------------
696 moveRight(bv, false);
697 selection_end = actpos;
698 bv->updateInset(this, false);
701 result= DISPATCH_RESULT(moveRight(bv));
702 if (hasSelection()) {
703 selection_start = selection_end = actpos;
704 bv->updateInset(this, false);
706 selection_start = selection_end = actpos;
711 selection_end = actpos;
712 bv->updateInset(this, false);
715 result= DISPATCH_RESULT(moveLeft(bv));
716 if (hasSelection()) {
717 selection_start = selection_end = actpos;
718 bv->updateInset(this, false);
720 selection_start = selection_end = actpos;
725 selection_end = actpos;
726 bv->updateInset(this, false);
729 result= DISPATCH_RESULT(moveDown(bv));
730 if (hasSelection()) {
731 selection_start = selection_end = actpos;
732 bv->updateInset(this, false);
734 selection_start = selection_end = actpos;
739 selection_end = actpos;
740 bv->updateInset(this, false);
743 result= DISPATCH_RESULT(moveUp(bv));
744 if (hasSelection()) {
745 selection_start = selection_end = actpos;
746 bv->updateInset(this, false);
748 selection_start = selection_end = actpos;
752 if (!actpos || par->IsNewline(actpos-1)) {
753 if (hasSelection()) {
754 selection_start = selection_end = actpos;
755 bv->updateInset(this, false);
761 if (Delete()) { // we need update
762 selection_start = selection_end = actpos;
764 bv->updateInset(this, true);
765 } else if (hasSelection()) {
766 selection_start = selection_end = actpos;
767 bv->updateInset(this, false);
771 for(; actpos > rows[actrow].pos; --actpos)
772 cx -= SingleWidth(bv, par, actpos);
773 cx -= SingleWidth(bv, par, actpos);
774 if (hasSelection()) {
775 selection_start = selection_end = actpos;
776 bv->updateInset(this, false);
778 selection_start = selection_end = actpos;
782 for(; actpos < rows[actrow + 1].pos; ++actpos)
783 cx += SingleWidth(bv, par, actpos);
784 if (hasSelection()) {
785 selection_start = selection_end = actpos;
786 bv->updateInset(this, false);
788 selection_start = selection_end = actpos;
791 case LFUN_MATH_MODE: // Open or create a math inset
792 InsertInset(bv, new InsetFormula);
793 if (hasSelection()) {
794 selection_start = selection_end = actpos;
795 bv->updateInset(this, false);
797 selection_start = selection_end = actpos;
802 result = UNDISPATCHED;
805 if (result != FINISHED) {
806 if (!the_locking_inset)
809 bv->unlockInset(this);
814 int InsetText::Latex(ostream &os, signed char fragile) const
818 int i = Latex(fstr, fragile);
824 int InsetText::Latex(string & file, signed char /* fragile */) const
828 return par->SimpleTeXOnePar(file, texrow);
832 void InsetText::Validate(LaTeXFeatures & features) const
834 par->validate(features);
838 // Returns the width of a character at a certain spot
839 int InsetText::SingleWidth(BufferView * bv, LyXParagraph * par, int pos)
841 LyXFont font = GetFont(par, pos);
842 char c = par->GetChar(pos);
844 // The most common case is handled first (Asger)
845 if (IsPrintable(c)) {
846 return font.width(c);
847 } else if (c == LyXParagraph::META_INSET) {
848 Inset const * tmpinset=par->GetInset(pos);
850 return tmpinset->width(bv->getPainter(), font);
853 } else if (IsSeparatorChar(c))
855 else if (IsNewlineChar(c))
857 return font.width(c);
861 // Returns the width of a character at a certain spot
862 void InsetText::SingleHeight(BufferView * bv, LyXParagraph * par,int pos,
863 int & asc, int & desc)
865 LyXFont font = GetFont(par, pos);
866 char c = par->GetChar(pos);
869 // The most common case is handled first (Asger)
870 if (c == LyXParagraph::META_INSET) {
871 Inset const * tmpinset=par->GetInset(pos);
873 asc = tmpinset->ascent(bv->getPainter(),font);
874 desc = tmpinset->descent(bv->getPainter(),font);
877 asc = font.maxAscent();
878 desc = font.maxDescent();
884 // Gets the fully instantiated font at a given position in a paragraph
885 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
886 // The difference is that this one is used for displaying, and thus we
887 // are allowed to make cosmetic improvements. For instance make footnotes
889 // If position is -1, we get the layout font of the paragraph.
890 // If position is -2, we get the font of the manual label of the paragraph.
891 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
893 char par_depth = par->GetDepth();
896 textclasslist.Style(buffer->params.textclass, par->GetLayout());
898 // We specialize the 95% common case:
899 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
902 if (layout.labeltype == LABEL_MANUAL
903 && pos < BeginningOfMainBody(par)) {
905 return par->GetFontSettings(pos).realize(layout.reslabelfont);
907 return par->GetFontSettings(pos).realize(layout.resfont);
910 // process layoutfont for pos == -1 and labelfont for pos < -1
912 return layout.resfont;
914 return layout.reslabelfont;
917 // The uncommon case need not be optimized as much
919 LyXFont layoutfont, tmpfont;
923 if (pos < BeginningOfMainBody(par)) {
925 layoutfont = layout.labelfont;
928 layoutfont = layout.font;
930 tmpfont = par->GetFontSettings(pos);
931 tmpfont.realize(layoutfont);
934 // process layoutfont for pos == -1 and labelfont for pos < -1
936 tmpfont = layout.font;
938 tmpfont = layout.labelfont;
941 // Resolve against environment font information
942 //if (par->GetDepth()){ // already in while condition
943 while (par && par_depth && !tmpfont.resolved()) {
944 par = par->DepthHook(par_depth - 1);
946 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
947 par->GetLayout()).font);
948 par_depth = par->GetDepth();
951 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
957 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
959 if (textclasslist.Style(buffer->params.textclass,
960 par->GetLayout()).labeltype != LABEL_MANUAL)
963 return par->BeginningOfMainBody();
967 void InsetText::GetCursorPos(int & x, int & y)
974 int InsetText::InsetInInsetY()
976 if (!the_locking_inset)
981 return (y + the_locking_inset->InsetInInsetY());
985 void InsetText::ToggleInsetCursor(BufferView * bv)
987 if (the_locking_inset) {
988 the_locking_inset->ToggleInsetCursor(bv);
992 LyXFont font = GetFont(par, actpos);
994 int asc = font.maxAscent();
995 int desc = font.maxDescent();
998 bv->hideLockedInsetCursor();
1000 bv->showLockedInsetCursor(cx, cy, asc, desc);
1001 cursor_visible = !cursor_visible;
1005 void InsetText::ShowInsetCursor(BufferView * bv)
1007 if (!cursor_visible) {
1008 LyXFont font = GetFont(par, actpos);
1010 int asc = font.maxAscent();
1011 int desc = font.maxDescent();
1012 bv->fitLockedInsetCursor(cx, cy, asc, desc);
1013 bv->showLockedInsetCursor(cx, cy, asc, desc);
1014 cursor_visible = true;
1019 void InsetText::HideInsetCursor(BufferView * bv)
1022 ToggleInsetCursor(bv);
1026 void InsetText::setPos(BufferView * bv, int x, int y, bool activate_inset)
1030 // search right X-pos x==0 -> top_x
1031 actpos = actrow = 0;
1034 for(unsigned int i = 1;
1035 ((cy + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
1036 cy = rows[i].baseline;
1037 actpos = rows[i].pos;
1045 int sw = swh = SingleWidth(bv, par,actpos);
1046 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
1048 while ((actpos < (rows[actrow + 1].pos - 1)) && ((cx + swh) < x)) {
1051 sw = swh = SingleWidth(bv, par,actpos);
1052 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
1055 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
1057 static_cast<UpdatableInset*>(par->GetInset(actpos));
1058 inset_x = cx - top_x;
1061 the_locking_inset->Edit(bv, ox - inset_x, oy - inset_y, 0);
1066 bool InsetText::moveRight(BufferView * bv, bool activate_inset)
1068 if (actpos >= par->Last())
1070 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
1072 static_cast<UpdatableInset*>(par->GetInset(actpos));
1073 inset_x = cx - top_x;
1076 the_locking_inset->Edit(bv, 0, 0, 0);
1085 bool InsetText::moveLeft(BufferView * bv, bool activate_inset)
1090 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
1092 static_cast<UpdatableInset*>(par->GetInset(actpos));
1094 inset_x = cx - top_x;
1097 the_locking_inset->Edit(bv, the_locking_inset->
1098 width(bv->getPainter(), GetFont(par,actpos)),
1107 bool InsetText::moveUp(BufferView * bv, bool activate_inset)
1111 cy = rows[actrow - 1].baseline - top_baseline;
1112 setPos(bv, cx - top_x, cy, activate_inset);
1117 bool InsetText::moveDown(BufferView * bv, bool activate_inset)
1119 if (actrow >= int(rows.size() - 2))
1121 cy = rows[actrow + 1].baseline - top_baseline;
1122 setPos(bv, cx - top_x, cy, activate_inset);
1127 void InsetText::resetPos(BufferView * bv)
1129 int old_pos = actpos;
1133 for(int i = 0; rows[i].pos <= actpos; ++i) {
1134 cy = rows[i].baseline;
1138 setPos(bv, 0, cy, false);
1140 while(actpos < old_pos) {
1141 cx += SingleWidth(bv, par,actpos);
1147 bool InsetText::Delete()
1149 /* some insets are undeletable here */
1150 if (par->GetChar(actpos)==LyXParagraph::META_INSET) {
1151 /* force complete redo when erasing display insets */
1152 /* this is a cruel mathod but save..... Matthias */
1153 if (par->GetInset(actpos)->Deletable() &&
1154 par->GetInset(actpos)->display()) {
1165 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1167 par->InsertChar(actpos, LyXParagraph::META_INSET);
1168 par->InsertInset(actpos, inset);
1169 computeTextRows(bv);
1170 bv->updateInset(this, true);
1171 the_locking_inset = static_cast<UpdatableInset*>(inset);
1172 inset_x = cx - top_x;
1175 inset->Edit(bv, 0, 0, 0);
1180 UpdatableInset * InsetText::GetLockingInset()
1182 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1186 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1188 // if there is no selection just set the current_font
1189 if (!hasSelection()) {
1190 // Determine basis font
1192 if (actpos < BeginningOfMainBody(par))
1193 layoutfont = GetFont(par, -2);
1195 layoutfont = GetFont(par, -1);
1197 // Update current font
1198 real_current_font.update(font, toggleall);
1200 // Reduce to implicit settings
1201 current_font = real_current_font;
1202 current_font.reduce(layoutfont);
1203 // And resolve it completely
1204 real_current_font.realize(layoutfont);
1209 if (selection_start > selection_end) {
1210 s_start = selection_end;
1211 s_end = selection_start;
1213 s_start = selection_start;
1214 s_end = selection_end;
1217 while(s_start < s_end) {
1218 newfont = GetFont(par,s_start);
1219 newfont.update(font, toggleall);
1220 SetCharFont(s_start, newfont);
1223 computeTextRows(bv);
1224 bv->updateInset(this, true);
1228 void InsetText::SetCharFont(int pos, LyXFont const & f)
1230 /* let the insets convert their font */
1233 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1234 if (par->GetInset(pos))
1235 font = par->GetInset(pos)->ConvertFont(font);
1238 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1240 // Get concrete layout font to reduce against
1243 if (pos < BeginningOfMainBody(par))
1244 layoutfont = layout.labelfont;
1246 layoutfont = layout.font;
1249 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1252 // Now, reduce font against full layout font
1253 font.reduce(layoutfont);
1255 par->SetFont(pos, font);
1259 void InsetText::computeTextRows(BufferView * bv)
1273 rows.erase(rows.begin(),rows.end());
1274 int width = wordAscent = wordDescent = 0;
1275 insetWidth = maxAscent = maxDescent = 0;
1280 rows.push_back(row);
1282 for(p=0; p < par->Last(); ++p) {
1283 insetWidth += SingleWidth(bv, par, p);
1284 SingleHeight(bv, par, p, asc, desc);
1285 if (asc > maxAscent)
1287 if (desc > maxDescent)
1290 rows[0].asc = maxAscent;
1291 rows[0].desc = maxDescent;
1292 // alocate a dummy row for the endpos
1293 row.pos = par->Last();
1294 rows.push_back(row);
1297 bool is_first_word_in_row = true;
1302 for(p = 0; p < par->Last(); ++p) {
1303 cw = SingleWidth(bv, par, p);
1305 lastWordWidth += cw;
1306 SingleHeight(bv, par, p, asc, desc);
1307 if (asc > wordAscent)
1309 if (desc > wordDescent)
1311 Inset const * inset = 0;
1312 if (((p + 1) < par->Last()) &&
1313 (par->GetChar(p+1)==LyXParagraph::META_INSET))
1314 inset = par->GetInset(p+1);
1315 if (inset && inset->display()) {
1316 if (!is_first_word_in_row && (width >= maxWidth)) {
1317 // we have to split also the row above
1318 rows[rows.size()-1].asc = oasc;
1319 rows[rows.size()-1].desc = odesc;
1321 rows.push_back(row);
1323 odesc = wordDescent;
1324 if (insetWidth < owidth)
1325 insetWidth = owidth;
1326 width = lastWordWidth;
1329 if (oasc < wordAscent)
1331 if (odesc < wordDescent)
1332 odesc = wordDescent;
1334 rows[rows.size() - 1].asc = oasc;
1335 rows[rows.size() - 1].desc = odesc;
1337 rows.push_back(row);
1338 SingleHeight(bv, par, p, asc, desc);
1339 rows[rows.size() - 1].asc = asc;
1340 rows[rows.size() - 1].desc = desc;
1341 row.pos = nwp = p + 1;
1342 rows.push_back(row);
1343 oasc = odesc = width = lastWordWidth = 0;
1344 is_first_word_in_row = true;
1345 wordAscent = wordDescent = 0;
1347 } else if (par->IsSeparator(p)) {
1348 if (width >= maxWidth) {
1349 if (is_first_word_in_row) {
1350 rows[rows.size()-1].asc = wordAscent;
1351 rows[rows.size()-1].desc = wordDescent;
1353 rows.push_back(row);
1354 oasc = odesc = width = 0;
1356 rows[rows.size()-1].asc = oasc;
1357 rows[rows.size()-1].desc = odesc;
1359 rows.push_back(row);
1361 odesc = wordDescent;
1362 if (insetWidth < owidth)
1363 insetWidth = owidth;
1364 width = lastWordWidth;
1366 wordAscent = wordDescent = lastWordWidth = 0;
1371 if (oasc < wordAscent)
1373 if (odesc < wordDescent)
1374 odesc = wordDescent;
1375 wordAscent = wordDescent = lastWordWidth = 0;
1377 is_first_word_in_row = false;
1380 // if we have some data in the paragraph we have ascent/descent
1382 if (width >= maxWidth) {
1384 rows[rows.size()-1].asc = oasc;
1385 rows[rows.size()-1].desc = odesc;
1386 // assign and allocate lower row
1388 rows.push_back(row);
1389 rows[rows.size()-1].asc = wordAscent;
1390 rows[rows.size()-1].desc = wordDescent;
1391 if (insetWidth < owidth)
1392 insetWidth = owidth;
1394 if (insetWidth < width)
1397 // assign last row data
1398 if (oasc < wordAscent)
1400 if (odesc < wordDescent)
1401 odesc = wordDescent;
1402 rows[rows.size()-1].asc = oasc;
1403 rows[rows.size()-1].desc = odesc;
1406 // alocate a dummy row for the endpos
1407 row.pos = par->Last();
1408 rows.push_back(row);
1409 // calculate maxAscent/Descent
1410 maxAscent = rows[0].asc;
1411 maxDescent = rows[0].desc;
1412 for (unsigned int i=1; i<rows.size()-1; ++i) {
1413 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1415 if (the_locking_inset) {
1416 computeBaselines(top_baseline);
1425 void InsetText::computeBaselines(int baseline) const
1427 rows[0].baseline = baseline;
1428 for (unsigned int i=1; i<rows.size()-1; i++) {
1429 rows[i].baseline = rows[i-1].baseline + rows[i-1].desc +
1430 rows[i].asc + interline_space;
1435 void InsetText::init(BufferView * bv)
1439 computeTextRows(bv);