]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
small stuff (whitspace, spelling...)
[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 {
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->cursorPar());
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)"),
284                         action, buffer()->fileName());
285                 owner()->getDialogs().show("errorlist", title);
286                 pimpl_->errorlist_.clear();
287         }
288 }
289
290
291 ErrorList const & 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         if (tmpid == -1)
305                 text->setCursor(0, 0);
306         else
307                 text->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
308 }
309
310
311 bool BufferView::insertInset(InsetOld * inset, string const & lout)
312 {
313         return pimpl_->insertInset(inset, lout);
314 }
315
316
317 void BufferView::gotoLabel(string const & label)
318 {
319         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
320              it != buffer()->inset_iterator_end(); ++it) {
321                 vector<string> labels;
322                 it->getLabelList(*buffer(), labels);
323                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
324                         beforeChange(text);
325                         text->setCursor(
326                                 std::distance(text->ownerParagraphs().begin(), it.getPar()),
327                                 it.getPos());
328                         text->selection.cursor = text->cursor;
329                         update();
330                         return;
331                 }
332         }
333 }
334
335
336 void BufferView::undo()
337 {
338         if (!available())
339                 return;
340
341         owner()->message(_("Undo"));
342         beforeChange(text);
343         if (!textUndo(this))
344                 owner()->message(_("No further undo information"));
345         update();
346         switchKeyMap();
347 }
348
349
350 void BufferView::redo()
351 {
352         if (!available())
353                 return;
354
355         owner()->message(_("Redo"));
356         beforeChange(text);
357         if (!textRedo(this))
358                 owner()->message(_("No further redo information"));
359         update();
360         switchKeyMap();
361 }
362
363
364 // these functions are for the spellchecker
365 WordLangTuple const BufferView::nextWord(float & value)
366 {
367         if (!available()) {
368                 value = 1;
369                 return WordLangTuple();
370         }
371
372         return text->selectNextWordToSpellcheck(value);
373 }
374
375
376 void BufferView::selectLastWord()
377 {
378         if (!available())
379                 return;
380
381         LyXCursor cur = text->selection.cursor;
382         beforeChange(text);
383         text->selection.cursor = cur;
384         text->selectSelectedWord();
385         update();
386 }
387
388
389 void BufferView::endOfSpellCheck()
390 {
391         if (!available()) return;
392
393         beforeChange(text);
394         text->selectSelectedWord();
395         text->clearSelection();
396         update();
397 }
398
399
400 void BufferView::replaceWord(string const & replacestring)
401 {
402         if (!available())
403                 return;
404
405         LyXText * text = getLyXText();
406
407         text->replaceSelectionWithString(replacestring);
408         text->setSelectionRange(replacestring.length());
409
410         // Go back so that replacement string is also spellchecked
411         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
412                 text->cursorLeft(this);
413
414         // FIXME: should be done through LFUN
415         buffer()->markDirty();
416         update();
417 }
418
419
420 bool BufferView::lockInset(UpdatableInset * inset)
421 {
422         if (!inset)
423                 return false;
424
425         // don't relock if we're already locked
426         if (theLockingInset() == inset)
427                 return true;
428
429         if (theLockingInset())
430                 return theLockingInset()->lockInsetInInset(this, inset);
431
432         // first check if it's the inset under the cursor we want lock
433         // should be most of the time
434         if (text->cursor.pos() < text->cursorPar()->size()
435                         && text->cursorPar()->getChar(text->cursor.pos()) ==
436                         Paragraph::META_INSET) {
437                 if (inset == text->cursorPar()->getInset(text->cursor.pos())) {
438                         theLockingInset(inset);
439                         return true;
440                 }
441         }
442
443         // then do a deep look at the inset and lock the right one
444         ParagraphList::iterator pit = buffer()->paragraphs().begin();
445         ParagraphList::iterator pend = buffer()->paragraphs().end();
446         for (int par = 0; pit != pend; ++pit, ++par) {
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(par, it->pos);
452                                 theLockingInset(inset);
453                                 return true;
454                         }
455                 }
456         }
457         return false;
458 }
459
460
461 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
462 {
463         if (theLockingInset() && available()) {
464                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
465                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
466                         updateScrollbar();
467                         return true;
468                 }
469         }
470         return false;
471 }
472
473
474 void BufferView::hideCursor()
475 {
476         screen().hideCursor();
477 }
478
479
480 int BufferView::unlockInset(UpdatableInset * inset)
481 {
482         if (!inset)
483                 return 0;
484         if (inset && theLockingInset() == inset) {
485                 inset->insetUnlock(this);
486                 theLockingInset(0);
487                 // make sure we update the combo !
488                 owner()->setLayout(getLyXText()->cursorPar()->layout()->name());
489                 // Tell the paragraph dialog that we changed paragraph
490                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
491                 finishUndo();
492                 return 0;
493         }
494         if (inset && theLockingInset() &&
495                    theLockingInset()->unlockInsetInInset(this, inset)) {
496                 // Tell the paragraph dialog that we changed paragraph
497                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
498                 // owner inset has updated the layout combo
499                 finishUndo();
500                 return 0;
501         }
502         return 1;
503 }
504
505
506 void BufferView::updateInset(InsetOld const * inset)
507 {
508         pimpl_->updateInset(inset);
509 }
510
511
512 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
513 {
514         // Check if the label 'from' appears more than once
515         vector<string> labels;
516         buffer()->getLabelList(labels);
517
518         if (lyx::count(labels.begin(), labels.end(), from) > 1)
519                 return false;
520
521         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
522 }
523
524
525 UpdatableInset * BufferView::theLockingInset() const
526 {
527         // If NULL is not allowed we should put an Assert here. (Lgb)
528         if (text)
529                 return text->the_locking_inset;
530         return 0;
531 }
532
533
534 void BufferView::theLockingInset(UpdatableInset * inset)
535 {
536         text->the_locking_inset = inset;
537 }
538
539
540 LyXText * BufferView::getLyXText() const
541 {
542         if (theLockingInset()) {
543                 LyXText * txt = theLockingInset()->getLyXText(this, true);
544                 if (txt)
545                         return txt;
546         }
547         return text;
548 }
549
550
551 Language const * BufferView::getParentLanguage(InsetOld * inset) const
552 {
553         Paragraph const & par = ownerPar(*buffer(), inset);
554         return par.getFontSettings(buffer()->params(),
555                                    par.getPositionOfInset(inset)).language();
556 }
557
558
559 Encoding const * BufferView::getEncoding() const
560 {
561         LyXText * text = getLyXText();
562         if (!text)
563                 return 0;
564
565         return text->cursorPar()->getFont(
566                 buffer()->params(),
567                 text->cursor.pos(),
568                 outerFont(text->cursorPar(), text->ownerParagraphs())
569         ).language()->encoding();
570 }
571
572
573 void BufferView::haveSelection(bool sel)
574 {
575         pimpl_->workarea().haveSelection(sel);
576 }
577
578
579 int BufferView::workHeight() const
580 {
581         return pimpl_->workarea().workHeight();
582 }