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"
22 #include "lyxcursor.h"
24 #include "iterators.h"
25 #include "lyxtextclasslist.h"
27 #include "insets/inseterror.h"
31 using lyx::textclass_type;
33 extern BufferView * current_view;
35 // Jürgen, note that this means that you cannot currently have a list
36 // of selections cut/copied. So IMHO later we should have a
37 // list/vector/deque that we could store
38 // struct selection_item {
40 // LyXTextClassList::size_type textclass;
42 // in and some method of choosing beween them (based on the first few chars
43 // in the selection probably.) This would be a nice feature and quite
44 // easy to implement. (Lgb)
46 // Sure but I just cleaned up this code for now with the same functionality
47 // as before. I also want to add a XClipboard function so that we can copy
48 // text from LyX to some other X-application in the form of ASCII or in the
49 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
50 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
51 // then do a middle mouse button click in the application you want and have
52 // the whole formula there in LaTeX-Code. (Jug)
57 textclass_type textclass = 0;
59 // for now here this should be in another Cut&Paste Class!
60 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
61 // member of the CutAndPaste class and in addition it was private.
62 // Perhaps it even should take a parameter? (Lgb)
81 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
82 int start, int & end, char tc, bool doclear,
85 if (!startpar || (start > startpar->size()))
93 if (!(*endpar) || startpar == (*endpar)) {
94 // only within one paragraph
97 buf->layout(startpar->layout());
100 if (end > startpar->size())
101 end = startpar->size();
102 for (; i < end; ++i) {
104 startpar->copyIntoMinibuffer(*current_view->buffer(),
106 startpar->erase(start);
108 buf->insertFromMinibuffer(buf->size());
112 // more than one paragraph
113 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
115 *endpar = (*endpar)->next();
118 startpar->breakParagraphConservative(current_view->buffer()->params,
121 // store the selection
123 buf = startpar->next();
126 startpar->next()->previous(0);
128 (*endpar)->previous()->next(0);
131 startpar->next(*endpar);
133 (*endpar)->previous(startpar);
135 // the cut selection should begin with standard layout
137 buf->params().clear();
139 buf->layout(textclasslist[current_view->buffer()->params.textclass].defaultLayoutName());
142 // paste the paragraphs again, if possible
144 startpar->next()->stripLeadingSpaces(textclass);
145 if (startpar->hasSameLayout(startpar->next()) ||
146 !startpar->next()->size()) {
147 startpar->pasteParagraph(current_view->buffer()->params);
148 (*endpar) = startpar; // this because endpar gets deleted here!
150 // this paragraph's are of noone's owner!
161 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
162 int start, int end, char tc)
164 if (!startpar || (start > startpar->size()))
171 if (!endpar || startpar == endpar) {
172 // only within one paragraph
174 buf->layout(startpar->layout());
176 if (end > startpar->size())
177 end = startpar->size();
178 for (; i < end; ++i) {
179 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
180 buf->insertFromMinibuffer(buf->size());
183 // copy more than one paragraph
184 // clone the paragraphs within the selection
185 Paragraph * tmppar = startpar;
186 buf = new Paragraph(*tmppar, false);
187 Paragraph * tmppar2 = buf;
189 while (tmppar != endpar
191 tmppar = tmppar->next();
192 tmppar2->next(new Paragraph(*tmppar, false));
193 tmppar2->next()->previous(tmppar2);
194 tmppar2 = tmppar2->next();
198 // the buf paragraph is too big
199 pos_type tmpi2 = start;
200 for (; tmpi2; --tmpi2)
203 // now tmppar 2 is too big, delete all after end
205 while (tmppar2->size() > tmpi2) {
206 tmppar2->erase(tmppar2->size() - 1);
208 // this paragraph's are of noone's owner!
211 tmppar->setInsetOwner(0);
212 tmppar = tmppar->next();
219 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
222 if (!checkPastePossible(*par))
225 if (pos > (*par)->size())
226 pos = (*par)->size();
229 // Paragraph * tmpbuf;
230 Paragraph * tmppar = *par;
233 // There are two cases: cutbuffer only one paragraph or many
235 // only within a paragraph
236 Paragraph * tmpbuf = new Paragraph(*buf, false);
238 // Some provisions should be done here for checking
239 // if we are inserting at the beginning of a
240 // paragraph. If there are a space at the beginning
241 // of the text to insert and we are inserting at
242 // the beginning of the paragraph the space should
244 while (buf->size()) {
245 // This is an attempt to fix the
246 // "never insert a space at the
247 // beginning of a paragraph" problem.
248 if (!tmppos && buf->isLineSeparator(0)) {
251 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
253 if (tmppar->insertFromMinibuffer(tmppos))
259 *endpar = tmppar->next();
266 // make a copy of the simple cut_buffer
267 Paragraph * tmpbuf = buf;
268 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
269 Paragraph * tmpbuf2 = simple_cut_clone;
271 while (tmpbuf->next()) {
272 tmpbuf = tmpbuf->next();
273 tmpbuf2->next(new Paragraph(*tmpbuf, false));
274 tmpbuf2->next()->previous(tmpbuf2);
275 tmpbuf2 = tmpbuf2->next();
278 // now remove all out of the buffer which is NOT allowed in the
279 // new environment and set also another font if that is required
281 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
282 // temporary set *par as previous of tmpbuf as we might have to realize
284 tmpbuf->previous(*par);
285 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter(current_view->buffer());
287 // if we have a negative jump so that the depth would go below
288 // 0 depth then we have to redo the delta to this new max depth
289 // level so that subsequent paragraphs are aligned correctly to
290 // this paragraph at level 0.
291 if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
293 // set the right depth so that we are not too deep or shallow.
294 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
295 if (tmpbuf->params().depth() > max_depth)
296 tmpbuf->params().depth(max_depth);
297 // only set this from the 2nd on as the 2nd depends for maxDepth
299 if (tmpbuf->previous() != (*par))
300 max_depth = tmpbuf->getMaxDepthAfter(current_view->buffer());
301 // set the inset owner of this paragraph
302 tmpbuf->setInsetOwner((*par)->inInset());
303 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
304 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
305 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
310 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
312 if (!(*par)->checkInsertChar(f1)) {
314 } else if (f1 != f2) {
315 tmpbuf->setFont(i, f1);
319 tmpbuf = tmpbuf->next();
324 // make sure there is no class difference
325 SwitchLayoutsBetweenClasses(textclass, tc, buf,
326 current_view->buffer()->params);
328 // make the buf exactly the same layout than
329 // the cursor paragraph
330 buf->makeSameLayout(*par);
332 // find the end of the buffer
333 Paragraph * lastbuffer = buf;
334 while (lastbuffer->next())
335 lastbuffer = lastbuffer->next();
337 bool paste_the_end = false;
339 // open the paragraph for inserting the buf
341 if (((*par)->size() > pos) || !(*par)->next()) {
342 (*par)->breakParagraphConservative(current_view->buffer()->params,
344 paste_the_end = true;
346 // set the end for redoing later
347 *endpar = (*par)->next()->next();
350 lastbuffer->next((*par)->next());
351 (*par)->next()->previous(lastbuffer);
356 if ((*par)->next() == lastbuffer)
359 (*par)->pasteParagraph(current_view->buffer()->params);
360 // store the new cursor position
362 pos = lastbuffer->size();
363 // maybe some pasting
364 if (lastbuffer->next() && paste_the_end) {
365 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
366 lastbuffer->pasteParagraph(current_view->buffer()->params);
367 } else if (!lastbuffer->next()->size()) {
368 lastbuffer->next()->makeSameLayout(lastbuffer);
369 lastbuffer->pasteParagraph(current_view->buffer()->params);
370 } else if (!lastbuffer->size()) {
371 lastbuffer->makeSameLayout(lastbuffer->next());
372 lastbuffer->pasteParagraph(current_view->buffer()->params);
374 lastbuffer->next()->stripLeadingSpaces(tc);
376 // restore the simple cut buffer
377 buf = simple_cut_clone;
384 int CutAndPaste::nrOfParagraphs()
390 Paragraph * tmppar = buf;
391 while (tmppar->next()) {
393 tmppar = tmppar->next();
399 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
402 BufferParams const & bparams)
405 if (!par || c1 == c2)
408 ParIterator end = ParIterator();
409 for (ParIterator it = ParIterator(par); it != end; ++it) {
411 string const name = par->layout();
412 LyXTextClass const & tclass = textclasslist[c2];
414 bool hasLayout = tclass.hasLayout(name);
416 string lay = tclass.defaultLayoutName();
420 // not found: use default layout
421 lay = tclass.defaultLayoutName();
425 if (name != par->layout()) {
427 string const s = _("Layout had to be changed from\n")
430 + _("\nbecause of class conversion from\n")
431 + textclasslist[c1].name() + _(" to ")
432 + textclasslist[c2].name();
433 InsetError * new_inset = new InsetError(s);
434 par->insertInset(0, new_inset,
435 LyXFont(LyXFont::ALL_INHERIT,
443 bool CutAndPaste::checkPastePossible(Paragraph *)
445 if (!buf) return false;