1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
12 #include "CutAndPaste.h"
13 #include "BufferView.h"
15 #include "paragraph.h"
16 #include "insets/inseterror.h"
17 #include "lyx_gui_misc.h"
18 #include "lyxcursor.h"
22 #pragma implementation
28 extern BufferView * current_view;
30 // Jürgen, note that this means that you cannot currently have a list
31 // of selections cut/copied. So IMHO later we should have a
32 // list/vector/deque that we could store
33 // struct selection_item {
35 // LyXTextClassList::size_type textclass;
37 // in and some method of choosing beween them (based on the first few chars
38 // in the selection probably.) This would be a nice feature and quite
39 // easy to implement. (Lgb)
41 // Sure but I just cleaned up this code for now with the same functionality
42 // as before. I also want to add a XClipboard function so that we can copy
43 // text from LyX to some other X-application in the form of ASCII or in the
44 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
45 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
46 // then do a middle mouse button click in the application you want and have
47 // the whole formula there in LaTeX-Code. (Jug)
52 LyXTextClassList::size_type textclass = 0;
54 // for now here this should be in another Cut&Paste Class!
55 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
56 // member of the CutAndPaste class and in addition it was private.
57 // Perhaps it even should take a parameter? (Lgb)
76 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
77 int start, int & end, char tc, bool doclear,
80 if (!startpar || (start > startpar->size()))
88 if (!(*endpar) || startpar == (*endpar)) {
89 // only within one paragraph
93 if (end > startpar->size())
94 end = startpar->size();
95 for (; i < end; ++i) {
97 startpar->copyIntoMinibuffer(*current_view->buffer(),
99 startpar->erase(start);
101 buf->insertFromMinibuffer(buf->size());
105 // more than one paragraph
106 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
108 *endpar = (*endpar)->next();
111 startpar->breakParagraphConservative(current_view->buffer()->params,
114 // store the selection
116 buf = startpar->next();
119 startpar->next()->previous(0);
121 (*endpar)->previous()->next(0);
124 startpar->next(*endpar);
126 (*endpar)->previous(startpar);
128 // the cut selection should begin with standard layout
132 // paste the paragraphs again, if possible
134 startpar->next()->stripLeadingSpaces(textclass);
135 if (startpar->hasSameLayout(startpar->next()) ||
136 !startpar->next()->size()) {
137 startpar->pasteParagraph(current_view->buffer()->params);
138 (*endpar) = startpar; // this because endpar gets deleted here!
140 // this paragraph's are of noone's owner!
151 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
152 int start, int end, char tc)
154 if (!startpar || (start > startpar->size()))
161 if (!endpar || startpar == endpar) {
162 // only within one paragraph
165 if (end > startpar->size())
166 end = startpar->size();
167 for (; i < end; ++i) {
168 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
169 buf->insertFromMinibuffer(buf->size());
172 // copy more than one paragraph
173 // clone the paragraphs within the selection
174 Paragraph * tmppar = startpar;
175 buf = new Paragraph(*tmppar, false);
176 Paragraph * tmppar2 = buf;
178 while (tmppar != endpar
180 tmppar = tmppar->next();
181 tmppar2->next(new Paragraph(*tmppar, false));
182 tmppar2->next()->previous(tmppar2);
183 tmppar2 = tmppar2->next();
187 // the buf paragraph is too big
188 pos_type tmpi2 = start;
189 for (; tmpi2; --tmpi2)
192 // now tmppar 2 is too big, delete all after end
194 while (tmppar2->size() > tmpi2) {
195 tmppar2->erase(tmppar2->size() - 1);
197 // this paragraph's are of noone's owner!
200 tmppar->setInsetOwner(0);
201 tmppar = tmppar->next();
208 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
211 if (!checkPastePossible(*par))
214 if (pos > (*par)->size())
215 pos = (*par)->size();
217 // Paragraph * tmpbuf;
218 Paragraph * tmppar = *par;
221 // There are two cases: cutbuffer only one paragraph or many
223 // only within a paragraph
224 Paragraph * tmpbuf = new Paragraph(*buf, false);
226 // Some provisions should be done here for checking
227 // if we are inserting at the beginning of a
228 // paragraph. If there are a space at the beginning
229 // of the text to insert and we are inserting at
230 // the beginning of the paragraph the space should
232 while (buf->size()) {
233 // This is an attempt to fix the
234 // "never insert a space at the
235 // beginning of a paragraph" problem.
236 if (!tmppos && buf->isLineSeparator(0)) {
239 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
241 if (tmppar->insertFromMinibuffer(tmppos))
247 *endpar = tmppar->next();
252 // make a copy of the simple cut_buffer
253 Paragraph * tmpbuf = buf;
254 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
255 Paragraph * tmpbuf2 = simple_cut_clone;
257 while (tmpbuf->next()) {
258 tmpbuf = tmpbuf->next();
259 tmpbuf2->next(new Paragraph(*tmpbuf, false));
260 tmpbuf2->next()->previous(tmpbuf2);
261 tmpbuf2 = tmpbuf2->next();
264 // make sure there is no class difference
265 SwitchLayoutsBetweenClasses(textclass, tc, buf);
267 // make the buf exactly the same layout than
268 // the cursor paragraph
269 buf->makeSameLayout(*par);
271 // find the end of the buffer
272 Paragraph * lastbuffer = buf;
273 while (lastbuffer->next())
274 lastbuffer = lastbuffer->next();
276 bool paste_the_end = false;
278 // open the paragraph for inserting the buf
280 if (((*par)->size() > pos) || !(*par)->next()) {
281 (*par)->breakParagraphConservative(current_view->buffer()->params,
283 paste_the_end = true;
285 // set the end for redoing later
286 *endpar = (*par)->next()->next();
289 lastbuffer->next((*par)->next());
290 (*par)->next()->previous(lastbuffer);
295 if ((*par)->next() == lastbuffer)
298 (*par)->pasteParagraph(current_view->buffer()->params);
299 // store the new cursor position
301 pos = lastbuffer->size();
302 // maybe some pasting
303 if (lastbuffer->next() && paste_the_end) {
304 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
305 lastbuffer->pasteParagraph(current_view->buffer()->params);
306 } else if (!lastbuffer->next()->size()) {
307 lastbuffer->next()->makeSameLayout(lastbuffer);
308 lastbuffer->pasteParagraph(current_view->buffer()->params);
309 } else if (!lastbuffer->size()) {
310 lastbuffer->makeSameLayout(lastbuffer->next());
311 lastbuffer->pasteParagraph(current_view->buffer()->params);
313 lastbuffer->next()->stripLeadingSpaces(tc);
315 // restore the simple cut buffer
316 buf = simple_cut_clone;
323 int CutAndPaste::nrOfParagraphs()
329 Paragraph * tmppar = buf;
330 while(tmppar->next()) {
332 tmppar = tmppar->next();
338 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
339 LyXTextClassList::size_type c2,
343 if (!par || c1 == c2)
347 string const name = textclasslist.NameOfLayout(c1,
350 pair<bool, LyXTextClass::LayoutList::size_type> pp =
351 textclasslist.NumberOfLayout(c2, name);
354 } else { // layout not found
355 // use default layout "Standard" (0)
360 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
362 string const s = _("Layout had to be changed from\n")
364 + textclasslist.NameOfLayout(c2, par->layout)
365 + _("\nbecause of class conversion from\n")
366 + textclasslist.NameOfClass(c1) + _(" to ")
367 + textclasslist.NameOfClass(c2);
368 InsetError * new_inset = new InsetError(s);
369 par->insertInset(0, new_inset);
377 bool CutAndPaste::checkPastePossible(Paragraph *)
379 if (!buf) return false;