1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
13 #pragma implementation
16 #include "CutAndPaste.h"
18 #include "BufferView.h"
20 #include "paragraph.h"
21 #include "ParagraphParameters.h"
23 #include "lyxcursor.h"
25 #include "iterators.h"
26 #include "lyxtextclasslist.h"
27 #include "undo_funcs.h"
28 #include "paragraph_funcs.h"
30 #include "insets/inseterror.h"
34 using lyx::textclass_type;
36 extern BufferView * current_view;
38 // Jürgen, note that this means that you cannot currently have a list
39 // of selections cut/copied. So IMHO later we should have a
40 // list/vector/deque that we could store
41 // struct selection_item {
43 // LyXTextClassList::size_type textclass;
45 // in and some method of choosing beween them (based on the first few chars
46 // in the selection probably.) This would be a nice feature and quite
47 // easy to implement. (Lgb)
49 // Sure but I just cleaned up this code for now with the same functionality
50 // as before. I also want to add a XClipboard function so that we can copy
51 // text from LyX to some other X-application in the form of ASCII or in the
52 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
53 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
54 // then do a middle mouse button click in the application you want and have
55 // the whole formula there in LaTeX-Code. (Jug)
60 textclass_type textclass = 0;
62 // for now here this should be in another Cut&Paste Class!
63 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
64 // member of the CutAndPaste class and in addition it was private.
65 // Perhaps it even should take a parameter? (Lgb)
84 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
85 int start, int & end, char tc, bool doclear,
88 if (!startpar || (start > startpar->size()))
96 if (!(*endpar) || startpar == (*endpar)) {
97 // only within one paragraph
100 buf->layout(startpar->layout());
103 if (end > startpar->size())
104 end = startpar->size();
105 for (; i < end; ++i) {
107 startpar->copyIntoMinibuffer(*current_view->buffer(),
109 startpar->erase(start);
111 buf->insertFromMinibuffer(buf->size());
115 // more than one paragraph
116 breakParagraphConservative(current_view->buffer()->params,
119 *endpar = (*endpar)->next();
122 breakParagraphConservative(current_view->buffer()->params,
126 // store the selection
128 buf = startpar->next();
131 startpar->next()->previous(0);
133 (*endpar)->previous()->next(0);
136 startpar->next(*endpar);
138 (*endpar)->previous(startpar);
140 // the cut selection should begin with standard layout
142 buf->params().clear();
144 buf->layout(current_view->buffer()->params.getLyXTextClass().defaultLayout());
147 // paste the paragraphs again, if possible
149 startpar->next()->stripLeadingSpaces();
150 if (startpar->hasSameLayout(startpar->next()) ||
151 startpar->next()->empty()) {
152 pasteParagraph(current_view->buffer()->params, startpar);
153 (*endpar) = startpar; // this because endpar gets deleted here!
155 // this paragraph's are of noone's owner!
166 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
167 int start, int end, char tc)
169 if (!startpar || (start > startpar->size()))
176 if (!endpar || startpar == endpar) {
177 // only within one paragraph
179 buf->layout(startpar->layout());
181 if (end > startpar->size())
182 end = startpar->size();
183 for (; i < end; ++i) {
184 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
185 buf->insertFromMinibuffer(buf->size());
188 // copy more than one paragraph
189 // clone the paragraphs within the selection
190 Paragraph * tmppar = startpar;
191 buf = new Paragraph(*tmppar, false);
192 Paragraph * tmppar2 = buf;
194 while (tmppar != endpar
196 tmppar = tmppar->next();
197 tmppar2->next(new Paragraph(*tmppar, false));
198 tmppar2->next()->previous(tmppar2);
199 tmppar2 = tmppar2->next();
203 // the buf paragraph is too big
204 pos_type tmpi2 = start;
205 for (; tmpi2; --tmpi2)
208 // now tmppar 2 is too big, delete all after end
210 while (tmppar2->size() > tmpi2) {
211 tmppar2->erase(tmppar2->size() - 1);
213 // this paragraph's are of noone's owner!
216 tmppar->setInsetOwner(0);
217 tmppar = tmppar->next();
224 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
227 if (!checkPastePossible(*par))
230 if (pos > (*par)->size())
231 pos = (*par)->size();
234 // Paragraph * tmpbuf;
235 Paragraph * tmppar = *par;
238 // There are two cases: cutbuffer only one paragraph or many
240 // only within a paragraph
241 Paragraph * tmpbuf = new Paragraph(*buf, false);
243 // Some provisions should be done here for checking
244 // if we are inserting at the beginning of a
245 // paragraph. If there are a space at the beginning
246 // of the text to insert and we are inserting at
247 // the beginning of the paragraph the space should
249 while (buf->size()) {
250 // This is an attempt to fix the
251 // "never insert a space at the
252 // beginning of a paragraph" problem.
253 if (!tmppos && buf->isLineSeparator(0)) {
256 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
258 if (tmppar->insertFromMinibuffer(tmppos))
264 *endpar = tmppar->next();
271 // make a copy of the simple cut_buffer
272 Paragraph * tmpbuf = buf;
273 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
274 Paragraph * tmpbuf2 = simple_cut_clone;
276 while (tmpbuf->next()) {
277 tmpbuf = tmpbuf->next();
278 tmpbuf2->next(new Paragraph(*tmpbuf, false));
279 tmpbuf2->next()->previous(tmpbuf2);
280 tmpbuf2 = tmpbuf2->next();
283 // now remove all out of the buffer which is NOT allowed in the
284 // new environment and set also another font if that is required
286 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
287 // temporary set *par as previous of tmpbuf as we might have to realize
289 tmpbuf->previous(*par);
291 // make sure there is no class difference
292 SwitchLayoutsBetweenClasses(textclass, tc, tmpbuf,
293 current_view->buffer()->params);
295 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
298 // if we have a negative jump so that the depth would go below
299 // 0 depth then we have to redo the delta to this new max depth
300 // level so that subsequent paragraphs are aligned correctly to
301 // this paragraph at level 0.
302 if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
304 // set the right depth so that we are not too deep or shallow.
305 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
306 if (tmpbuf->params().depth() > max_depth)
307 tmpbuf->params().depth(max_depth);
308 // only set this from the 2nd on as the 2nd depends for maxDepth
310 if (tmpbuf->previous() != (*par))
311 max_depth = tmpbuf->getMaxDepthAfter();
312 // set the inset owner of this paragraph
313 tmpbuf->setInsetOwner((*par)->inInset());
314 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
315 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
316 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
321 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
323 if (!(*par)->checkInsertChar(f1)) {
325 } else if (f1 != f2) {
326 tmpbuf->setFont(i, f1);
330 tmpbuf = tmpbuf->next();
335 // make the buf exactly the same layout than
336 // the cursor paragraph
337 buf->makeSameLayout(*par);
339 // find the end of the buffer
340 Paragraph * lastbuffer = buf;
341 while (lastbuffer->next())
342 lastbuffer = lastbuffer->next();
344 bool paste_the_end = false;
346 // open the paragraph for inserting the buf
348 if (((*par)->size() > pos) || !(*par)->next()) {
349 breakParagraphConservative(
350 current_view->buffer()->params,
353 paste_the_end = true;
355 // set the end for redoing later
356 *endpar = (*par)->next()->next();
359 lastbuffer->next((*par)->next());
360 (*par)->next()->previous(lastbuffer);
365 if ((*par)->next() == lastbuffer)
368 pasteParagraph(current_view->buffer()->params, *par);
369 // store the new cursor position
371 pos = lastbuffer->size();
372 // maybe some pasting
373 if (lastbuffer->next() && paste_the_end) {
374 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
375 pasteParagraph(current_view->buffer()->params, lastbuffer);
376 } else if (lastbuffer->next()->empty()) {
377 lastbuffer->next()->makeSameLayout(lastbuffer);
378 pasteParagraph(current_view->buffer()->params, lastbuffer);
379 } else if (lastbuffer->empty()) {
380 lastbuffer->makeSameLayout(lastbuffer->next());
381 pasteParagraph(current_view->buffer()->params, lastbuffer);
383 lastbuffer->next()->stripLeadingSpaces();
385 // restore the simple cut buffer
386 buf = simple_cut_clone;
393 int CutAndPaste::nrOfParagraphs()
399 Paragraph * tmppar = buf;
400 while (tmppar->next()) {
402 tmppar = tmppar->next();
408 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
411 BufferParams const & /*bparams*/)
414 if (!par || c1 == c2)
417 LyXTextClass const & tclass1 = textclasslist[c1];
418 LyXTextClass const & tclass2 = textclasslist[c2];
419 ParIterator end = ParIterator();
420 for (ParIterator it = ParIterator(par); it != end; ++it) {
422 string const name = par->layout()->name();
423 bool hasLayout = tclass2.hasLayout(name);
426 par->layout(tclass2.defaultLayout());
428 if (!hasLayout && name != tclass1.defaultLayoutName()) {
430 string const s = _("Layout had to be changed from\n")
432 + par->layout()->name()
433 + _("\nbecause of class conversion from\n")
434 + tclass1.name() + _(" to ")
437 InsetError * new_inset = new InsetError(s);
438 LyXText * txt = current_view->getLyXText();
439 LyXCursor cur = txt->cursor;
440 txt->setCursorIntern(current_view, par, 0);
441 txt->insertInset(current_view, new_inset);
442 txt->fullRebreak(current_view);
443 txt->setCursorIntern(current_view, cur.par(), cur.pos());
451 bool CutAndPaste::checkPastePossible(Paragraph *)
453 if (!buf) return false;