]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
1cdbf0c75e9dd9a40e84ef28694497eac714ce27
[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 Braunstein
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.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 using std::string;
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           x_target_(0)
61 {
62         text = 0;
63 }
64
65
66 BufferView::~BufferView()
67 {
68         delete text;
69         delete pimpl_;
70 }
71
72
73 Buffer * BufferView::buffer() const
74 {
75         return pimpl_->buffer_;
76 }
77
78
79 LyXScreen & BufferView::screen() const
80 {
81         return pimpl_->screen();
82 }
83
84
85 LyXView * BufferView::owner() const
86 {
87         return pimpl_->owner_;
88 }
89
90
91 Painter & BufferView::painter() const
92 {
93         return pimpl_->painter();
94 }
95
96
97 void BufferView::buffer(Buffer * b)
98 {
99         pimpl_->buffer(b);
100 }
101
102
103 bool BufferView::newFile(string const & fn, string const & tn, bool named)
104 {
105         return pimpl_->newFile(fn, tn, named);
106 }
107
108
109 bool BufferView::loadLyXFile(string const & fn, bool tl)
110 {
111         return pimpl_->loadLyXFile(fn, tl);
112 }
113
114
115 void BufferView::reload()
116 {
117         string const fn = buffer()->fileName();
118         if (bufferlist.close(buffer(), false))
119                 loadLyXFile(fn);
120 }
121
122
123 void BufferView::resize()
124 {
125         if (pimpl_->buffer_)
126                 pimpl_->resizeCurrentBuffer();
127 }
128
129
130 bool BufferView::fitCursor()
131 {
132         return pimpl_->fitCursor();
133 }
134
135
136 void BufferView::update()
137 {
138         pimpl_->update();
139 }
140
141
142 void BufferView::updateScrollbar()
143 {
144         pimpl_->updateScrollbar();
145 }
146
147
148 void BufferView::scrollDocView(int value)
149 {
150         pimpl_->scrollDocView(value);
151 }
152
153
154 void BufferView::redoCurrentBuffer()
155 {
156         pimpl_->redoCurrentBuffer();
157 }
158
159
160 bool BufferView::available() const
161 {
162         return pimpl_->available();
163 }
164
165
166 Change const BufferView::getCurrentChange()
167 {
168         return pimpl_->getCurrentChange();
169 }
170
171
172 void BufferView::beforeChange(LyXText * text)
173 {
174         pimpl_->beforeChange(text);
175 }
176
177
178 void BufferView::savePosition(unsigned int i)
179 {
180         pimpl_->savePosition(i);
181 }
182
183
184 void BufferView::restorePosition(unsigned int i)
185 {
186         pimpl_->restorePosition(i);
187 }
188
189
190 bool BufferView::isSavedPosition(unsigned int i)
191 {
192         return pimpl_->isSavedPosition(i);
193 }
194
195
196 void BufferView::switchKeyMap()
197 {
198         pimpl_->switchKeyMap();
199 }
200
201
202 void BufferView::insetUnlock()
203 {
204         pimpl_->insetUnlock();
205 }
206
207
208 int BufferView::workWidth() const
209 {
210         return pimpl_->workarea().workWidth();
211 }
212
213
214 void BufferView::center()
215 {
216         pimpl_->center();
217 }
218
219
220 int BufferView::top_y() const
221 {
222         return pimpl_->top_y();
223 }
224
225
226 void BufferView::top_y(int y)
227 {
228         pimpl_->top_y(y);
229 }
230
231
232 string const BufferView::getClipboard() const
233 {
234         return pimpl_->workarea().getClipboard();
235 }
236
237
238 void BufferView::stuffClipboard(string const & stuff) const
239 {
240         pimpl_->stuffClipboard(stuff);
241 }
242
243
244 bool BufferView::dispatch(FuncRequest const & ev)
245 {
246         return pimpl_->dispatch(ev);
247 }
248
249
250 void BufferView::scroll(int lines)
251 {
252         pimpl_->scroll(lines);
253 }
254
255
256 // Inserts a file into current document
257 bool BufferView::insertLyXFile(string const & filen)
258         //
259         // Copyright CHT Software Service GmbH
260         // Uwe C. Schroeder
261         //
262         // Insert a LyXformat - file into current buffer
263         //
264         // Moved from lyx_cb.C (Lgb)
265 {
266         BOOST_ASSERT(!filen.empty());
267
268         string const fname = MakeAbsPath(filen);
269
270         beforeChange(text);
271
272         text->breakParagraph(buffer()->paragraphs());
273
274         bool res = buffer()->readFile(fname, text->cursorPar());
275
276         resize();
277         return res;
278 }
279
280
281 void BufferView::showErrorList(string const & action) const
282 {
283         if (getErrorList().size()) {
284                 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
285                         action, buffer()->fileName());
286                 owner()->getDialogs().show("errorlist", title);
287                 pimpl_->errorlist_.clear();
288         }
289 }
290
291
292 ErrorList const & BufferView::getErrorList() const
293 {
294         return pimpl_->errorlist_;
295 }
296
297
298 void BufferView::setCursorFromRow(int row)
299 {
300         int tmpid = -1;
301         int tmppos = -1;
302
303         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
304
305         if (tmpid == -1)
306                 text->setCursor(0, 0);
307         else
308                 text->setCursor(buffer()->getParFromID(tmpid).pit(), 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(*buffer(), labels);
324                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
325                         beforeChange(text);
326                         text->setCursor(
327                                 std::distance(text->ownerParagraphs().begin(), it.getPar()),
328                                 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 * text = getLyXText();
407
408         text->replaceSelectionWithString(replacestring);
409         text->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                 text->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
426         // don't relock if we're already locked
427         if (theLockingInset() == inset)
428                 return true;
429
430         if (theLockingInset())
431                 return theLockingInset()->lockInsetInInset(this, inset);
432
433         // first check if it's the inset under the cursor we want lock
434         // should be most of the time
435         if (text->cursor.pos() < text->cursorPar()->size()
436                         && text->cursorPar()->getChar(text->cursor.pos()) ==
437                         Paragraph::META_INSET) {
438                 if (inset == text->cursorPar()->getInset(text->cursor.pos())) {
439                         theLockingInset(inset);
440                         return true;
441                 }
442         }
443
444         // then do a deep look at the inset and lock the right one
445         ParagraphList::iterator pit = buffer()->paragraphs().begin();
446         ParagraphList::iterator pend = buffer()->paragraphs().end();
447         for (int par = 0; pit != pend; ++pit, ++par) {
448                 InsetList::iterator it = pit->insetlist.begin();
449                 InsetList::iterator end = pit->insetlist.end();
450                 for (; it != end; ++it) {
451                         if (it->inset == inset) {
452                                 text->setCursorIntern(par, it->pos);
453                                 theLockingInset(inset);
454                                 return true;
455                         }
456                 }
457         }
458         return false;
459 }
460
461
462 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
463 {
464         if (theLockingInset() && available()) {
465                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
466                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
467                         updateScrollbar();
468                         return true;
469                 }
470         }
471         return false;
472 }
473
474
475 void BufferView::hideCursor()
476 {
477         screen().hideCursor();
478 }
479
480
481 int BufferView::unlockInset(UpdatableInset * inset)
482 {
483         if (!inset)
484                 return 0;
485         if (inset && theLockingInset() == inset) {
486                 inset->insetUnlock(this);
487                 theLockingInset(0);
488                 // make sure we update the combo !
489                 owner()->setLayout(getLyXText()->cursorPar()->layout()->name());
490                 // Tell the paragraph dialog that we changed paragraph
491                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
492                 finishUndo();
493                 return 0;
494         }
495         if (inset && theLockingInset() &&
496                    theLockingInset()->unlockInsetInInset(this, inset)) {
497                 // Tell the paragraph dialog that we changed paragraph
498                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
499                 // owner inset has updated the layout combo
500                 finishUndo();
501                 return 0;
502         }
503         return 1;
504 }
505
506
507 void BufferView::updateInset(InsetOld const * inset)
508 {
509         pimpl_->updateInset(inset);
510 }
511
512
513 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
514 {
515         // Check if the label 'from' appears more than once
516         vector<string> labels;
517         buffer()->getLabelList(labels);
518
519         if (lyx::count(labels.begin(), labels.end(), from) > 1)
520                 return false;
521
522         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
523 }
524
525
526 UpdatableInset * BufferView::theLockingInset() const
527 {
528         // If NULL is not allowed we should put an Assert here. (Lgb)
529         if (text)
530                 return text->the_locking_inset;
531         return 0;
532 }
533
534
535 void BufferView::theLockingInset(UpdatableInset * inset)
536 {
537         text->the_locking_inset = inset;
538 }
539
540
541 LyXText * BufferView::getLyXText() const
542 {
543         if (theLockingInset()) {
544                 LyXText * txt = theLockingInset()->getLyXText(this, true);
545                 if (txt)
546                         return txt;
547         }
548         return text;
549 }
550
551
552 Language const * BufferView::getParentLanguage(InsetOld * inset) const
553 {
554         Paragraph const & par = ownerPar(*buffer(), inset);
555         return par.getFontSettings(buffer()->params(),
556                                    par.getPositionOfInset(inset)).language();
557 }
558
559
560 Encoding const * BufferView::getEncoding() const
561 {
562         LyXText * text = getLyXText();
563         if (!text)
564                 return 0;
565
566         return text->cursorPar()->getFont(
567                 buffer()->params(),
568                 text->cursor.pos(),
569                 outerFont(text->cursorPar(), text->ownerParagraphs())
570         ).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 }
584
585
586 void BufferView::x_target(int x)
587 {
588         x_target_ = x;
589 }
590
591
592 int BufferView::x_target() const
593 {
594         return x_target_;
595 }