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