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"
20 #include "iterators.h"
23 #pragma implementation
28 using lyx::layout_type;
29 using lyx::textclass_type;
31 extern BufferView * current_view;
33 // Jürgen, note that this means that you cannot currently have a list
34 // of selections cut/copied. So IMHO later we should have a
35 // list/vector/deque that we could store
36 // struct selection_item {
38 // LyXTextClassList::size_type textclass;
40 // in and some method of choosing beween them (based on the first few chars
41 // in the selection probably.) This would be a nice feature and quite
42 // easy to implement. (Lgb)
44 // Sure but I just cleaned up this code for now with the same functionality
45 // as before. I also want to add a XClipboard function so that we can copy
46 // text from LyX to some other X-application in the form of ASCII or in the
47 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
48 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
49 // then do a middle mouse button click in the application you want and have
50 // the whole formula there in LaTeX-Code. (Jug)
55 textclass_type textclass = 0;
57 // for now here this should be in another Cut&Paste Class!
58 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
59 // member of the CutAndPaste class and in addition it was private.
60 // Perhaps it even should take a parameter? (Lgb)
79 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
80 int start, int & end, char tc, bool doclear,
83 if (!startpar || (start > startpar->size()))
91 if (!(*endpar) || startpar == (*endpar)) {
92 // only within one paragraph
96 if (end > startpar->size())
97 end = startpar->size();
98 for (; i < end; ++i) {
100 startpar->copyIntoMinibuffer(*current_view->buffer(),
102 startpar->erase(start);
104 buf->insertFromMinibuffer(buf->size());
108 // more than one paragraph
109 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
111 *endpar = (*endpar)->next();
114 startpar->breakParagraphConservative(current_view->buffer()->params,
117 // store the selection
119 buf = startpar->next();
122 startpar->next()->previous(0);
124 (*endpar)->previous()->next(0);
127 startpar->next(*endpar);
129 (*endpar)->previous(startpar);
131 // the cut selection should begin with standard layout
135 // paste the paragraphs again, if possible
137 startpar->next()->stripLeadingSpaces(textclass);
138 if (startpar->hasSameLayout(startpar->next()) ||
139 !startpar->next()->size()) {
140 startpar->pasteParagraph(current_view->buffer()->params);
141 (*endpar) = startpar; // this because endpar gets deleted here!
143 // this paragraph's are of noone's owner!
154 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
155 int start, int end, char tc)
157 if (!startpar || (start > startpar->size()))
164 if (!endpar || startpar == endpar) {
165 // only within one paragraph
168 if (end > startpar->size())
169 end = startpar->size();
170 for (; i < end; ++i) {
171 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
172 buf->insertFromMinibuffer(buf->size());
175 // copy more than one paragraph
176 // clone the paragraphs within the selection
177 Paragraph * tmppar = startpar;
178 buf = new Paragraph(*tmppar, false);
179 Paragraph * tmppar2 = buf;
181 while (tmppar != endpar
183 tmppar = tmppar->next();
184 tmppar2->next(new Paragraph(*tmppar, false));
185 tmppar2->next()->previous(tmppar2);
186 tmppar2 = tmppar2->next();
190 // the buf paragraph is too big
191 pos_type tmpi2 = start;
192 for (; tmpi2; --tmpi2)
195 // now tmppar 2 is too big, delete all after end
197 while (tmppar2->size() > tmpi2) {
198 tmppar2->erase(tmppar2->size() - 1);
200 // this paragraph's are of noone's owner!
203 tmppar->setInsetOwner(0);
204 tmppar = tmppar->next();
211 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
214 if (!checkPastePossible(*par))
217 if (pos > (*par)->size())
218 pos = (*par)->size();
220 // Paragraph * tmpbuf;
221 Paragraph * tmppar = *par;
224 // There are two cases: cutbuffer only one paragraph or many
226 // only within a paragraph
227 Paragraph * tmpbuf = new Paragraph(*buf, false);
229 // Some provisions should be done here for checking
230 // if we are inserting at the beginning of a
231 // paragraph. If there are a space at the beginning
232 // of the text to insert and we are inserting at
233 // the beginning of the paragraph the space should
235 while (buf->size()) {
236 // This is an attempt to fix the
237 // "never insert a space at the
238 // beginning of a paragraph" problem.
239 if (!tmppos && buf->isLineSeparator(0)) {
242 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
244 if (tmppar->insertFromMinibuffer(tmppos))
250 *endpar = tmppar->next();
255 // make a copy of the simple cut_buffer
256 Paragraph * tmpbuf = buf;
257 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
258 Paragraph * tmpbuf2 = simple_cut_clone;
260 while (tmpbuf->next()) {
261 tmpbuf = tmpbuf->next();
262 tmpbuf2->next(new Paragraph(*tmpbuf, false));
263 tmpbuf2->next()->previous(tmpbuf2);
264 tmpbuf2 = tmpbuf2->next();
267 // now remove all out of the buffer which is NOT allowed in the
268 // new environment and set also another font if that is required
271 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
272 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
273 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
278 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
280 if (!(*par)->checkInsertChar(f1)) {
282 } else if (f1 != f2) {
283 tmpbuf->setFont(i, f1);
287 tmpbuf = tmpbuf->next();
290 // make sure there is no class difference
291 SwitchLayoutsBetweenClasses(textclass, tc, buf,
292 current_view->buffer()->params);
294 // make the buf exactly the same layout than
295 // the cursor paragraph
296 buf->makeSameLayout(*par);
298 // find the end of the buffer
299 Paragraph * lastbuffer = buf;
300 while (lastbuffer->next())
301 lastbuffer = lastbuffer->next();
303 bool paste_the_end = false;
305 // open the paragraph for inserting the buf
307 if (((*par)->size() > pos) || !(*par)->next()) {
308 (*par)->breakParagraphConservative(current_view->buffer()->params,
310 paste_the_end = true;
312 // set the end for redoing later
313 *endpar = (*par)->next()->next();
316 lastbuffer->next((*par)->next());
317 (*par)->next()->previous(lastbuffer);
322 if ((*par)->next() == lastbuffer)
325 (*par)->pasteParagraph(current_view->buffer()->params);
326 // store the new cursor position
328 pos = lastbuffer->size();
329 // maybe some pasting
330 if (lastbuffer->next() && paste_the_end) {
331 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
332 lastbuffer->pasteParagraph(current_view->buffer()->params);
333 } else if (!lastbuffer->next()->size()) {
334 lastbuffer->next()->makeSameLayout(lastbuffer);
335 lastbuffer->pasteParagraph(current_view->buffer()->params);
336 } else if (!lastbuffer->size()) {
337 lastbuffer->makeSameLayout(lastbuffer->next());
338 lastbuffer->pasteParagraph(current_view->buffer()->params);
340 lastbuffer->next()->stripLeadingSpaces(tc);
342 // restore the simple cut buffer
343 buf = simple_cut_clone;
350 int CutAndPaste::nrOfParagraphs()
356 Paragraph * tmppar = buf;
357 while (tmppar->next()) {
359 tmppar = tmppar->next();
365 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
368 BufferParams const & bparams)
371 if (!par || c1 == c2)
374 ParIterator end = ParIterator();
375 for (ParIterator it = ParIterator(par); it != end; ++it) {
377 string const name = textclasslist.NameOfLayout(c1, par->layout);
379 pair<bool, layout_type> pp =
380 textclasslist.NumberOfLayout(c2, name);
384 // not found: use default layout "Standard" (0)
389 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
391 string const s = _("Layout had to be changed from\n")
393 + textclasslist.NameOfLayout(c2, par->layout)
394 + _("\nbecause of class conversion from\n")
395 + textclasslist.NameOfClass(c1) + _(" to ")
396 + textclasslist.NameOfClass(c2);
397 InsetError * new_inset = new InsetError(s);
398 par->insertInset(0, new_inset,
399 LyXFont(LyXFont::ALL_INHERIT,
407 bool CutAndPaste::checkPastePossible(Paragraph *)
409 if (!buf) return false;