]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
Various trivial bits 'n' bats about config.h, header blurb etc.
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braustein
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "BufferView.h"
18 #include "buffer.h"
19 #include "bufferlist.h"
20 #include "BufferView_pimpl.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "iterators.h"
24 #include "language.h"
25 #include "lyxtext.h"
26 #include "paragraph.h"
27 #include "paragraph_funcs.h"
28 #include "undo_funcs.h"
29 #include "WordLangTuple.h"
30
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/screen.h"
35 #include "frontends/WorkArea.h"
36
37 #include "insets/insetcommand.h" // ChangeRefs
38 #include "insets/updatableinset.h"
39
40 #include "support/filetools.h"
41 #include "support/LAssert.h"
42 #include "support/lyxalgo.h" // lyx_count
43
44
45 extern BufferList bufferlist;
46
47 using namespace lyx::support;
48
49 using std::find;
50 using std::vector;
51
52
53 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
54                        int width, int height)
55         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
56 {
57         text = 0;
58 }
59
60
61 BufferView::~BufferView()
62 {
63         delete text;
64         delete pimpl_;
65 }
66
67
68 Buffer * BufferView::buffer() const
69 {
70         return pimpl_->buffer_;
71 }
72
73
74 LyXScreen & BufferView::screen() const
75 {
76         return pimpl_->screen();
77 }
78
79
80 LyXView * BufferView::owner() const
81 {
82         return pimpl_->owner_;
83 }
84
85
86 Painter & BufferView::painter() const
87 {
88         return pimpl_->painter();
89 }
90
91
92 void BufferView::buffer(Buffer * b)
93 {
94         pimpl_->buffer(b);
95 }
96
97
98 bool BufferView::newFile(string const & fn, string const & tn, bool named)
99 {
100         return pimpl_->newFile(fn, tn, named);
101 }
102
103
104 bool BufferView::loadLyXFile(string const & fn, bool tl)
105 {
106         return pimpl_->loadLyXFile(fn, tl);
107 }
108
109
110 void BufferView::reload()
111 {
112         string const fn = buffer()->fileName();
113         if (bufferlist.close(buffer(), false))
114                 loadLyXFile(fn);
115 }
116
117
118 void BufferView::resize()
119 {
120         if (pimpl_->buffer_)
121                 pimpl_->resizeCurrentBuffer();
122 }
123
124
125 bool BufferView::fitCursor()
126 {
127         return pimpl_->fitCursor();
128 }
129
130
131 void BufferView::update()
132 {
133         pimpl_->update();
134 }
135
136
137 void BufferView::updateScrollbar()
138 {
139         pimpl_->updateScrollbar();
140 }
141
142
143 void BufferView::scrollDocView(int value)
144 {
145         pimpl_->scrollDocView(value);
146 }
147
148
149 void BufferView::redoCurrentBuffer()
150 {
151         pimpl_->redoCurrentBuffer();
152 }
153
154
155 bool BufferView::available() const
156 {
157         return pimpl_->available();
158 }
159
160
161 Change const BufferView::getCurrentChange()
162 {
163         return pimpl_->getCurrentChange();
164 }
165
166
167 void BufferView::beforeChange(LyXText * text)
168 {
169         pimpl_->beforeChange(text);
170 }
171
172
173 void BufferView::savePosition(unsigned int i)
174 {
175         pimpl_->savePosition(i);
176 }
177
178
179 void BufferView::restorePosition(unsigned int i)
180 {
181         pimpl_->restorePosition(i);
182 }
183
184
185 bool BufferView::isSavedPosition(unsigned int i)
186 {
187         return pimpl_->isSavedPosition(i);
188 }
189
190
191 void BufferView::switchKeyMap()
192 {
193         pimpl_->switchKeyMap();
194 }
195
196
197 void BufferView::insetUnlock()
198 {
199         pimpl_->insetUnlock();
200 }
201
202
203 int BufferView::workWidth() const
204 {
205         return pimpl_->workarea().workWidth();
206 }
207
208
209 void BufferView::center()
210 {
211         pimpl_->center();
212 }
213
214
215 int BufferView::top_y() const
216 {
217         return pimpl_->top_y();
218 }
219
220
221 void BufferView::top_y(int y)
222 {
223         pimpl_->top_y(y);
224 }
225
226
227 string const BufferView::getClipboard() const
228 {
229         return pimpl_->workarea().getClipboard();
230 }
231
232
233 void BufferView::stuffClipboard(string const & stuff) const
234 {
235         pimpl_->stuffClipboard(stuff);
236 }
237
238
239 bool BufferView::dispatch(FuncRequest const & ev)
240 {
241         return pimpl_->dispatch(ev);
242 }
243
244
245 void BufferView::scroll(int lines)
246 {
247         pimpl_->scroll(lines);
248 }
249
250
251 // Inserts a file into current document
252 bool BufferView::insertLyXFile(string const & filen)
253         //
254         // Copyright CHT Software Service GmbH
255         // Uwe C. Schroeder
256         //
257         // Insert a LyXformat - file into current buffer
258         //
259         // Moved from lyx_cb.C (Lgb)
260 {
261         Assert(!filen.empty());
262
263         string const fname = MakeAbsPath(filen);
264
265         beforeChange(text);
266
267         text->breakParagraph(buffer()->paragraphs);
268
269         bool res = buffer()->readFile(fname, text->cursor.par());
270
271         resize();
272         return res;
273 }
274
275
276 void BufferView::showErrorList(string const & action) const
277 {
278         if (getErrorList().size()) {
279                 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
280                 owner()->getDialogs().show("errorlist", title);
281                 pimpl_->errorlist_.clear();
282         }
283 }
284
285
286 ErrorList const &
287 BufferView::getErrorList() const
288 {
289         return pimpl_->errorlist_;
290 }
291
292
293 void BufferView::setCursorFromRow(int row)
294 {
295         int tmpid = -1;
296         int tmppos = -1;
297
298         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
299
300         ParagraphList::iterator texrowpar;
301
302         if (tmpid == -1) {
303                 texrowpar = text->ownerParagraphs().begin();
304                 tmppos = 0;
305         } else {
306                 texrowpar = buffer()->getParFromID(tmpid).pit();
307         }
308         text->setCursor(texrowpar, tmppos);
309 }
310
311
312 bool BufferView::insertInset(InsetOld * inset, string const & lout)
313 {
314         return pimpl_->insertInset(inset, lout);
315 }
316
317
318 void BufferView::gotoLabel(string const & label)
319 {
320         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
321              it != buffer()->inset_iterator_end(); ++it) {
322                 vector<string> labels;
323                 it->getLabelList(labels);
324                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
325                         beforeChange(text);
326                         text->setCursor(it.getPar(), it.getPos());
327                         text->selection.cursor = text->cursor;
328                         update();
329                         return;
330                 }
331         }
332 }
333
334
335 void BufferView::undo()
336 {
337         if (!available())
338                 return;
339
340         owner()->message(_("Undo"));
341         beforeChange(text);
342         if (!textUndo(this))
343                 owner()->message(_("No further undo information"));
344         update();
345         switchKeyMap();
346 }
347
348
349 void BufferView::redo()
350 {
351         if (!available())
352                 return;
353
354         owner()->message(_("Redo"));
355         beforeChange(text);
356         if (!textRedo(this))
357                 owner()->message(_("No further redo information"));
358         update();
359         switchKeyMap();
360 }
361
362
363 // these functions are for the spellchecker
364 WordLangTuple const BufferView::nextWord(float & value)
365 {
366         if (!available()) {
367                 value = 1;
368                 return WordLangTuple();
369         }
370
371         return text->selectNextWordToSpellcheck(value);
372 }
373
374
375 void BufferView::selectLastWord()
376 {
377         if (!available())
378                 return;
379
380         LyXCursor cur = text->selection.cursor;
381         beforeChange(text);
382         text->selection.cursor = cur;
383         text->selectSelectedWord();
384         update();
385 }
386
387
388 void BufferView::endOfSpellCheck()
389 {
390         if (!available()) return;
391
392         beforeChange(text);
393         text->selectSelectedWord();
394         text->clearSelection();
395         update();
396 }
397
398
399 void BufferView::replaceWord(string const & replacestring)
400 {
401         if (!available())
402                 return;
403
404         LyXText * tt = getLyXText();
405
406         tt->replaceSelectionWithString(replacestring);
407         tt->setSelectionRange(replacestring.length());
408
409         // Go back so that replacement string is also spellchecked
410         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
411                 tt->cursorLeft(this);
412
413         // FIXME: should be done through LFUN
414         buffer()->markDirty();
415         update();
416 }
417
418
419 bool BufferView::lockInset(UpdatableInset * inset)
420 {
421         if (!inset)
422                 return false;
423         // don't relock if we're already locked
424         if (theLockingInset() == inset)
425                 return true;
426         if (!theLockingInset()) {
427                 // first check if it's the inset under the cursor we want lock
428                 // should be most of the time
429                 if (text->cursor.pos() < text->cursor.par()->size()
430                     && text->cursor.par()->getChar(text->cursor.pos()) ==
431                     Paragraph::META_INSET) {
432                         InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
433                         if (inset == in) {
434                                 theLockingInset(inset);
435                                 return true;
436                         }
437                 }
438                 // Then do a deep look of the inset and lock the right one
439                 int const id = inset->id();
440                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
441                 ParagraphList::iterator pend = buffer()->paragraphs.end();
442                 for (; pit != pend; ++pit) {
443                         InsetList::iterator it = pit->insetlist.begin();
444                         InsetList::iterator end = pit->insetlist.end();
445                         for (; it != end; ++it) {
446                                 if (it->inset == inset) {
447                                         text->setCursorIntern(pit, it->pos);
448                                         theLockingInset(inset);
449                                         return true;
450                                 }
451                                 if (it->inset->getInsetFromID(id)) {
452                                         text->setCursorIntern(pit, it->pos);
453                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
454                                         it->inset->localDispatch(cmd);
455                                         return theLockingInset()->lockInsetInInset(this, inset);
456                                 }
457                         }
458                 }
459                 return false;
460         }
461         return theLockingInset()->lockInsetInInset(this, inset);
462 }
463
464
465 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
466 {
467         if (theLockingInset() && available()) {
468                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
469                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
470                         updateScrollbar();
471                         return true;
472                 }
473         }
474         return false;
475 }
476
477
478 void BufferView::hideCursor()
479 {
480         screen().hideCursor();
481 }
482
483
484 int BufferView::unlockInset(UpdatableInset * inset)
485 {
486         if (!inset)
487                 return 0;
488         if (inset && theLockingInset() == inset) {
489                 inset->insetUnlock(this);
490                 theLockingInset(0);
491                 // make sure we update the combo !
492                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
493                 // Tell the paragraph dialog that we changed paragraph
494                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
495                 finishUndo();
496                 return 0;
497         } else if (inset && theLockingInset() &&
498                    theLockingInset()->unlockInsetInInset(this, inset)) {
499                 // Tell the paragraph dialog that we changed paragraph
500                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
501                 // owner inset has updated the layout combo
502                 finishUndo();
503                 return 0;
504         }
505         return 1;
506 }
507
508
509 void BufferView::updateInset(InsetOld const * inset)
510 {
511         pimpl_->updateInset(inset);
512 }
513
514
515 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
516 {
517         // Check if the label 'from' appears more than once
518         vector<string> labels;
519         buffer()->getLabelList(labels);
520
521         if (lyx::count(labels.begin(), labels.end(), from) > 1)
522                 return false;
523
524         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
525 }
526
527
528 UpdatableInset * BufferView::theLockingInset() const
529 {
530         // If NULL is not allowed we should put an Assert here. (Lgb)
531         if (text)
532                 return text->the_locking_inset;
533         return 0;
534 }
535
536
537 void BufferView::theLockingInset(UpdatableInset * inset)
538 {
539         text->the_locking_inset = inset;
540 }
541
542
543 LyXText * BufferView::getLyXText() const
544 {
545         if (theLockingInset()) {
546                 LyXText * txt = theLockingInset()->getLyXText(this, true);
547                 if (txt)
548                         return txt;
549         }
550         return text;
551 }
552
553
554 Language const * BufferView::getParentLanguage(InsetOld * inset) const
555 {
556         Paragraph const & par = ownerPar(*buffer(), inset);
557         return par.getFontSettings(buffer()->params,
558                                    par.getPositionOfInset(inset)).language();
559 }
560
561
562 Encoding const * BufferView::getEncoding() const
563 {
564         LyXText * t = getLyXText();
565         if (!t)
566                 return 0;
567
568         LyXCursor const & c = t->cursor;
569         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
570                                               outerFont(c.par(), t->ownerParagraphs()));
571         return font.language()->encoding();
572 }
573
574
575 void BufferView::haveSelection(bool sel)
576 {
577         pimpl_->workarea().haveSelection(sel);
578 }
579
580
581 int BufferView::workHeight() const
582 {
583         return pimpl_->workarea().workHeight();
584 }