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;
24 static LyXTextClassList::size_type textclass;
26 // for now here this should be in another Cut&Paste Class!
28 void CutAndPaste::DeleteBuffer()
32 LyXParagraph * tmppar;
43 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
44 int start, int & end, char tc, bool doclear)
46 if (!startpar || (start > startpar->Last()))
53 if (!(*endpar) || (startpar->ParFromPos(start) ==
54 (*endpar)->ParFromPos(end))) {
55 // only within one paragraph
56 buf = new LyXParagraph;
57 LyXParagraph::size_type i = start;
58 if (end > startpar->Last())
59 end = startpar->Last();
60 for (; i < end; ++i) {
61 startpar->CopyIntoMinibuffer(start);
62 /* table stuff -- begin */
63 if (startpar->table && startpar->IsNewline(start)) {
66 /* table stuff -- end */
67 startpar->Erase(start);
69 buf->InsertFromMinibuffer(buf->Last());
72 // more than one paragraph
73 (*endpar)->BreakParagraphConservative(end);
74 *endpar = (*endpar)->Next();
77 startpar->BreakParagraphConservative(start);
79 // store the selection
80 buf = startpar->ParFromPos(start)->next;
82 (*endpar)->previous->next = 0;
85 startpar->ParFromPos(start)->next = (*endpar);
87 (*endpar)->previous = startpar->ParFromPos(start);
89 // care about footnotes
90 if (buf->footnoteflag) {
91 LyXParagraph * tmppar = buf;
93 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
94 tmppar = tmppar->next;
98 // the cut selection should begin with standard layout
101 // paste the paragraphs again, if possible
103 startpar->Next()->ClearParagraph();
104 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->Next()) ||
105 !startpar->Next()->Last())
106 startpar->ParFromPos(start)->PasteParagraph();
112 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
113 int start, int end, char tc)
115 if (!startpar || (start > startpar->Last()))
122 if (!(endpar) || (startpar->ParFromPos(start) ==
123 (endpar)->ParFromPos(end))) {
124 // only within one paragraph
125 buf = new LyXParagraph;
126 LyXParagraph::size_type i = start;
127 if (end > startpar->Last())
128 end = startpar->Last();
129 for (; i < end; ++i) {
130 startpar->CopyIntoMinibuffer(i);
131 buf->InsertFromMinibuffer(buf->Last());
134 // copy more than one paragraph
135 // clone the paragraphs within the selection
136 LyXParagraph *tmppar = startpar->ParFromPos(start);
137 buf = tmppar->Clone();
138 LyXParagraph *tmppar2 = buf;
140 while (tmppar != endpar->ParFromPos(end)
142 tmppar = tmppar->next;
143 tmppar2->next = tmppar->Clone();
144 tmppar2->next->previous = tmppar2;
145 tmppar2 = tmppar2->next;
149 // care about footnotes
150 if (buf->footnoteflag) {
153 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
154 tmppar = tmppar->next;
158 // the buf paragraph is too big
159 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
160 for (; tmpi2; --tmpi2)
163 // now tmppar 2 is too big, delete all after end
165 tmpi2 = endpar->PositionInParFromPos(end);
166 while (tmppar2->size() > tmpi2) {
167 tmppar2->Erase(tmppar2->size() - 1);
174 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
177 if (!checkPastePossible(*par, pos))
180 if (pos > (*par)->Last())
181 pos = (*par)->Last();
183 LyXParagraph * tmpbuf;
184 LyXParagraph * tmppar = *par;
187 // There are two cases: cutbuffer only one paragraph or many
189 // only within a paragraph
190 tmpbuf = buf->Clone();
191 /* table stuff -- begin */
192 bool table_too_small = false;
194 while (buf->size() && !table_too_small) {
195 if (buf->IsNewline(0)){
196 while((tmppos < tmppar->Last()) &&
197 !tmppar->IsNewline(tmppos))
200 if (tmppos < tmppar->Last())
203 table_too_small = true;
205 // This is an attempt to fix the
206 // "never insert a space at the
207 // beginning of a paragraph" problem.
208 if (!tmppos && buf->IsLineSeparator(0)) {
211 buf->CutIntoMinibuffer(0);
213 if (tmppar->InsertFromMinibuffer(tmppos))
219 /* table stuff -- end */
220 // Some provisions should be done here for checking
221 // if we are inserting at the beginning of a
222 // paragraph. If there are a space at the beginning
223 // of the text to insert and we are inserting at
224 // the beginning of the paragraph the space should
226 while (buf->size()) {
227 // This is an attempt to fix the
228 // "never insert a space at the
229 // beginning of a paragraph" problem.
230 if (!tmppos && buf->IsLineSeparator(0)) {
233 buf->CutIntoMinibuffer(0);
235 if (tmppar->InsertFromMinibuffer(tmppos))
242 *endpar = tmppar->Next();
247 // make a copy of the simple cut_buffer
249 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
250 LyXParagraph * tmpbuf2 = simple_cut_clone;
251 if ((*par)->footnoteflag){
252 tmpbuf->footnoteflag = (*par)->footnoteflag;
253 tmpbuf->footnotekind = (*par)->footnotekind;
255 while (tmpbuf->next) {
256 tmpbuf = tmpbuf->next;
257 tmpbuf2->next = tmpbuf->Clone();
258 tmpbuf2->next->previous = tmpbuf2;
259 tmpbuf2 = tmpbuf2->next;
260 if ((*par)->footnoteflag){
261 tmpbuf->footnoteflag = (*par)->footnoteflag;
262 tmpbuf->footnotekind = (*par)->footnotekind;
266 // make sure there is no class difference
267 SwitchLayoutsBetweenClasses(textclass, tc, buf);
269 // make the buf exactly the same layout than
270 // the cursor paragraph
271 buf->MakeSameLayout(*par);
273 // find the end of the buffer
274 LyXParagraph * lastbuffer = buf;
275 while (lastbuffer->Next())
276 lastbuffer = lastbuffer->Next();
278 bool paste_the_end = false;
280 // open the paragraph for inserting the buf
282 if (((*par)->Last() > pos) || !(*par)->Next()) {
283 (*par)->BreakParagraphConservative(pos);
284 paste_the_end = true;
287 // set the end for redoing later
288 *endpar = (*par)->ParFromPos(pos)->next->Next();
291 lastbuffer->ParFromPos(lastbuffer->Last())->next =
292 (*par)->ParFromPos(pos)->next;
293 (*par)->ParFromPos(pos)->next->previous =
294 lastbuffer->ParFromPos(lastbuffer->Last());
296 (*par)->ParFromPos(pos)->next = buf;
297 buf->previous = (*par)->ParFromPos(pos);
299 if ((*par)->ParFromPos(pos)->Next() == lastbuffer)
302 (*par)->ParFromPos(pos)->PasteParagraph();
304 // store the new cursor position
306 tmppos = lastbuffer->Last();
308 // maybe some pasting
309 if (lastbuffer->Next() && paste_the_end) {
310 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
311 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
312 } else if (!lastbuffer->Next()->Last()) {
313 lastbuffer->Next()->MakeSameLayout(lastbuffer);
314 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
315 } else if (!lastbuffer->Last()) {
316 lastbuffer->MakeSameLayout(lastbuffer->next);
317 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
319 lastbuffer->Next()->ClearParagraph();
321 // restore the simple cut buffer
322 buf = simple_cut_clone;
330 int CutAndPaste::nrOfParagraphs() const
335 LyXParagraph *tmppar = buf;
336 while(tmppar->next) {
338 tmppar = tmppar->next;
344 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
345 LyXTextClassList::size_type c2,
349 if (!par || c1 == c2)
351 par = par->FirstPhysicalPar();
353 string name = textclasslist.NameOfLayout(c1, par->layout);
355 pair<bool, LyXTextClass::LayoutList::size_type> pp =
356 textclasslist.NumberOfLayout(c2, name);
359 } else { // layout not found
360 // use default layout "Standard" (0)
365 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
367 string s = "Layout had to be changed from\n"
369 + textclasslist.NameOfLayout(c2, par->layout)
370 + "\nbecause of class conversion from\n"
371 + textclasslist.NameOfClass(c1) + " to "
372 + textclasslist.NameOfClass(c2);
373 InsetError * new_inset = new InsetError(s);
374 par->InsertChar(0, LyXParagraph::META_INSET);
375 par->InsertInset(0, new_inset);
384 LyXTextClassList::size_type CutAndPaste::getBufferTextClass() const
390 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int) const
392 if (!buf) return false;
394 LyXParagraph * tmppar;
396 // be carefull with footnotes in footnotes
397 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
398 // check whether the cut_buffer includes a footnote
400 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
401 tmppar = tmppar->next;
404 WriteAlert(_("Impossible operation"),
405 _("Can't paste float into float!"),
410 /* table stuff -- begin */
413 WriteAlert(_("Impossible operation"),
414 _("Table cell cannot include more than one paragraph!"),
419 /* table stuff -- end */