]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.C
* Status.15x: updates.
[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()
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         // No need to do anything if this is the current view. The BufferView 
144         // metrics are already up to date.
145         if (&lyx_view_ != &theApp->currentView())
146                 // FIXME: it would be nice to optimize for the off-screen case.
147                 buffer_view_->updateMetrics(false);
148
149         updateScrollbar();
150
151         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
152         greyed_out_ = false;
153
154         if (lyxerr.debugging(Debug::WORKAREA)) {
155                 lyxerr[Debug::WORKAREA] << "WorkArea::redraw screen" << endl;
156         }
157         int const ymin = std::max(vi.y1, 0);
158         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
159
160         expose(0, ymin, width(), ymax - ymin);
161
162         //lyxerr[Debug::WORKAREA]
163         //<< "  ymin = " << ymin << "  width() = " << width()
164 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
165
166         if (lyxerr.debugging(Debug::WORKAREA))
167                 buffer_view_->coordCache().dump();
168 }
169
170
171 void WorkArea::processKeySym(LyXKeySymPtr key,
172                                                          key_modifier::state state)
173 {
174         // In order to avoid bad surprise in the middle of an operation, we better stop
175         // the blinking cursor.
176         stopBlinkingCursor();
177
178         theLyXFunc().setLyXView(&lyx_view_);
179         theLyXFunc().processKeySym(key, state);
180
181         /* When we move around, or type, it's nice to be able to see
182          * the cursor immediately after the keypress.
183          */
184         startBlinkingCursor();
185 }
186
187
188 void WorkArea::dispatch(FuncRequest const & cmd0)
189 {
190         // Handle drag&drop
191         if (cmd0.action == LFUN_FILE_OPEN) {
192                 lyx_view_.dispatch(cmd0);
193                 return;
194         }
195
196         theLyXFunc().setLyXView(&lyx_view_);
197
198         std::pair<bool, bool> needRedraw = buffer_view_->workAreaDispatch(cmd0);
199
200         // Skip these when selecting
201         if (cmd0.action != LFUN_MOUSE_MOTION) {
202                 lyx_view_.updateLayoutChoice();
203                 lyx_view_.updateMenubar();
204                 lyx_view_.updateToolbars();
205         }
206
207         // Slight hack: this is only called currently when we
208         // clicked somewhere, so we force through the display
209         // of the new status here.
210         lyx_view_.clearMessage();
211
212         // Show the cursor immediately after any operation.
213         hideCursor();
214         toggleCursor();
215
216         if (needRedraw.first)
217                 redraw();
218 }
219
220
221 void WorkArea::resizeBufferView()
222 {
223         lyx_view_.busy(true);
224         lyx_view_.message(_("Formatting document..."));
225         buffer_view_->workAreaResize(width(), height());
226         lyx_view_.updateLayoutChoice();
227         redraw();
228         lyx_view_.busy(false);
229         lyx_view_.clearMessage();
230 }
231
232
233 void WorkArea::updateScrollbar()
234 {
235         buffer_view_->updateScrollbar(); 
236         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
237         setScrollbarParams(scroll_.height, scroll_.position,
238                 scroll_.lineScrollHeight);
239 }
240
241
242 void WorkArea::scrollBufferView(int position)
243 {
244         buffer_view_->scrollDocView(position);
245         redraw();
246         hideCursor();
247         if (lyxrc.cursor_follows_scrollbar) {
248                 buffer_view_->setCursorFromScrollbar();
249                 lyx_view_.updateLayoutChoice();
250         }
251         toggleCursor();
252 }
253
254
255 void WorkArea::showCursor()
256 {
257         if (cursor_visible_)
258                 return;
259
260         if (!buffer_view_->buffer())
261                 return;
262
263         CursorShape shape = BAR_SHAPE;
264
265         LyXText const & text = *buffer_view_->getLyXText();
266         LyXFont const & realfont = text.real_current_font;
267         BufferParams const & bp = buffer_view_->buffer()->params();
268         bool const samelang = realfont.language() == bp.language;
269         bool const isrtl = realfont.isVisibleRightToLeft();
270
271         if (!samelang || isrtl != bp.language->rightToLeft()) {
272                 shape = L_SHAPE;
273                 if (isrtl)
274                         shape = REVERSED_L_SHAPE;
275         }
276
277         // The ERT language hack needs fixing up
278         if (realfont.language() == latex_language)
279                 shape = BAR_SHAPE;
280
281         LyXFont const font = buffer_view_->cursor().getFont();
282         FontMetrics const & fm = theFontMetrics(font);
283         int const asc = fm.maxAscent();
284         int const des = fm.maxDescent();
285         int h = asc + des;
286         int x = 0;
287         int y = 0;
288         buffer_view_->cursor().getPos(x, y);
289         y -= asc;
290
291         // if it doesn't touch the screen, don't try to show it
292         if (y + h < 0 || y >= height())
293                 return;
294
295         cursor_visible_ = true;
296         showCursor(x, y, h, shape);
297 }
298
299
300 void WorkArea::hideCursor()
301 {
302         if (!cursor_visible_)
303                 return;
304
305         cursor_visible_ = false;
306         removeCursor();
307 }
308
309
310 void WorkArea::toggleCursor()
311 {
312         if (buffer_view_->buffer()) {
313
314                 if (cursor_visible_)
315                         hideCursor();
316                 else
317                         showCursor();
318
319                 // Use this opportunity to deal with any child processes that
320                 // have finished but are waiting to communicate this fact
321                 // to the rest of LyX.
322                 ForkedcallsController & fcc = ForkedcallsController::get();
323                 fcc.handleCompletedProcesses();
324         }
325
326         cursor_timeout_.restart();
327 }
328
329
330 void WorkArea::displayMessage(lyx::docstring const & message)
331 {
332         lyx_view_.message(message);
333 }
334
335 } // namespace frontend
336 } // namespace lyx