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