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 "insets/inseterror.h"
32 using lyx::textclass_type;
34 extern BufferView * current_view;
36 // Jürgen, note that this means that you cannot currently have a list
37 // of selections cut/copied. So IMHO later we should have a
38 // list/vector/deque that we could store
39 // struct selection_item {
41 // LyXTextClassList::size_type textclass;
43 // in and some method of choosing beween them (based on the first few chars
44 // in the selection probably.) This would be a nice feature and quite
45 // easy to implement. (Lgb)
47 // Sure but I just cleaned up this code for now with the same functionality
48 // as before. I also want to add a XClipboard function so that we can copy
49 // text from LyX to some other X-application in the form of ASCII or in the
50 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
51 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
52 // then do a middle mouse button click in the application you want and have
53 // the whole formula there in LaTeX-Code. (Jug)
58 textclass_type textclass = 0;
60 // for now here this should be in another Cut&Paste Class!
61 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
62 // member of the CutAndPaste class and in addition it was private.
63 // Perhaps it even should take a parameter? (Lgb)
82 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
83 int start, int & end, char tc, bool doclear,
86 if (!startpar || (start > startpar->size()))
94 if (!(*endpar) || startpar == (*endpar)) {
95 // only within one paragraph
98 buf->layout(startpar->layout());
101 if (end > startpar->size())
102 end = startpar->size();
103 for (; i < end; ++i) {
105 startpar->copyIntoMinibuffer(*current_view->buffer(),
107 startpar->erase(start);
109 buf->insertFromMinibuffer(buf->size());
113 // more than one paragraph
114 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
116 *endpar = (*endpar)->next();
119 startpar->breakParagraphConservative(current_view->buffer()->params,
122 // store the selection
124 buf = startpar->next();
127 startpar->next()->previous(0);
129 (*endpar)->previous()->next(0);
132 startpar->next(*endpar);
134 (*endpar)->previous(startpar);
136 // the cut selection should begin with standard layout
138 buf->params().clear();
140 buf->layout(current_view->buffer()->params.getLyXTextClass().defaultLayout());
143 // paste the paragraphs again, if possible
145 startpar->next()->stripLeadingSpaces();
146 if (startpar->hasSameLayout(startpar->next()) ||
147 startpar->next()->empty()) {
148 startpar->pasteParagraph(current_view->buffer()->params);
149 (*endpar) = startpar; // this because endpar gets deleted here!
151 // this paragraph's are of noone's owner!
162 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
163 int start, int end, char tc)
165 if (!startpar || (start > startpar->size()))
172 if (!endpar || startpar == endpar) {
173 // only within one paragraph
175 buf->layout(startpar->layout());
177 if (end > startpar->size())
178 end = startpar->size();
179 for (; i < end; ++i) {
180 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
181 buf->insertFromMinibuffer(buf->size());
184 // copy more than one paragraph
185 // clone the paragraphs within the selection
186 Paragraph * tmppar = startpar;
187 buf = new Paragraph(*tmppar, false);
188 Paragraph * tmppar2 = buf;
190 while (tmppar != endpar
192 tmppar = tmppar->next();
193 tmppar2->next(new Paragraph(*tmppar, false));
194 tmppar2->next()->previous(tmppar2);
195 tmppar2 = tmppar2->next();
199 // the buf paragraph is too big
200 pos_type tmpi2 = start;
201 for (; tmpi2; --tmpi2)
204 // now tmppar 2 is too big, delete all after end
206 while (tmppar2->size() > tmpi2) {
207 tmppar2->erase(tmppar2->size() - 1);
209 // this paragraph's are of noone's owner!
212 tmppar->setInsetOwner(0);
213 tmppar = tmppar->next();
220 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
223 if (!checkPastePossible(*par))
226 if (pos > (*par)->size())
227 pos = (*par)->size();
230 // Paragraph * tmpbuf;
231 Paragraph * tmppar = *par;
234 // There are two cases: cutbuffer only one paragraph or many
236 // only within a paragraph
237 Paragraph * tmpbuf = new Paragraph(*buf, false);
239 // Some provisions should be done here for checking
240 // if we are inserting at the beginning of a
241 // paragraph. If there are a space at the beginning
242 // of the text to insert and we are inserting at
243 // the beginning of the paragraph the space should
245 while (buf->size()) {
246 // This is an attempt to fix the
247 // "never insert a space at the
248 // beginning of a paragraph" problem.
249 if (!tmppos && buf->isLineSeparator(0)) {
252 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
254 if (tmppar->insertFromMinibuffer(tmppos))
260 *endpar = tmppar->next();
267 // make a copy of the simple cut_buffer
268 Paragraph * tmpbuf = buf;
269 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
270 Paragraph * tmpbuf2 = simple_cut_clone;
272 while (tmpbuf->next()) {
273 tmpbuf = tmpbuf->next();
274 tmpbuf2->next(new Paragraph(*tmpbuf, false));
275 tmpbuf2->next()->previous(tmpbuf2);
276 tmpbuf2 = tmpbuf2->next();
279 // now remove all out of the buffer which is NOT allowed in the
280 // new environment and set also another font if that is required
282 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
283 // temporary set *par as previous of tmpbuf as we might have to realize
285 tmpbuf->previous(*par);
287 // make sure there is no class difference
288 SwitchLayoutsBetweenClasses(textclass, tc, tmpbuf,
289 current_view->buffer()->params);
291 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
294 // if we have a negative jump so that the depth would go below
295 // 0 depth then we have to redo the delta to this new max depth
296 // level so that subsequent paragraphs are aligned correctly to
297 // this paragraph at level 0.
298 if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
300 // set the right depth so that we are not too deep or shallow.
301 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
302 if (tmpbuf->params().depth() > max_depth)
303 tmpbuf->params().depth(max_depth);
304 // only set this from the 2nd on as the 2nd depends for maxDepth
306 if (tmpbuf->previous() != (*par))
307 max_depth = tmpbuf->getMaxDepthAfter();
308 // set the inset owner of this paragraph
309 tmpbuf->setInsetOwner((*par)->inInset());
310 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
311 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
312 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
317 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
319 if (!(*par)->checkInsertChar(f1)) {
321 } else if (f1 != f2) {
322 tmpbuf->setFont(i, f1);
326 tmpbuf = tmpbuf->next();
331 // make the buf exactly the same layout than
332 // the cursor paragraph
333 buf->makeSameLayout(*par);
335 // find the end of the buffer
336 Paragraph * lastbuffer = buf;
337 while (lastbuffer->next())
338 lastbuffer = lastbuffer->next();
340 bool paste_the_end = false;
342 // open the paragraph for inserting the buf
344 if (((*par)->size() > pos) || !(*par)->next()) {
345 (*par)->breakParagraphConservative(current_view->buffer()->params,
347 paste_the_end = true;
349 // set the end for redoing later
350 *endpar = (*par)->next()->next();
353 lastbuffer->next((*par)->next());
354 (*par)->next()->previous(lastbuffer);
359 if ((*par)->next() == lastbuffer)
362 (*par)->pasteParagraph(current_view->buffer()->params);
363 // store the new cursor position
365 pos = lastbuffer->size();
366 // maybe some pasting
367 if (lastbuffer->next() && paste_the_end) {
368 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
369 lastbuffer->pasteParagraph(current_view->buffer()->params);
370 } else if (lastbuffer->next()->empty()) {
371 lastbuffer->next()->makeSameLayout(lastbuffer);
372 lastbuffer->pasteParagraph(current_view->buffer()->params);
373 } else if (lastbuffer->empty()) {
374 lastbuffer->makeSameLayout(lastbuffer->next());
375 lastbuffer->pasteParagraph(current_view->buffer()->params);
377 lastbuffer->next()->stripLeadingSpaces();
379 // restore the simple cut buffer
380 buf = simple_cut_clone;
387 int CutAndPaste::nrOfParagraphs()
393 Paragraph * tmppar = buf;
394 while (tmppar->next()) {
396 tmppar = tmppar->next();
402 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
405 BufferParams const & /*bparams*/)
408 if (!par || c1 == c2)
411 LyXTextClass const & tclass1 = textclasslist[c1];
412 LyXTextClass const & tclass2 = textclasslist[c2];
413 ParIterator end = ParIterator();
414 for (ParIterator it = ParIterator(par); it != end; ++it) {
416 string const name = par->layout()->name();
417 bool hasLayout = tclass2.hasLayout(name);
420 par->layout(tclass2.defaultLayout());
422 if (!hasLayout && name != tclass1.defaultLayoutName()) {
424 string const s = _("Layout had to be changed from\n")
426 + par->layout()->name()
427 + _("\nbecause of class conversion from\n")
428 + tclass1.name() + _(" to ")
431 InsetError * new_inset = new InsetError(s);
432 LyXText * txt = current_view->getLyXText();
433 LyXCursor cur = txt->cursor;
434 txt->setCursorIntern(current_view, par, 0);
435 txt->insertInset(current_view, new_inset);
436 txt->fullRebreak(current_view);
437 txt->setCursorIntern(current_view, cur.par(), cur.pos());
445 bool CutAndPaste::checkPastePossible(Paragraph *)
447 if (!buf) return false;