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 "BufferView.h"
15 #include "lyxparagraph.h"
16 #include "insets/inseterror.h"
17 #include "lyx_gui_misc.h"
18 #include "lyxcursor.h"
21 #pragma implementation
26 extern BufferView * current_view;
28 // Jürgen, note that this means that you cannot currently have a list
29 // of selections cut/copied. So IMHO later we should have a
30 // list/vector/deque that we could store
31 // struct selection_item {
32 // LyXParagraph * buf;
33 // LyXTextClassList::size_type textclass;
35 // in and some method of choosing beween them (based on the first few chars
36 // in the selection probably.) This would be a nice feature and quite
37 // easy to implement. (Lgb)
39 // Sure but I just cleaned up this code for now with the same functionality
40 // as before. I also want to add a XClipboard function so that we can copy
41 // text from LyX to some other X-application in the form of ASCII or in the
42 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
43 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
44 // then do a middle mouse button click in the application you want and have
45 // the whole formula there in LaTeX-Code. (Jug)
47 static LyXParagraph * buf = 0;
48 static LyXTextClassList::size_type textclass = 0;
50 // for now here this should be in another Cut&Paste Class!
51 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
52 // member of the CutAndPaste class and in addition it was private.
53 // Perhaps it even should take a parameter? (Lgb)
60 LyXParagraph * tmppar;
75 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
76 int start, int & end, char tc, bool doclear)
79 if (!startpar || (start > startpar->Last()))
82 if (!startpar || (start > startpar->size()))
92 (startpar->ParFromPos(start) ==
93 (*endpar)->ParFromPos(end))
95 (startpar == (*endpar))
98 // only within one paragraph
99 buf = new LyXParagraph;
100 LyXParagraph::size_type i = start;
102 if (end > startpar->Last())
103 end = startpar->Last();
105 if (end > startpar->size())
106 end = startpar->size();
108 for (; i < end; ++i) {
109 startpar->CopyIntoMinibuffer(*current_view->buffer(), start);
110 startpar->Erase(start);
113 buf->InsertFromMinibuffer(buf->Last());
115 buf->InsertFromMinibuffer(buf->size());
120 // more than one paragraph
121 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
123 *endpar = (*endpar)->next();
126 startpar->BreakParagraphConservative(current_view->buffer()->params,
129 // store the selection
131 buf = startpar->ParFromPos(start)->next_;
133 buf = startpar->next();
136 (*endpar)->previous()->next(0);
140 startpar->ParFromPos(start)->next(*endpar);
142 (*endpar)->previous(startpar->ParFromPos(start));
144 startpar->next(*endpar);
146 (*endpar)->previous(startpar);
150 // care about footnotes
151 if (buf->footnoteflag) {
152 LyXParagraph * tmppar = buf;
154 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
155 tmppar = tmppar->next_;
160 // the cut selection should begin with standard layout
163 // paste the paragraphs again, if possible
165 startpar->next()->StripLeadingSpaces(textclass);
167 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->next()) ||
168 !startpar->next()->Last()) {
170 if (startpar->HasSameLayout(startpar->next()) ||
171 !startpar->next()->size()) {
174 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
176 startpar->PasteParagraph(current_view->buffer()->params);
178 (*endpar) = startpar; // this because endpar gets deleted here!
185 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
186 int start, int end, char tc)
189 if (!startpar || (start > startpar->Last()))
192 if (!startpar || (start > startpar->size()))
202 (startpar->ParFromPos(start) ==
203 (endpar)->ParFromPos(end))
208 // only within one paragraph
209 buf = new LyXParagraph;
210 LyXParagraph::size_type i = start;
212 if (end > startpar->Last())
213 end = startpar->Last();
215 if (end > startpar->size())
216 end = startpar->size();
218 for (; i < end; ++i) {
219 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
221 buf->InsertFromMinibuffer(buf->Last());
223 buf->InsertFromMinibuffer(buf->size());
227 // copy more than one paragraph
228 // clone the paragraphs within the selection
230 LyXParagraph * tmppar = startpar->ParFromPos(start);
232 LyXParagraph * tmppar = startpar;
234 buf = tmppar->Clone();
235 LyXParagraph * tmppar2 = buf;
238 while (tmppar != endpar->ParFromPos(end)
240 tmppar = tmppar->next_;
241 tmppar2->next(tmppar->Clone());
242 tmppar2->next_->previous(tmppar2);
243 tmppar2 = tmppar2->next_;
247 while (tmppar != endpar
249 tmppar = tmppar->next();
250 tmppar2->next(tmppar->Clone());
251 tmppar2->next()->previous(tmppar2);
252 tmppar2 = tmppar2->next();
258 // care about footnotes
259 if (buf->footnoteflag) {
262 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
263 tmppar = tmppar->next_;
268 // the buf paragraph is too big
270 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
272 LyXParagraph::size_type tmpi2 = start;
274 for (; tmpi2; --tmpi2)
277 // now tmppar 2 is too big, delete all after end
279 tmpi2 = endpar->PositionInParFromPos(end);
283 while (tmppar2->size() > tmpi2) {
284 tmppar2->Erase(tmppar2->size() - 1);
291 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
294 if (!checkPastePossible(*par))
298 if (pos > (*par)->Last())
299 pos = (*par)->Last();
301 if (pos > (*par)->size())
302 pos = (*par)->size();
305 LyXParagraph * tmpbuf;
306 LyXParagraph * tmppar = *par;
309 // There are two cases: cutbuffer only one paragraph or many
315 // only within a paragraph
316 tmpbuf = buf->Clone();
317 // Some provisions should be done here for checking
318 // if we are inserting at the beginning of a
319 // paragraph. If there are a space at the beginning
320 // of the text to insert and we are inserting at
321 // the beginning of the paragraph the space should
323 while (buf->size()) {
324 // This is an attempt to fix the
325 // "never insert a space at the
326 // beginning of a paragraph" problem.
327 if (!tmppos && buf->IsLineSeparator(0)) {
330 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
332 if (tmppar->InsertFromMinibuffer(tmppos))
338 *endpar = tmppar->next();
343 // make a copy of the simple cut_buffer
345 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
346 LyXParagraph * tmpbuf2 = simple_cut_clone;
348 if ((*par)->footnoteflag) {
349 tmpbuf->footnoteflag = (*par)->footnoteflag;
350 tmpbuf->footnotekind = (*par)->footnotekind;
352 while (tmpbuf->next_) {
353 tmpbuf = tmpbuf->next_;
354 tmpbuf2->next(tmpbuf->Clone());
355 tmpbuf2->next_->previous(tmpbuf2);
356 tmpbuf2 = tmpbuf2->next_;
357 if ((*par)->footnoteflag){
358 tmpbuf->footnoteflag = (*par)->footnoteflag;
359 tmpbuf->footnotekind = (*par)->footnotekind;
363 while (tmpbuf->next()) {
364 tmpbuf = tmpbuf->next();
365 tmpbuf2->next(tmpbuf->Clone());
366 tmpbuf2->next()->previous(tmpbuf2);
367 tmpbuf2 = tmpbuf2->next();
371 // make sure there is no class difference
372 SwitchLayoutsBetweenClasses(textclass, tc, buf);
374 // make the buf exactly the same layout than
375 // the cursor paragraph
376 buf->MakeSameLayout(*par);
378 // find the end of the buffer
379 LyXParagraph * lastbuffer = buf;
380 while (lastbuffer->next())
381 lastbuffer = lastbuffer->next();
383 bool paste_the_end = false;
386 // open the paragraph for inserting the buf
388 if (((*par)->Last() > pos) || !(*par)->next()) {
389 (*par)->BreakParagraphConservative(current_view->buffer()->params,
391 paste_the_end = true;
393 // set the end for redoing later
394 *endpar = (*par)->ParFromPos(pos)->next_->next();
397 lastbuffer->ParFromPos(lastbuffer->Last())->next(
398 (*par)->ParFromPos(pos)->next_);
399 (*par)->ParFromPos(pos)->next()->previous(
400 lastbuffer->ParFromPos(lastbuffer->Last()));
402 (*par)->ParFromPos(pos)->next(buf);
403 buf->previous((*par)->ParFromPos(pos));
405 if ((*par)->ParFromPos(pos)->next() == lastbuffer)
408 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
409 // store the new cursor position
411 pos = lastbuffer->Last();
412 // maybe some pasting
413 if (lastbuffer->next() && paste_the_end) {
414 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
415 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
416 } else if (!lastbuffer->next()->Last()) {
417 lastbuffer->next()->MakeSameLayout(lastbuffer);
418 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
419 } else if (!lastbuffer->Last()) {
420 lastbuffer->MakeSameLayout(lastbuffer->next_);
421 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
423 lastbuffer->next()->StripLeadingSpaces(tc);
425 // restore the simple cut buffer
426 buf = simple_cut_clone;
429 // open the paragraph for inserting the buf
431 if (((*par)->size() > pos) || !(*par)->next()) {
432 (*par)->BreakParagraphConservative(current_view->buffer()->params,
434 paste_the_end = true;
436 // set the end for redoing later
437 *endpar = (*par)->next()->next();
440 lastbuffer->next((*par)->next());
441 (*par)->next()->previous(lastbuffer);
446 if ((*par)->next() == lastbuffer)
449 (*par)->PasteParagraph(current_view->buffer()->params);
450 // store the new cursor position
452 pos = lastbuffer->size();
453 // maybe some pasting
454 if (lastbuffer->next() && paste_the_end) {
455 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
456 lastbuffer->PasteParagraph(current_view->buffer()->params);
457 } else if (!lastbuffer->next()->size()) {
458 lastbuffer->next()->MakeSameLayout(lastbuffer);
459 lastbuffer->PasteParagraph(current_view->buffer()->params);
460 } else if (!lastbuffer->size()) {
461 lastbuffer->MakeSameLayout(lastbuffer->next());
462 lastbuffer->PasteParagraph(current_view->buffer()->params);
464 lastbuffer->next()->StripLeadingSpaces(tc);
466 // restore the simple cut buffer
467 buf = simple_cut_clone;
475 int CutAndPaste::nrOfParagraphs()
480 LyXParagraph * tmppar = buf;
482 while(tmppar->next_) {
484 tmppar = tmppar->next_;
487 while(tmppar->next()) {
489 tmppar = tmppar->next();
496 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
497 LyXTextClassList::size_type c2,
501 if (!par || c1 == c2)
504 par = par->FirstPhysicalPar();
507 string name = textclasslist.NameOfLayout(c1, par->layout);
509 pair<bool, LyXTextClass::LayoutList::size_type> pp =
510 textclasslist.NumberOfLayout(c2, name);
513 } else { // layout not found
514 // use default layout "Standard" (0)
519 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
521 string s = _("Layout had to be changed from\n")
523 + textclasslist.NameOfLayout(c2, par->layout)
524 + _("\nbecause of class conversion from\n")
525 + textclasslist.NameOfClass(c1) + _(" to ")
526 + textclasslist.NameOfClass(c2);
527 InsetError * new_inset = new InsetError(s);
528 par->InsertInset(0, new_inset);
540 bool CutAndPaste::checkPastePossible(LyXParagraph * par)
542 if (!buf) return false;
545 // be carefull with footnotes in footnotes
546 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
547 // check whether the cut_buffer includes a footnote
548 LyXParagraph * tmppar = buf;
549 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
550 tmppar = tmppar->next_;
553 WriteAlert(_("Impossible operation"),
554 _("Can't paste float into float!"),