]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
more cursor dispatch
[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 "debug.h"
23 #include "funcrequest.h"
24 #include "gettext.h"
25 #include "iterators.h"
26 #include "language.h"
27 #include "lyxlayout.h"
28 #include "lyxtext.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "PosIterator.h"
32 #include "texrow.h"
33 #include "undo.h"
34 #include "WordLangTuple.h"
35
36 #include "frontends/Alert.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/screen.h"
40 #include "frontends/WorkArea.h"
41
42 #include "insets/insetcommand.h" // ChangeRefs
43 #include "insets/updatableinset.h"
44
45 #include "support/filetools.h"
46 #include "support/lyxalgo.h" // lyx_count
47
48 using lyx::support::bformat;
49 using lyx::support::MakeAbsPath;
50
51 using std::distance;
52 using std::find;
53 using std::string;
54 using std::swap;
55 using std::vector;
56
57
58 extern BufferList bufferlist;
59
60
61 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
62                        int width, int height)
63         : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
64 {}
65
66
67 BufferView::~BufferView()
68 {
69         delete pimpl_;
70 }
71
72
73 void BufferView::unsetXSel()
74 {
75         pimpl_->xsel_cache_.set = false;
76 }
77
78
79 Buffer * BufferView::buffer() const
80 {
81         return pimpl_->buffer_;
82 }
83
84
85 LyXScreen & BufferView::screen() const
86 {
87         return pimpl_->screen();
88 }
89
90
91 LyXView * BufferView::owner() const
92 {
93         return pimpl_->owner_;
94 }
95
96
97 Painter & BufferView::painter() const
98 {
99         return pimpl_->painter();
100 }
101
102
103 void BufferView::buffer(Buffer * b)
104 {
105         pimpl_->buffer(b);
106 }
107
108
109 bool BufferView::newFile(string const & fn, string const & tn, bool named)
110 {
111         return pimpl_->newFile(fn, tn, named);
112 }
113
114
115 bool BufferView::loadLyXFile(string const & fn, bool tl)
116 {
117         return pimpl_->loadLyXFile(fn, tl);
118 }
119
120
121 void BufferView::reload()
122 {
123         string const fn = buffer()->fileName();
124         if (bufferlist.close(buffer(), false))
125                 loadLyXFile(fn);
126 }
127
128
129 void BufferView::resize()
130 {
131         if (pimpl_->buffer_)
132                 pimpl_->resizeCurrentBuffer();
133 }
134
135
136 bool BufferView::fitCursor()
137 {
138         return pimpl_->fitCursor();
139 }
140
141
142 void BufferView::update()
143 {
144         pimpl_->update();
145 }
146
147
148 void BufferView::updateScrollbar()
149 {
150         pimpl_->updateScrollbar();
151 }
152
153
154 void BufferView::scrollDocView(int value)
155 {
156         pimpl_->scrollDocView(value);
157 }
158
159
160 void BufferView::redoCurrentBuffer()
161 {
162         pimpl_->redoCurrentBuffer();
163 }
164
165
166 bool BufferView::available() const
167 {
168         return pimpl_->available();
169 }
170
171
172 Change const BufferView::getCurrentChange()
173 {
174         return pimpl_->getCurrentChange();
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 int BufferView::workWidth() const
203 {
204         return pimpl_->workarea().workWidth();
205 }
206
207
208 void BufferView::center()
209 {
210         pimpl_->center();
211 }
212
213
214 int BufferView::top_y() const
215 {
216         return pimpl_->top_y();
217 }
218
219
220 void BufferView::top_y(int y)
221 {
222         pimpl_->top_y(y);
223 }
224
225
226 string const BufferView::getClipboard() const
227 {
228         return pimpl_->workarea().getClipboard();
229 }
230
231
232 void BufferView::stuffClipboard(string const & stuff) const
233 {
234         pimpl_->stuffClipboard(stuff);
235 }
236
237
238 bool BufferView::dispatch(FuncRequest const & ev)
239 {
240         return pimpl_->dispatch(ev);
241 }
242
243
244 void BufferView::scroll(int lines)
245 {
246         pimpl_->scroll(lines);
247 }
248
249
250 // Inserts a file into current document
251 bool BufferView::insertLyXFile(string const & filen)
252         //
253         // Copyright CHT Software Service GmbH
254         // Uwe C. Schroeder
255         //
256         // Insert a LyXformat - file into current buffer
257         //
258         // Moved from lyx_cb.C (Lgb)
259 {
260         BOOST_ASSERT(!filen.empty());
261
262         string const fname = MakeAbsPath(filen);
263
264         cursor().clearSelection();
265         text()->breakParagraph(buffer()->paragraphs());
266
267         bool res = buffer()->readFile(fname, text()->cursorPar());
268         resize();
269         return res;
270 }
271
272
273 void BufferView::showErrorList(string const & action) const
274 {
275         if (getErrorList().size()) {
276                 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
277                         action, buffer()->fileName());
278                 owner()->getDialogs().show("errorlist", title);
279                 pimpl_->errorlist_.clear();
280         }
281 }
282
283
284 ErrorList const & BufferView::getErrorList() const
285 {
286         return pimpl_->errorlist_;
287 }
288
289
290 void BufferView::setCursorFromRow(int row)
291 {
292         int tmpid = -1;
293         int tmppos = -1;
294
295         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
296
297         if (tmpid == -1)
298                 text()->setCursor(0, 0);
299         else
300                 text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
301 }
302
303
304 bool BufferView::insertInset(InsetBase * inset, string const & lout)
305 {
306         return pimpl_->insertInset(inset, lout);
307 }
308
309
310 void BufferView::gotoLabel(string const & label)
311 {
312         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
313              it != buffer()->inset_iterator_end(); ++it) {
314                 vector<string> labels;
315                 it->getLabelList(*buffer(), labels);
316                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
317                         cursor().clearSelection();
318                         text()->setCursor(
319                                 distance(text()->paragraphs().begin(), it.getPar()),
320                                 it.getPos());
321                         cursor().resetAnchor();
322                         update();
323                         return;
324                 }
325         }
326 }
327
328
329 void BufferView::undo()
330 {
331         if (!available())
332                 return;
333
334         owner()->message(_("Undo"));
335         cursor().clearSelection();
336         if (!textUndo(*this))
337                 owner()->message(_("No further undo information"));
338         update();
339         switchKeyMap();
340 }
341
342
343 void BufferView::redo()
344 {
345         if (!available())
346                 return;
347
348         owner()->message(_("Redo"));
349         cursor().clearSelection();
350         if (!textRedo(*this))
351                 owner()->message(_("No further redo information"));
352         update();
353         switchKeyMap();
354 }
355
356
357 void BufferView::replaceWord(string const & replacestring)
358 {
359         if (!available())
360                 return;
361
362         LyXText * t = getLyXText();
363
364         t->replaceSelectionWithString(replacestring);
365         t->setSelectionRange(replacestring.length());
366
367         // Go back so that replacement string is also spellchecked
368         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
369                 t->cursorLeft(this);
370
371         // FIXME: should be done through LFUN
372         buffer()->markDirty();
373         update();
374 }
375
376
377 void BufferView::hideCursor()
378 {
379         screen().hideCursor();
380 }
381
382
383 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
384 {
385         // Check if the label 'from' appears more than once
386         vector<string> labels;
387         buffer()->getLabelList(labels);
388
389         if (lyx::count(labels.begin(), labels.end(), from) > 1)
390                 return false;
391
392         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
393 }
394
395
396 LyXText * BufferView::getLyXText() const
397 {
398         return cursor().innerText();
399 }
400
401
402 Language const * BufferView::getParentLanguage(InsetOld * inset) const
403 {
404         Paragraph const & par = ownerPar(*buffer(), inset);
405         return par.getFontSettings(buffer()->params(),
406                                    par.getPositionOfInset(inset)).language();
407 }
408
409
410 Encoding const * BufferView::getEncoding() const
411 {
412         LyXText * t = getLyXText();
413         if (!t)
414                 return 0;
415         CursorSlice const & cur = cursor().innerTextSlice();
416         return t->getPar(cur.par())->getFont(
417                 buffer()->params(), cur.pos(),
418                 outerFont(t->getPar(cur.par()), t->paragraphs())
419         ).language()->encoding();
420 }
421
422
423 void BufferView::haveSelection(bool sel)
424 {
425         pimpl_->workarea().haveSelection(sel);
426 }
427
428
429 int BufferView::workHeight() const
430 {
431         return pimpl_->workarea().workHeight();
432 }
433
434
435 void BufferView::updateParagraphDialog()
436 {
437         pimpl_->updateParagraphDialog();
438 }
439
440
441 LyXText * BufferView::text() const
442 {
443         return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
444 }
445
446
447 void BufferView::setCursor(ParIterator const & par,
448                            lyx::pos_type pos)
449 {
450         LCursor & cur = cursor();
451         cur.reset();
452         ParIterator::PosHolder const & positions = par.positions();
453         int const last = par.size() - 1;
454         for (int i = 0; i < last; ++i)
455                 (*positions[i].it)->inset->edit(cur, true);
456         cur.resetAnchor();
457         LyXText * lt = par.text(*buffer());
458         lt->setCursor(par.pit(), pos);
459 }
460
461
462 /*
463 if the fitCursor call refers to some point in never-explored-land, then we
464 don't have y information in insets there, then we cannot even do an update
465 to get it (because we need the y infomation for setting top_y first). So
466 this is solved in putSelectionAt with:
467
468 - setting top_y to the y of the outerPar (that has good info)
469 - calling update
470 - calling cursor().updatePos()
471 - then call fitCursor()
472
473 Ab.
474 */
475
476 void BufferView::putSelectionAt(PosIterator const & cur,
477                       int length, bool backwards)
478 {
479         ParIterator par(cur);
480
481         cursor().clearSelection();
482
483         LyXText * text = par.text(*buffer());
484         setCursor(par, cur.pos());
485         
486         // hack for the chicken and egg problem
487         if (par.inset())
488                 top_y(par.outerPar()->y);
489         update();
490         text->setCursor(cur.pit(), cur.pos());
491         cursor().updatePos();
492
493         if (length) {
494                 text->setSelectionRange(length);
495                 cursor().setSelection();
496                 if (backwards)
497                         swap(cursor().cursor_, cursor().anchor_);
498         }
499
500         fitCursor();
501         update();
502 }
503
504
505 LCursor & BufferView::cursor()
506 {
507         return pimpl_->cursor_;
508 }
509
510
511 LCursor const & BufferView::cursor() const
512 {
513         return pimpl_->cursor_;
514 }