]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
remove dead spellcheck code
[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 void BufferView::replaceWord(string const & replacestring)
366 {
367         if (!available())
368                 return;
369
370         LyXText * text = getLyXText();
371
372         text->replaceSelectionWithString(replacestring);
373         text->setSelectionRange(replacestring.length());
374
375         // Go back so that replacement string is also spellchecked
376         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
377                 text->cursorLeft(this);
378
379         // FIXME: should be done through LFUN
380         buffer()->markDirty();
381         update();
382 }
383
384
385 bool BufferView::lockInset(UpdatableInset * inset)
386 {
387         if (!inset)
388                 return false;
389
390         // don't relock if we're already locked
391         if (theLockingInset() == inset)
392                 return true;
393
394         if (theLockingInset())
395                 return theLockingInset()->lockInsetInInset(this, inset);
396
397         // first check if it's the inset under the cursor we want lock
398         // should be most of the time
399         if (text->cursor.pos() < text->cursorPar()->size()
400                         && text->cursorPar()->getChar(text->cursor.pos()) ==
401                         Paragraph::META_INSET) {
402                 if (inset == text->cursorPar()->getInset(text->cursor.pos())) {
403                         theLockingInset(inset);
404                         return true;
405                 }
406         }
407
408         // then do a deep look at the inset and lock the right one
409         ParagraphList::iterator pit = buffer()->paragraphs().begin();
410         ParagraphList::iterator pend = buffer()->paragraphs().end();
411         for (int par = 0; pit != pend; ++pit, ++par) {
412                 InsetList::iterator it = pit->insetlist.begin();
413                 InsetList::iterator end = pit->insetlist.end();
414                 for (; it != end; ++it) {
415                         if (it->inset == inset) {
416                                 text->setCursorIntern(par, it->pos);
417                                 theLockingInset(inset);
418                                 return true;
419                         }
420                 }
421         }
422         return false;
423 }
424
425
426 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
427 {
428         if (theLockingInset() && available()) {
429                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
430                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
431                         updateScrollbar();
432                         return true;
433                 }
434         }
435         return false;
436 }
437
438
439 void BufferView::hideCursor()
440 {
441         screen().hideCursor();
442 }
443
444
445 int BufferView::unlockInset(UpdatableInset * inset)
446 {
447         if (!inset)
448                 return 0;
449         if (inset && theLockingInset() == inset) {
450                 inset->insetUnlock(this);
451                 theLockingInset(0);
452                 // make sure we update the combo !
453                 owner()->setLayout(getLyXText()->cursorPar()->layout()->name());
454                 // Tell the paragraph dialog that we changed paragraph
455                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
456                 finishUndo();
457                 return 0;
458         }
459         if (inset && theLockingInset() &&
460                    theLockingInset()->unlockInsetInInset(this, inset)) {
461                 // Tell the paragraph dialog that we changed paragraph
462                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
463                 // owner inset has updated the layout combo
464                 finishUndo();
465                 return 0;
466         }
467         return 1;
468 }
469
470
471 void BufferView::updateInset(InsetOld const * inset)
472 {
473         pimpl_->updateInset(inset);
474 }
475
476
477 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
478 {
479         // Check if the label 'from' appears more than once
480         vector<string> labels;
481         buffer()->getLabelList(labels);
482
483         if (lyx::count(labels.begin(), labels.end(), from) > 1)
484                 return false;
485
486         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
487 }
488
489
490 UpdatableInset * BufferView::theLockingInset() const
491 {
492         // If NULL is not allowed we should put an Assert here. (Lgb)
493         if (text)
494                 return text->the_locking_inset;
495         return 0;
496 }
497
498
499 void BufferView::theLockingInset(UpdatableInset * inset)
500 {
501         text->the_locking_inset = inset;
502 }
503
504
505 LyXText * BufferView::getLyXText() const
506 {
507         if (theLockingInset()) {
508                 LyXText * txt = theLockingInset()->getLyXText(this, true);
509                 if (txt)
510                         return txt;
511         }
512         return text;
513 }
514
515
516 Language const * BufferView::getParentLanguage(InsetOld * inset) const
517 {
518         Paragraph const & par = ownerPar(*buffer(), inset);
519         return par.getFontSettings(buffer()->params(),
520                                    par.getPositionOfInset(inset)).language();
521 }
522
523
524 Encoding const * BufferView::getEncoding() const
525 {
526         LyXText * text = getLyXText();
527         if (!text)
528                 return 0;
529
530         return text->cursorPar()->getFont(
531                 buffer()->params(),
532                 text->cursor.pos(),
533                 outerFont(text->cursorPar(), text->ownerParagraphs())
534         ).language()->encoding();
535 }
536
537
538 void BufferView::haveSelection(bool sel)
539 {
540         pimpl_->workarea().haveSelection(sel);
541 }
542
543
544 int BufferView::workHeight() const
545 {
546         return pimpl_->workarea().workHeight();
547 }
548
549
550 void BufferView::x_target(int x)
551 {
552         x_target_ = x;
553 }
554
555
556 int BufferView::x_target() const
557 {
558         return x_target_;
559 }