]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.C
hopefully fix tex2lyx linking.
[lyx.git] / src / frontends / WorkArea.C
1 /**
2  * \file WorkArea.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Splash screen code added by Angus Leeming
12  */
13
14 #include <config.h>
15
16 #include "frontends/WorkArea.h"
17
18 #include "frontends/Application.h"
19 #include "frontends/FontMetrics.h"
20
21 #include "funcrequest.h"
22 #include "lyxfunc.h"
23 #include "Painter.h"
24
25 #include "BufferView.h"
26 #include "buffer.h"
27 #include "bufferparams.h"
28 #include "coordcache.h"
29 #include "cursor.h"
30 #include "debug.h"
31 #include "language.h"
32 #include "LColor.h"
33 #include "lyxfont.h"
34 #include "lyxrc.h"
35 #include "lyxrow.h"
36 #include "lyxtext.h"
37 #include "LyXView.h"
38 #include "metricsinfo.h"
39 #include "paragraph.h"
40 #include "rowpainter.h"
41
42 #include "gettext.h"
43 #include "support/filetools.h" // LibFileSearch
44 #include "support/forkedcontr.h"
45
46 #include <boost/utility.hpp>
47 #include <boost/bind.hpp>
48 #include <boost/current_function.hpp>
49
50 using lyx::support::libFileSearch;
51 using lyx::support::ForkedcallsController;
52
53 using std::endl;
54 using std::min;
55 using std::max;
56 using std::string;
57
58
59 namespace {
60
61 // All the below connection objects are needed because of a bug in some
62 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
63 // to these connections we avoid a segfault upon startup, and also at exit.
64 // (Lgb)
65
66 boost::signals::connection timecon;
67
68 } // anon namespace
69
70 namespace lyx {
71 namespace frontend {
72
73 WorkArea::WorkArea(int id, LyXView & lyx_view)
74         : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
75           id_(id), cursor_visible_(false), cursor_timeout_(400)
76 {
77         // Start loading the pixmap as soon as possible
78         //if (lyxrc.show_banner) {
79         //      showBanner();
80         //}
81
82         // Setup the signals
83         timecon = cursor_timeout_.timeout
84                 .connect(boost::bind(&WorkArea::toggleCursor, this));
85
86         cursor_timeout_.start();
87 }
88
89
90 void WorkArea::setBufferView(BufferView * buffer_view)
91 {
92         if (buffer_view_) {
93                 message_connection_.disconnect();
94                 lyx_view_.disconnectBufferView();
95         }
96
97         hideCursor();
98         buffer_view_ = buffer_view;
99         toggleCursor();
100
101         message_connection_ = buffer_view_->message.connect(
102                         boost::bind(&WorkArea::displayMessage, this, _1));
103
104         lyx_view_.connectBufferView(*buffer_view);
105 }
106
107
108 BufferView & WorkArea::bufferView()
109 {
110         return *buffer_view_;
111 }
112
113
114 BufferView const & WorkArea::bufferView() const
115 {
116         return *buffer_view_;
117 }
118
119
120 void WorkArea::stopBlinkingCursor()
121 {
122         cursor_timeout_.stop();
123         hideCursor();
124 }
125
126
127 void WorkArea::startBlinkingCursor()
128 {
129         showCursor();
130         cursor_timeout_.restart();
131 }
132
133
134 void WorkArea::redraw(bool singlePar)
135 {
136         if (!buffer_view_ || !buffer_view_->buffer()) {
137                 greyed_out_ = true;
138                 // The argument here are meaningless.
139                 expose(1,1,1,1);
140                 return;
141         }
142
143         buffer_view_->updateMetrics(singlePar && hasFocus());
144
145         updateScrollbar();
146
147         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
148         greyed_out_ = false;
149
150         if (lyxerr.debugging(Debug::WORKAREA)) {
151                 lyxerr[Debug::WORKAREA] << "WorkArea::redraw screen" << endl;
152         }
153         int const ymin = std::max(vi.y1, 0);
154         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
155
156         expose(0, ymin, width(), ymax - ymin);
157
158         //lyxerr[Debug::WORKAREA]
159         //<< "  ymin = " << ymin << "  width() = " << width()
160 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
161
162         if (lyxerr.debugging(Debug::WORKAREA))
163                 buffer_view_->coordCache().dump();
164 }
165
166
167 void WorkArea::processKeySym(LyXKeySymPtr key,
168                                                          key_modifier::state state)
169 {
170         // In order to avoid bad surprise in the middle of an operation, we better stop
171         // the blinking cursor.
172         stopBlinkingCursor();
173
174         theLyXFunc().setLyXView(&lyx_view_);
175         theLyXFunc().processKeySym(key, state);
176
177         /* When we move around, or type, it's nice to be able to see
178          * the cursor immediately after the keypress.
179          */
180         startBlinkingCursor();
181 }
182
183
184 void WorkArea::dispatch(FuncRequest const & cmd0)
185 {
186         // Handle drag&drop
187         if (cmd0.action == LFUN_FILE_OPEN) {
188                 lyx_view_.dispatch(cmd0);
189                 return;
190         }
191
192         theLyXFunc().setLyXView(&lyx_view_);
193
194         std::pair<bool, bool> needRedraw = buffer_view_->workAreaDispatch(cmd0);
195
196         // Skip these when selecting
197         if (cmd0.action != LFUN_MOUSE_MOTION) {
198                 lyx_view_.updateLayoutChoice();
199                 lyx_view_.updateMenubar();
200                 lyx_view_.updateToolbars();
201         }
202
203         // Slight hack: this is only called currently when we
204         // clicked somewhere, so we force through the display
205         // of the new status here.
206         lyx_view_.clearMessage();
207
208         // Show the cursor immediately after any operation.
209         hideCursor();
210         toggleCursor();
211
212         if (needRedraw.first)
213                 redraw(needRedraw.second);
214 }
215
216
217 void WorkArea::resizeBufferView()
218 {
219         lyx_view_.busy(true);
220         lyx_view_.message(_("Formatting document..."));
221         buffer_view_->workAreaResize(width(), height());
222         lyx_view_.updateLayoutChoice();
223         redraw();
224         lyx_view_.busy(false);
225         lyx_view_.clearMessage();
226 }
227
228
229 void WorkArea::updateScrollbar()
230 {
231         buffer_view_->updateScrollbar(); 
232         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
233         setScrollbarParams(scroll_.height, scroll_.position,
234                 scroll_.lineScrollHeight);
235 }
236
237
238 void WorkArea::scrollBufferView(int position)
239 {
240         buffer_view_->scrollDocView(position);
241         redraw();
242         hideCursor();
243         if (lyxrc.cursor_follows_scrollbar) {
244                 buffer_view_->setCursorFromScrollbar();
245                 lyx_view_.updateLayoutChoice();
246         }
247         toggleCursor();
248 }
249
250
251 void WorkArea::showCursor()
252 {
253         if (cursor_visible_)
254                 return;
255
256         if (!buffer_view_->buffer())
257                 return;
258
259         CursorShape shape = BAR_SHAPE;
260
261         LyXText const & text = *buffer_view_->getLyXText();
262         LyXFont const & realfont = text.real_current_font;
263         BufferParams const & bp = buffer_view_->buffer()->params();
264         bool const samelang = realfont.language() == bp.language;
265         bool const isrtl = realfont.isVisibleRightToLeft();
266
267         if (!samelang || isrtl != bp.language->rightToLeft()) {
268                 shape = L_SHAPE;
269                 if (isrtl)
270                         shape = REVERSED_L_SHAPE;
271         }
272
273         // The ERT language hack needs fixing up
274         if (realfont.language() == latex_language)
275                 shape = BAR_SHAPE;
276
277         LyXFont const font = buffer_view_->cursor().getFont();
278         FontMetrics const & fm = theFontMetrics(font);
279         int const asc = fm.maxAscent();
280         int const des = fm.maxDescent();
281         int h = asc + des;
282         int x = 0;
283         int y = 0;
284         buffer_view_->cursor().getPos(x, y);
285         y -= asc;
286
287         // if it doesn't touch the screen, don't try to show it
288         if (y + h < 0 || y >= height())
289                 return;
290
291         cursor_visible_ = true;
292         showCursor(x, y, h, shape);
293 }
294
295
296 void WorkArea::hideCursor()
297 {
298         if (!cursor_visible_)
299                 return;
300
301         cursor_visible_ = false;
302         removeCursor();
303 }
304
305
306 void WorkArea::toggleCursor()
307 {
308         if (buffer_view_->buffer()) {
309
310                 if (cursor_visible_)
311                         hideCursor();
312                 else
313                         showCursor();
314
315                 // Use this opportunity to deal with any child processes that
316                 // have finished but are waiting to communicate this fact
317                 // to the rest of LyX.
318                 ForkedcallsController & fcc = ForkedcallsController::get();
319                 fcc.handleCompletedProcesses();
320         }
321
322         cursor_timeout_.restart();
323 }
324
325
326 void WorkArea::displayMessage(lyx::docstring const & message)
327 {
328         lyx_view_.message(message);
329 }
330
331 } // namespace frontend
332 } // namespace lyx