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