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 Inset * 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
351 par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
354 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
355 par->InsertChar(pos, LyXParagraph::META_INSET);
356 par->InsertInset(pos, inset);
358 par->SetFont(pos, font);
361 } else if (token == "\\bibitem") { // ale970302
363 par->bibkey = new InsetBibKey;
364 par->bibkey->Read(lex);
365 } else if (token == "\\backslash") {
366 par->InsertChar(pos, '\\');
367 par->SetFont(pos, font);
369 } else if (token == "\\end_inset") {
372 lex.printError("Unknown tabular token `$$Token'. Not handled!");
376 if (token != "\\end_inset") {
377 lex.printError("Missing \\end_inset at this point. "
384 int InsetText::ascent(Painter & pain, LyXFont const & font) const
387 computeTextRows(pain);
392 return font.maxAscent();
396 int InsetText::descent(Painter & pain, LyXFont const & font) const
399 computeTextRows(pain);
404 return font.maxDescent();
408 int InsetText::width(Painter & pain, LyXFont const &) const
411 computeTextRows(pain);
418 int InsetText::getMaxWidth(UpdatableInset * inset) const
420 if (!the_locking_inset) {
421 lyxerr << "Text: No locking inset in this inset.\n";
425 if (the_locking_inset == inset)
428 return the_locking_inset->getMaxWidth(inset);
432 void InsetText::draw(Painter & pain, LyXFont const & f,
433 int baseline, float & x) const
436 computeTextRows(pain);
437 // init_inset = false;
439 UpdatableInset::draw(pain, f, baseline, x);
441 bool do_reset_pos = (x != top_x) || (baseline != top_baseline);
443 top_baseline = baseline;
444 computeBaselines(baseline);
445 for(unsigned int r = 0; r < rows.size() - 1; ++r) {
446 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
447 rows[r].baseline, x);
448 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
451 if (!the_locking_inset && do_reset_pos) {
452 // HideInsetCursor(bv);
454 // ShowInsetCursor(bv);
459 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
460 int row, int baseline, float x) const
466 if (selection_start > selection_end) {
467 s_start = selection_end;
468 s_end = selection_start;
470 s_start = selection_start;
471 s_end = selection_end;
473 if ((s_start > endpos) || (s_end < startpos))
477 int ssel_x = esel_x = int(x);
480 for(; p < endpos; ++p) {
483 if ((p >= s_start) && (p <= s_end))
485 char ch = par->GetChar(p);
486 font = GetFont(par,p);
487 if (IsFloatChar(ch)) {
489 } else if (ch == LyXParagraph::META_INSET) {
490 Inset const * tmpinset = par->GetInset(p);
491 x += tmpinset->width(pain, font);
493 x += pain.width(ch,font);
498 if ((p >= s_start) && (p <= s_end))
500 if (ssel_x < esel_x) {
501 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
502 int(esel_x - ssel_x),
503 rows[row].asc + rows[row].desc,
509 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
510 int baseline, float x) const
512 Assert(endpos <= par->Last());
514 for(int p = startpos; p < endpos; ++p) {
515 char ch = par->GetChar(p);
516 LyXFont font = GetFont(par,p);
517 if (IsFloatChar(ch)) {
519 } else if (ch == LyXParagraph::META_INSET) {
520 Inset * tmpinset = par->GetInset(p);
522 tmpinset->draw(pain, font, baseline, x);
524 pain.text(int(x), baseline, ch, font);
525 x += pain.width(ch,font);
531 char const * InsetText::EditMessage() const
533 return _("Opened Text Inset");
537 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
539 UpdatableInset::Edit(bv, x, y, button);
542 the_locking_inset = 0;
543 inset_pos = inset_x = inset_y = 0;
546 selection_start = selection_end = actpos;
547 current_font = real_current_font = GetFont(par, actpos);
551 void InsetText::InsetUnlock(BufferView * bv)
553 if (the_locking_inset)
554 the_locking_inset->InsetUnlock(bv);
556 if (hasSelection()) {
557 selection_start = selection_end = actpos;
558 bv->updateInset(this, false);
560 the_locking_inset = 0;
561 no_selection = false;
565 bool InsetText::UnlockInsetInInset(BufferView * bv, Inset * inset, bool lr)
567 if (!the_locking_inset)
569 if (the_locking_inset == inset) {
570 the_locking_inset->InsetUnlock(bv);
571 the_locking_inset = 0;
573 moveRight(bv, false);
576 return the_locking_inset->UnlockInsetInInset(bv, inset,lr);
580 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
582 if (!the_locking_inset)
584 if (the_locking_inset != inset)
585 return the_locking_inset->UpdateInsetInInset(bv, inset);
587 inset->draw(bv->getPainter(), real_current_font, inset_y, x);
588 bv->updateInset(this, true);
593 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
595 if (the_locking_inset) {
596 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
599 no_selection = false;
603 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
605 if (hasSelection()) {
606 selection_start = selection_end = actpos;
607 bv->updateInset(this, false);
609 no_selection = false;
610 if (the_locking_inset) {
611 setPos(bv, x, y, false);
614 if (par->GetChar(actpos) == LyXParagraph::META_INSET)
615 inset = static_cast<UpdatableInset*>(par->GetInset(actpos));
616 if (the_locking_inset == inset) {
617 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
620 // otherwise unlock the_locking_inset and lock the new inset
624 the_locking_inset->InsetUnlock(bv);
625 the_locking_inset = inset;
626 the_locking_inset->Edit(bv, x - inset_x, y - inset_y, button);
629 // otherwise only unlock the_locking_inset
630 the_locking_inset->InsetUnlock(bv);
633 the_locking_inset = 0;
635 selection_start = selection_end = actpos;
636 if (!the_locking_inset)
641 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int button)
643 if (the_locking_inset) {
644 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
649 int old = selection_end;
650 setPos(bv, x, y, false);
651 selection_end = actpos;
652 if (old != selection_end)
653 bv->updateInset(this, false);
655 no_selection = false;
659 void InsetText::InsetKeyPress(XKeyEvent * xke)
661 if (the_locking_inset) {
662 the_locking_inset->InsetKeyPress(xke);
668 UpdatableInset::RESULT InsetText::LocalDispatch(BufferView * bv,
669 int action, string const & arg)
671 no_selection = false;
672 if (UpdatableInset::LocalDispatch(bv, action, arg)) {
677 UpdatableInset::RESULT
680 if ((action < 0) && arg.empty())
683 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
684 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
686 if (the_locking_inset) {
687 result = the_locking_inset->LocalDispatch(bv, action, arg);
688 if (result == DISPATCHED) {
689 the_locking_inset->ToggleInsetCursor(bv);
690 bv->updateInset(this, false);
691 the_locking_inset->ToggleInsetCursor(bv);
693 } else if (result == FINISHED) {
694 if ((action == LFUN_RIGHT) || (action == -1)) {
695 actpos = inset_pos + 1;
698 the_locking_inset = 0;
706 par->InsertChar(actpos,arg[0]);
707 par->SetFont(actpos,real_current_font);
708 computeTextRows(bv->getPainter());
709 bv->updateInset(this, true);
711 selection_start = selection_end = actpos;
714 // --- Cursor Movements ---------------------------------------------
716 moveRight(bv, false);
717 selection_end = actpos;
718 bv->updateInset(this, false);
721 result= DISPATCH_RESULT(moveRight(bv));
722 if (hasSelection()) {
723 selection_start = selection_end = actpos;
724 bv->updateInset(this, false);
726 selection_start = selection_end = actpos;
731 selection_end = actpos;
732 bv->updateInset(this, false);
735 result= DISPATCH_RESULT(moveLeft(bv));
736 if (hasSelection()) {
737 selection_start = selection_end = actpos;
738 bv->updateInset(this, false);
740 selection_start = selection_end = actpos;
745 selection_end = actpos;
746 bv->updateInset(this, false);
749 result= DISPATCH_RESULT(moveDown(bv));
750 if (hasSelection()) {
751 selection_start = selection_end = actpos;
752 bv->updateInset(this, false);
754 selection_start = selection_end = actpos;
759 selection_end = actpos;
760 bv->updateInset(this, false);
763 result= DISPATCH_RESULT(moveUp(bv));
764 if (hasSelection()) {
765 selection_start = selection_end = actpos;
766 bv->updateInset(this, false);
768 selection_start = selection_end = actpos;
772 if (!actpos || par->IsNewline(actpos-1)) {
773 if (hasSelection()) {
774 selection_start = selection_end = actpos;
775 bv->updateInset(this, false);
781 if (Delete()) { // we need update
782 selection_start = selection_end = actpos;
783 computeTextRows(bv->getPainter());
784 bv->updateInset(this, true);
785 } else if (hasSelection()) {
786 selection_start = selection_end = actpos;
787 bv->updateInset(this, false);
791 for(; actpos > rows[actrow].pos; --actpos)
792 cx -= SingleWidth(bv->getPainter(), par, actpos);
793 cx -= SingleWidth(bv->getPainter(), par, actpos);
794 if (hasSelection()) {
795 selection_start = selection_end = actpos;
796 bv->updateInset(this, false);
798 selection_start = selection_end = actpos;
802 for(; actpos < rows[actrow + 1].pos; ++actpos)
803 cx += SingleWidth(bv->getPainter(), par, actpos);
804 if (hasSelection()) {
805 selection_start = selection_end = actpos;
806 bv->updateInset(this, false);
808 selection_start = selection_end = actpos;
811 case LFUN_MATH_MODE: // Open or create a math inset
812 InsertInset(bv, new InsetFormula);
813 if (hasSelection()) {
814 selection_start = selection_end = actpos;
815 bv->updateInset(this, false);
817 selection_start = selection_end = actpos;
821 result = UNDISPATCHED;
824 if (result != FINISHED) {
825 if (!the_locking_inset)
828 bv->unlockInset(this);
833 int InsetText::Latex(ostream & os, signed char fragile) const
837 int i = Latex(fstr, fragile);
843 int InsetText::Latex(string & file, signed char /* fragile */) const
847 return par->SimpleTeXOnePar(file, texrow);
851 void InsetText::Validate(LaTeXFeatures & features) const
853 par->validate(features);
857 // Returns the width of a character at a certain spot
858 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
860 LyXFont font = GetFont(par, pos);
861 char c = par->GetChar(pos);
863 if (IsPrintable(c)) {
864 return font.width(c);
865 } else if (c == LyXParagraph::META_INSET) {
866 Inset const * tmpinset = par->GetInset(pos);
868 return tmpinset->width(pain, font);
871 } else if (IsSeparatorChar(c))
873 else if (IsNewlineChar(c))
875 return font.width(c);
879 // Returns the width of a character at a certain spot
880 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
881 int & asc, int & desc) const
883 LyXFont font = GetFont(par, pos);
884 char c = par->GetChar(pos);
887 if (c == LyXParagraph::META_INSET) {
888 Inset const * tmpinset=par->GetInset(pos);
890 asc = tmpinset->ascent(pain, font);
891 desc = tmpinset->descent(pain, font);
894 asc = font.maxAscent();
895 desc = font.maxDescent();
901 // Gets the fully instantiated font at a given position in a paragraph
902 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
903 // The difference is that this one is used for displaying, and thus we
904 // are allowed to make cosmetic improvements. For instance make footnotes
906 // If position is -1, we get the layout font of the paragraph.
907 // If position is -2, we get the font of the manual label of the paragraph.
908 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
910 char par_depth = par->GetDepth();
912 LyXLayout const & layout =
913 textclasslist.Style(buffer->params.textclass, par->GetLayout());
915 // We specialize the 95% common case:
916 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
919 if (layout.labeltype == LABEL_MANUAL
920 && pos < BeginningOfMainBody(par)) {
922 return par->GetFontSettings(pos).realize(layout.reslabelfont);
924 return par->GetFontSettings(pos).realize(layout.resfont);
927 // process layoutfont for pos == -1 and labelfont for pos < -1
929 return layout.resfont;
931 return layout.reslabelfont;
934 // The uncommon case need not be optimized as much
936 LyXFont layoutfont, tmpfont;
940 if (pos < BeginningOfMainBody(par)) {
942 layoutfont = layout.labelfont;
945 layoutfont = layout.font;
947 tmpfont = par->GetFontSettings(pos);
948 tmpfont.realize(layoutfont);
951 // process layoutfont for pos == -1 and labelfont for pos < -1
953 tmpfont = layout.font;
955 tmpfont = layout.labelfont;
958 // Resolve against environment font information
959 //if (par->GetDepth()){ // already in while condition
960 while (par && par_depth && !tmpfont.resolved()) {
961 par = par->DepthHook(par_depth - 1);
963 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
964 par->GetLayout()).font);
965 par_depth = par->GetDepth();
968 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
974 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
976 if (textclasslist.Style(buffer->params.textclass,
977 par->GetLayout()).labeltype != LABEL_MANUAL)
980 return par->BeginningOfMainBody();
984 void InsetText::GetCursorPos(int & x, int & y) const
991 int InsetText::InsetInInsetY()
993 if (!the_locking_inset)
997 return (y + the_locking_inset->InsetInInsetY());
1001 void InsetText::ToggleInsetCursor(BufferView * bv)
1003 if (the_locking_inset) {
1004 the_locking_inset->ToggleInsetCursor(bv);
1008 LyXFont font = GetFont(par, actpos);
1010 int asc = font.maxAscent();
1011 int desc = font.maxDescent();
1014 bv->hideLockedInsetCursor();
1016 bv->showLockedInsetCursor(cx, cy, asc, desc);
1017 cursor_visible = !cursor_visible;
1021 void InsetText::ShowInsetCursor(BufferView * bv)
1023 if (!cursor_visible) {
1024 LyXFont font = GetFont(par, actpos);
1026 int asc = font.maxAscent();
1027 int desc = font.maxDescent();
1028 bv->fitLockedInsetCursor(cx, cy, asc, desc);
1029 bv->showLockedInsetCursor(cx, cy, asc, desc);
1030 cursor_visible = true;
1035 void InsetText::HideInsetCursor(BufferView * bv)
1038 ToggleInsetCursor(bv);
1042 void InsetText::setPos(BufferView * bv, int x, int y, bool activate_inset)
1047 // search right X-pos x==0 -> top_x
1048 actpos = actrow = 0;
1051 for(unsigned int i = 1;
1052 ((cy + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
1053 cy = rows[i].baseline;
1054 actpos = rows[i].pos;
1062 int sw = swh = SingleWidth(bv->getPainter(), par,actpos);
1063 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
1065 while ((actpos < (rows[actrow + 1].pos - 1)) && ((cx + swh) < x)) {
1068 sw = swh = SingleWidth(bv->getPainter(), par,actpos);
1069 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
1072 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
1074 static_cast<UpdatableInset*>(par->GetInset(actpos));
1075 inset_x = cx - top_x;
1078 the_locking_inset->Edit(bv, ox - inset_x, oy - inset_y, 0);
1083 bool InsetText::moveRight(BufferView * bv, bool activate_inset)
1085 if (actpos >= par->Last())
1087 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
1089 static_cast<UpdatableInset*>(par->GetInset(actpos));
1090 inset_x = cx - top_x;
1093 the_locking_inset->Edit(bv, 0, 0, 0);
1102 bool InsetText::moveLeft(BufferView * bv, bool activate_inset)
1107 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
1109 static_cast<UpdatableInset*>(par->GetInset(actpos));
1111 inset_x = cx - top_x;
1114 the_locking_inset->Edit(bv, the_locking_inset->
1115 width(bv->getPainter(), GetFont(par,actpos)),
1124 bool InsetText::moveUp(BufferView * bv, bool activate_inset)
1128 cy = rows[actrow - 1].baseline - top_baseline;
1129 setPos(bv, cx - top_x, cy, activate_inset);
1134 bool InsetText::moveDown(BufferView * bv, bool activate_inset)
1136 if (actrow >= int(rows.size() - 2))
1138 cy = rows[actrow + 1].baseline - top_baseline;
1139 setPos(bv, cx - top_x, cy, activate_inset);
1144 void InsetText::resetPos(BufferView * bv)
1146 int old_pos = actpos;
1150 for(int i = 0; rows[i].pos <= actpos; ++i) {
1151 cy = rows[i].baseline;
1155 setPos(bv, 0, cy, false);
1157 while(actpos < old_pos) {
1158 cx += SingleWidth(bv->getPainter(), par,actpos);
1164 bool InsetText::Delete()
1166 /* some insets are undeletable here */
1167 if (par->GetChar(actpos)==LyXParagraph::META_INSET) {
1168 /* force complete redo when erasing display insets */
1169 /* this is a cruel mathod but save..... Matthias */
1170 if (par->GetInset(actpos)->Deletable() &&
1171 par->GetInset(actpos)->display()) {
1182 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1184 par->InsertChar(actpos, LyXParagraph::META_INSET);
1185 par->InsertInset(actpos, inset);
1186 computeTextRows(bv->getPainter());
1187 bv->updateInset(this, true);
1188 the_locking_inset = static_cast<UpdatableInset*>(inset);
1189 inset_x = cx - top_x;
1192 inset->Edit(bv, 0, 0, 0);
1197 UpdatableInset * InsetText::GetLockingInset()
1199 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1203 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1205 // if there is no selection just set the current_font
1206 if (!hasSelection()) {
1207 // Determine basis font
1209 if (actpos < BeginningOfMainBody(par))
1210 layoutfont = GetFont(par, -2);
1212 layoutfont = GetFont(par, -1);
1214 // Update current font
1215 real_current_font.update(font, toggleall);
1217 // Reduce to implicit settings
1218 current_font = real_current_font;
1219 current_font.reduce(layoutfont);
1220 // And resolve it completely
1221 real_current_font.realize(layoutfont);
1226 if (selection_start > selection_end) {
1227 s_start = selection_end;
1228 s_end = selection_start;
1230 s_start = selection_start;
1231 s_end = selection_end;
1234 while(s_start < s_end) {
1235 newfont = GetFont(par,s_start);
1236 newfont.update(font, toggleall);
1237 SetCharFont(s_start, newfont);
1240 computeTextRows(bv->getPainter());
1241 bv->updateInset(this, true);
1245 void InsetText::SetCharFont(int pos, LyXFont const & f)
1247 /* let the insets convert their font */
1250 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1251 if (par->GetInset(pos))
1252 font = par->GetInset(pos)->ConvertFont(font);
1254 LyXLayout const & layout =
1255 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1257 // Get concrete layout font to reduce against
1260 if (pos < BeginningOfMainBody(par))
1261 layoutfont = layout.labelfont;
1263 layoutfont = layout.font;
1266 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1269 // Now, reduce font against full layout font
1270 font.reduce(layoutfont);
1272 par->SetFont(pos, font);
1276 void InsetText::computeTextRows(Painter & pain) const
1290 rows.erase(rows.begin(),rows.end());
1291 int width = wordAscent = wordDescent = 0;
1292 insetWidth = maxAscent = maxDescent = 0;
1297 rows.push_back(row);
1299 for(p = 0; p < par->Last(); ++p) {
1300 insetWidth += SingleWidth(pain, par, p);
1301 SingleHeight(pain, par, p, asc, desc);
1302 if (asc > maxAscent)
1304 if (desc > maxDescent)
1307 rows[0].asc = maxAscent;
1308 rows[0].desc = maxDescent;
1309 // alocate a dummy row for the endpos
1310 row.pos = par->Last();
1311 rows.push_back(row);
1314 bool is_first_word_in_row = true;
1319 for(p = 0; p < par->Last(); ++p) {
1320 cw = SingleWidth(pain, par, p);
1322 lastWordWidth += cw;
1323 SingleHeight(pain, par, p, asc, desc);
1324 if (asc > wordAscent)
1326 if (desc > wordDescent)
1328 Inset const * inset = 0;
1329 if (((p + 1) < par->Last()) &&
1330 (par->GetChar(p + 1)==LyXParagraph::META_INSET))
1331 inset = par->GetInset(p + 1);
1332 if (inset && inset->display()) {
1333 if (!is_first_word_in_row && (width >= maxWidth)) {
1334 // we have to split also the row above
1335 rows[rows.size() - 1].asc = oasc;
1336 rows[rows.size() - 1].desc = odesc;
1338 rows.push_back(row);
1340 odesc = wordDescent;
1341 if (insetWidth < owidth)
1342 insetWidth = owidth;
1343 width = lastWordWidth;
1346 if (oasc < wordAscent)
1348 if (odesc < wordDescent)
1349 odesc = wordDescent;
1351 rows[rows.size() - 1].asc = oasc;
1352 rows[rows.size() - 1].desc = odesc;
1354 rows.push_back(row);
1355 SingleHeight(pain, par, p, asc, desc);
1356 rows[rows.size() - 1].asc = asc;
1357 rows[rows.size() - 1].desc = desc;
1358 row.pos = nwp = p + 1;
1359 rows.push_back(row);
1360 oasc = odesc = width = lastWordWidth = 0;
1361 is_first_word_in_row = true;
1362 wordAscent = wordDescent = 0;
1364 } else if (par->IsSeparator(p)) {
1365 if (width >= maxWidth) {
1366 if (is_first_word_in_row) {
1367 rows[rows.size() - 1].asc = wordAscent;
1368 rows[rows.size() - 1].desc = wordDescent;
1370 rows.push_back(row);
1371 oasc = odesc = width = 0;
1373 rows[rows.size() - 1].asc = oasc;
1374 rows[rows.size() - 1].desc = odesc;
1376 rows.push_back(row);
1378 odesc = wordDescent;
1379 if (insetWidth < owidth)
1380 insetWidth = owidth;
1381 width = lastWordWidth;
1383 wordAscent = wordDescent = lastWordWidth = 0;
1388 if (oasc < wordAscent)
1390 if (odesc < wordDescent)
1391 odesc = wordDescent;
1392 wordAscent = wordDescent = lastWordWidth = 0;
1394 is_first_word_in_row = false;
1397 // if we have some data in the paragraph we have ascent/descent
1399 if (width >= maxWidth) {
1401 rows[rows.size() - 1].asc = oasc;
1402 rows[rows.size() - 1].desc = odesc;
1403 // assign and allocate lower row
1405 rows.push_back(row);
1406 rows[rows.size() - 1].asc = wordAscent;
1407 rows[rows.size() - 1].desc = wordDescent;
1408 if (insetWidth < owidth)
1409 insetWidth = owidth;
1411 if (insetWidth < width)
1414 // assign last row data
1415 if (oasc < wordAscent)
1417 if (odesc < wordDescent)
1418 odesc = wordDescent;
1419 rows[rows.size() - 1].asc = oasc;
1420 rows[rows.size() - 1].desc = odesc;
1423 // alocate a dummy row for the endpos
1424 row.pos = par->Last();
1425 rows.push_back(row);
1426 // calculate maxAscent/Descent
1427 maxAscent = rows[0].asc;
1428 maxDescent = rows[0].desc;
1429 for (unsigned int i = 1; i < rows.size() - 1; ++i) {
1430 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1433 if (the_locking_inset) {
1434 computeBaselines(top_baseline);
1437 inset_x = cx - top_x;
1444 void InsetText::computeBaselines(int baseline) const
1446 rows[0].baseline = baseline;
1447 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1448 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1449 rows[i].asc + interline_space;