]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
setFont rework + some code simplification
[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 "texrow.h"
32 #include "undo.h"
33 #include "WordLangTuple.h"
34
35 #include "frontends/Alert.h"
36 #include "frontends/Dialogs.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/screen.h"
39 #include "frontends/WorkArea.h"
40
41 #include "insets/insetcommand.h" // ChangeRefs
42 #include "insets/updatableinset.h"
43
44 #include "support/filetools.h"
45 #include "support/lyxalgo.h" // lyx_count
46
47 using lyx::support::bformat;
48 using lyx::support::MakeAbsPath;
49
50 using std::find;
51 using std::string;
52 using std::vector;
53
54
55 extern BufferList bufferlist;
56
57
58 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
59                        int width, int height)
60         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height)),
61           x_target_(0)
62 {
63         text = 0;
64 }
65
66
67 BufferView::~BufferView()
68 {
69         delete text;
70         delete pimpl_;
71 }
72
73
74 Buffer * BufferView::buffer() const
75 {
76         return pimpl_->buffer_;
77 }
78
79
80 LyXScreen & BufferView::screen() const
81 {
82         return pimpl_->screen();
83 }
84
85
86 LyXView * BufferView::owner() const
87 {
88         return pimpl_->owner_;
89 }
90
91
92 Painter & BufferView::painter() const
93 {
94         return pimpl_->painter();
95 }
96
97
98 void BufferView::buffer(Buffer * b)
99 {
100         pimpl_->buffer(b);
101 }
102
103
104 bool BufferView::newFile(string const & fn, string const & tn, bool named)
105 {
106         return pimpl_->newFile(fn, tn, named);
107 }
108
109
110 bool BufferView::loadLyXFile(string const & fn, bool tl)
111 {
112         return pimpl_->loadLyXFile(fn, tl);
113 }
114
115
116 void BufferView::reload()
117 {
118         string const fn = buffer()->fileName();
119         if (bufferlist.close(buffer(), false))
120                 loadLyXFile(fn);
121 }
122
123
124 void BufferView::resize()
125 {
126         if (pimpl_->buffer_)
127                 pimpl_->resizeCurrentBuffer();
128 }
129
130
131 bool BufferView::fitCursor()
132 {
133         return pimpl_->fitCursor();
134 }
135
136
137 void BufferView::update()
138 {
139         pimpl_->update();
140 }
141
142
143 void BufferView::updateScrollbar()
144 {
145         pimpl_->updateScrollbar();
146 }
147
148
149 void BufferView::scrollDocView(int value)
150 {
151         pimpl_->scrollDocView(value);
152 }
153
154
155 void BufferView::redoCurrentBuffer()
156 {
157         pimpl_->redoCurrentBuffer();
158 }
159
160
161 bool BufferView::available() const
162 {
163         return pimpl_->available();
164 }
165
166
167 Change const BufferView::getCurrentChange()
168 {
169         return pimpl_->getCurrentChange();
170 }
171
172
173 void BufferView::savePosition(unsigned int i)
174 {
175         pimpl_->savePosition(i);
176 }
177
178
179 void BufferView::restorePosition(unsigned int i)
180 {
181         pimpl_->restorePosition(i);
182 }
183
184
185 bool BufferView::isSavedPosition(unsigned int i)
186 {
187         return pimpl_->isSavedPosition(i);
188 }
189
190
191 void BufferView::switchKeyMap()
192 {
193         pimpl_->switchKeyMap();
194 }
195
196
197 int BufferView::workWidth() const
198 {
199         return pimpl_->workarea().workWidth();
200 }
201
202
203 void BufferView::center()
204 {
205         pimpl_->center();
206 }
207
208
209 int BufferView::top_y() const
210 {
211         return pimpl_->top_y();
212 }
213
214
215 void BufferView::top_y(int y)
216 {
217         pimpl_->top_y(y);
218 }
219
220
221 string const BufferView::getClipboard() const
222 {
223         return pimpl_->workarea().getClipboard();
224 }
225
226
227 void BufferView::stuffClipboard(string const & stuff) const
228 {
229         pimpl_->stuffClipboard(stuff);
230 }
231
232
233 bool BufferView::dispatch(FuncRequest const & ev)
234 {
235         return pimpl_->dispatch(ev);
236 }
237
238
239 void BufferView::scroll(int lines)
240 {
241         pimpl_->scroll(lines);
242 }
243
244
245 // Inserts a file into current document
246 bool BufferView::insertLyXFile(string const & filen)
247         //
248         // Copyright CHT Software Service GmbH
249         // Uwe C. Schroeder
250         //
251         // Insert a LyXformat - file into current buffer
252         //
253         // Moved from lyx_cb.C (Lgb)
254 {
255         BOOST_ASSERT(!filen.empty());
256
257         string const fname = MakeAbsPath(filen);
258
259         text->clearSelection();
260         text->breakParagraph(buffer()->paragraphs());
261
262         bool res = buffer()->readFile(fname, text->cursorPar());
263         resize();
264         return res;
265 }
266
267
268 void BufferView::showErrorList(string const & action) const
269 {
270         if (getErrorList().size()) {
271                 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
272                         action, buffer()->fileName());
273                 owner()->getDialogs().show("errorlist", title);
274                 pimpl_->errorlist_.clear();
275         }
276 }
277
278
279 ErrorList const & BufferView::getErrorList() const
280 {
281         return pimpl_->errorlist_;
282 }
283
284
285 void BufferView::setCursorFromRow(int row)
286 {
287         int tmpid = -1;
288         int tmppos = -1;
289
290         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
291
292         if (tmpid == -1)
293                 text->setCursor(0, 0);
294         else
295                 text->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
296 }
297
298
299 bool BufferView::insertInset(InsetOld * inset, string const & lout)
300 {
301         return pimpl_->insertInset(inset, lout);
302 }
303
304
305 void BufferView::gotoLabel(string const & label)
306 {
307         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
308              it != buffer()->inset_iterator_end(); ++it) {
309                 vector<string> labels;
310                 it->getLabelList(*buffer(), labels);
311                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
312                         text->clearSelection();
313                         text->setCursor(
314                                 std::distance(text->ownerParagraphs().begin(), it.getPar()),
315                                 it.getPos());
316                         text->selection.cursor = text->cursor;
317                         update();
318                         return;
319                 }
320         }
321 }
322
323
324 void BufferView::undo()
325 {
326         if (!available())
327                 return;
328
329         owner()->message(_("Undo"));
330         text->clearSelection();
331         if (!textUndo(this))
332                 owner()->message(_("No further undo information"));
333         update();
334         switchKeyMap();
335 }
336
337
338 void BufferView::redo()
339 {
340         if (!available())
341                 return;
342
343         owner()->message(_("Redo"));
344         text->clearSelection();
345         if (!textRedo(this))
346                 owner()->message(_("No further redo information"));
347         update();
348         switchKeyMap();
349 }
350
351
352 void BufferView::replaceWord(string const & replacestring)
353 {
354         if (!available())
355                 return;
356
357         LyXText * text = getLyXText();
358
359         text->replaceSelectionWithString(replacestring);
360         text->setSelectionRange(replacestring.length());
361
362         // Go back so that replacement string is also spellchecked
363         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
364                 text->cursorLeft(this);
365
366         // FIXME: should be done through LFUN
367         buffer()->markDirty();
368         update();
369 }
370
371
372 void BufferView::hideCursor()
373 {
374         screen().hideCursor();
375 }
376
377
378 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
379 {
380         // Check if the label 'from' appears more than once
381         vector<string> labels;
382         buffer()->getLabelList(labels);
383
384         if (lyx::count(labels.begin(), labels.end(), from) > 1)
385                 return false;
386
387         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
388 }
389
390
391 UpdatableInset * BufferView::innerInset() const
392 {
393         return static_cast<UpdatableInset*>(cursor().innerInset());
394 }
395
396
397 LyXText * BufferView::getLyXText() const
398 {
399         return cursor().innerText();
400 }
401
402
403 Language const * BufferView::getParentLanguage(InsetOld * inset) const
404 {
405         Paragraph const & par = ownerPar(*buffer(), inset);
406         return par.getFontSettings(buffer()->params(),
407                                    par.getPositionOfInset(inset)).language();
408 }
409
410
411 Encoding const * BufferView::getEncoding() const
412 {
413         LyXText * text = getLyXText();
414         if (!text)
415                 return 0;
416         return text->cursorPar()->getFont(
417                 buffer()->params(),
418                 text->cursor.pos(),
419                 outerFont(text->cursorPar(), text->ownerParagraphs())
420         ).language()->encoding();
421 }
422
423
424 void BufferView::haveSelection(bool sel)
425 {
426         pimpl_->workarea().haveSelection(sel);
427 }
428
429
430 int BufferView::workHeight() const
431 {
432         return pimpl_->workarea().workHeight();
433 }
434
435
436 LCursor & BufferView::cursor()
437 {
438         return pimpl_->cursor_;
439 }
440
441
442 LCursor const & BufferView::cursor() const
443 {
444         return pimpl_->cursor_;
445 }
446
447
448 void BufferView::x_target(int x)
449 {
450         x_target_ = x;
451 }
452
453
454 int BufferView::x_target() const
455 {
456         return x_target_;
457 }
458
459
460 void BufferView::updateParagraphDialog()
461 {
462         pimpl_->updateParagraphDialog();
463 }