1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2000 The LyX Team.
8 * ====================================================== */
12 #include "CutAndPaste.h"
13 #include "lyxparagraph.h"
14 #include "insets/inseterror.h"
15 #include "lyx_gui_misc.h"
18 #pragma implementation
23 static LyXParagraph * buf = 0;
25 // for now here this should be in another Cut&Paste Class!
27 void CutAndPaste::DeleteBuffer()
31 LyXParagraph * tmppar;
42 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
43 int start, int & end, char tc, bool doclear)
45 if (!startpar || (start > startpar->Last()))
52 if (!(*endpar) || (startpar->ParFromPos(start) ==
53 (*endpar)->ParFromPos(end))) {
54 // only within one paragraph
55 buf = new LyXParagraph;
56 LyXParagraph::size_type i = start;
57 if (end > startpar->Last())
58 end = startpar->Last();
59 for (; i < end; ++i) {
60 startpar->CopyIntoMinibuffer(start);
61 /* table stuff -- begin */
62 if (startpar->table && startpar->IsNewline(start)) {
65 /* table stuff -- end */
66 startpar->Erase(start);
68 buf->InsertFromMinibuffer(buf->Last());
71 // more than one paragraph
72 (*endpar)->BreakParagraphConservative(end);
73 *endpar = (*endpar)->Next();
76 startpar->BreakParagraphConservative(start);
78 // store the selection
79 buf = startpar->ParFromPos(start)->next;
81 (*endpar)->previous->next = 0;
84 startpar->ParFromPos(start)->next = (*endpar);
86 (*endpar)->previous = startpar->ParFromPos(start);
88 // care about footnotes
89 if (buf->footnoteflag) {
90 LyXParagraph * tmppar = buf;
92 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
93 tmppar = tmppar->next;
97 // the cut selection should begin with standard layout
100 // paste the paragraphs again, if possible
102 startpar->Next()->ClearParagraph();
103 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->Next()) ||
104 !startpar->Next()->Last())
105 startpar->ParFromPos(start)->PasteParagraph();
111 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
112 int start, int end, char tc)
114 if (!startpar || (start > startpar->Last()))
121 if (!(endpar) || (startpar->ParFromPos(start) ==
122 (endpar)->ParFromPos(end))) {
123 // only within one paragraph
124 buf = new LyXParagraph;
125 LyXParagraph::size_type i = start;
126 if (end > startpar->Last())
127 end = startpar->Last();
128 for (; i < end; ++i) {
129 startpar->CopyIntoMinibuffer(i);
130 buf->InsertFromMinibuffer(buf->Last());
133 // copy more than one paragraph
134 // clone the paragraphs within the selection
135 LyXParagraph *tmppar = startpar->ParFromPos(start);
136 buf = tmppar->Clone();
137 LyXParagraph *tmppar2 = buf;
139 while (tmppar != endpar->ParFromPos(end)
141 tmppar = tmppar->next;
142 tmppar2->next = tmppar->Clone();
143 tmppar2->next->previous = tmppar2;
144 tmppar2 = tmppar2->next;
148 // care about footnotes
149 if (buf->footnoteflag) {
152 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
153 tmppar = tmppar->next;
157 // the buf paragraph is too big
158 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
159 for (; tmpi2; --tmpi2)
162 // now tmppar 2 is too big, delete all after end
164 tmpi2 = endpar->PositionInParFromPos(end);
165 while (tmppar2->size() > tmpi2) {
166 tmppar2->Erase(tmppar2->size() - 1);
173 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
176 if (!checkPastePossible(*par, pos))
179 if (pos > (*par)->Last())
180 pos = (*par)->Last();
182 LyXParagraph * tmpbuf;
183 LyXParagraph * tmppar = *par;
186 // There are two cases: cutbuffer only one paragraph or many
188 // only within a paragraph
189 tmpbuf = buf->Clone();
190 /* table stuff -- begin */
191 bool table_too_small = false;
193 while (buf->size() && !table_too_small) {
194 if (buf->IsNewline(0)){
195 while((tmppos < tmppar->Last()) &&
196 !tmppar->IsNewline(tmppos))
199 if (tmppos < tmppar->Last())
202 table_too_small = true;
204 // This is an attempt to fix the
205 // "never insert a space at the
206 // beginning of a paragraph" problem.
207 if (!tmppos && buf->IsLineSeparator(0)) {
210 buf->CutIntoMinibuffer(0);
212 if (tmppar->InsertFromMinibuffer(tmppos))
218 /* table stuff -- end */
219 // Some provisions should be done here for checking
220 // if we are inserting at the beginning of a
221 // paragraph. If there are a space at the beginning
222 // of the text to insert and we are inserting at
223 // the beginning of the paragraph the space should
225 while (buf->size()) {
226 // This is an attempt to fix the
227 // "never insert a space at the
228 // beginning of a paragraph" problem.
229 if (!tmppos && buf->IsLineSeparator(0)) {
232 buf->CutIntoMinibuffer(0);
234 if (tmppar->InsertFromMinibuffer(tmppos))
241 *endpar = tmppar->Next();
246 // make a copy of the simple cut_buffer
248 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
249 LyXParagraph * tmpbuf2 = simple_cut_clone;
250 if ((*par)->footnoteflag){
251 tmpbuf->footnoteflag = (*par)->footnoteflag;
252 tmpbuf->footnotekind = (*par)->footnotekind;
254 while (tmpbuf->next) {
255 tmpbuf = tmpbuf->next;
256 tmpbuf2->next = tmpbuf->Clone();
257 tmpbuf2->next->previous = tmpbuf2;
258 tmpbuf2 = tmpbuf2->next;
259 if ((*par)->footnoteflag){
260 tmpbuf->footnoteflag = (*par)->footnoteflag;
261 tmpbuf->footnotekind = (*par)->footnotekind;
265 // make sure there is no class difference
266 SwitchLayoutsBetweenClasses(textclass, tc, buf);
268 // make the buf exactly the same layout than
269 // the cursor paragraph
270 buf->MakeSameLayout(*par);
272 // find the end of the buffer
273 LyXParagraph * lastbuffer = buf;
274 while (lastbuffer->Next())
275 lastbuffer = lastbuffer->Next();
277 bool paste_the_end = false;
279 // open the paragraph for inserting the buf
281 if (((*par)->Last() > pos) || !(*par)->Next()) {
282 (*par)->BreakParagraphConservative(pos);
283 paste_the_end = true;
286 // set the end for redoing later
287 *endpar = (*par)->ParFromPos(pos)->next->Next();
290 lastbuffer->ParFromPos(lastbuffer->Last())->next =
291 (*par)->ParFromPos(pos)->next;
292 (*par)->ParFromPos(pos)->next->previous =
293 lastbuffer->ParFromPos(lastbuffer->Last());
295 (*par)->ParFromPos(pos)->next = buf;
296 buf->previous = (*par)->ParFromPos(pos);
298 if ((*par)->ParFromPos(pos)->Next() == lastbuffer)
301 (*par)->ParFromPos(pos)->PasteParagraph();
303 // store the new cursor position
305 tmppos = lastbuffer->Last();
307 // maybe some pasting
308 if (lastbuffer->Next() && paste_the_end) {
309 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
310 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
311 } else if (!lastbuffer->Next()->Last()) {
312 lastbuffer->Next()->MakeSameLayout(lastbuffer);
313 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
314 } else if (!lastbuffer->Last()) {
315 lastbuffer->MakeSameLayout(lastbuffer->next);
316 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
318 lastbuffer->Next()->ClearParagraph();
320 // restore the simple cut buffer
321 buf = simple_cut_clone;
329 int CutAndPaste::nrOfParagraphs() const
334 LyXParagraph *tmppar = buf;
335 while(tmppar->next) {
337 tmppar = tmppar->next;
343 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
344 LyXTextClassList::size_type c2,
348 if (!par || c1 == c2)
350 par = par->FirstPhysicalPar();
352 string name = textclasslist.NameOfLayout(c1, par->layout);
354 pair<bool, LyXTextClass::LayoutList::size_type> pp =
355 textclasslist.NumberOfLayout(c2, name);
358 } else { // layout not found
359 // use default layout "Standard" (0)
364 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
366 string s = "Layout had to be changed from\n"
368 + textclasslist.NameOfLayout(c2, par->layout)
369 + "\nbecause of class conversion from\n"
370 + textclasslist.NameOfClass(c1) + " to "
371 + textclasslist.NameOfClass(c2);
372 InsetError * new_inset = new InsetError(s);
373 par->InsertChar(0, LyXParagraph::META_INSET);
374 par->InsertInset(0, new_inset);
383 LyXTextClassList::size_type CutAndPaste::getBufferTextClass() const
389 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int) const
391 if (!buf) return false;
393 LyXParagraph * tmppar;
395 // be carefull with footnotes in footnotes
396 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
397 // check whether the cut_buffer includes a footnote
399 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
400 tmppar = tmppar->next;
403 WriteAlert(_("Impossible operation"),
404 _("Can't paste float into float!"),
409 /* table stuff -- begin */
412 WriteAlert(_("Impossible operation"),
413 _("Table cell cannot include more than one paragraph!"),
418 /* table stuff -- end */