]> git.lyx.org Git - features.git/blob - src/frontends/WorkArea.cpp
rename LColor into Color
[features.git] / src / frontends / WorkArea.cpp
1 /**
2  * \file WorkArea.cpp
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 "Color.h"
33 #include "LyXFont.h"
34 #include "LyXRC.h"
35 #include "Row.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/ForkedcallsController.h"
44
45 #include <boost/utility.hpp>
46 #include <boost/bind.hpp>
47 #include <boost/current_function.hpp>
48
49 using lyx::support::ForkedcallsController;
50
51 using std::endl;
52 using std::min;
53 using std::max;
54 using std::string;
55
56
57 namespace {
58
59 // All the below connection objects are needed because of a bug in some
60 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
61 // to these connections we avoid a segfault upon startup, and also at exit.
62 // (Lgb)
63
64 boost::signals::connection timecon;
65
66 } // anon namespace
67
68 namespace lyx {
69 namespace frontend {
70
71 WorkArea::WorkArea(int id, LyXView & lyx_view)
72         : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
73           id_(id), cursor_visible_(false), cursor_timeout_(400)
74 {
75         // Start loading the pixmap as soon as possible
76         //if (lyxrc.show_banner) {
77         //      showBanner();
78         //}
79
80         // Setup the signals
81         timecon = cursor_timeout_.timeout
82                 .connect(boost::bind(&WorkArea::toggleCursor, this));
83
84         cursor_timeout_.start();
85 }
86
87
88 void WorkArea::setBufferView(BufferView * buffer_view)
89 {
90         if (buffer_view_) {
91                 message_connection_.disconnect();
92                 lyx_view_.disconnectBufferView();
93         }
94
95         hideCursor();
96         buffer_view_ = buffer_view;
97         toggleCursor();
98
99         message_connection_ = buffer_view_->message.connect(
100                         boost::bind(&WorkArea::displayMessage, this, _1));
101
102         lyx_view_.connectBufferView(*buffer_view);
103 }
104
105
106 BufferView & WorkArea::bufferView()
107 {
108         return *buffer_view_;
109 }
110
111
112 BufferView const & WorkArea::bufferView() const
113 {
114         return *buffer_view_;
115 }
116
117
118 void WorkArea::stopBlinkingCursor()
119 {
120         cursor_timeout_.stop();
121         hideCursor();
122 }
123
124
125 void WorkArea::startBlinkingCursor()
126 {
127         showCursor();
128         cursor_timeout_.restart();
129 }
130
131
132 void WorkArea::redraw()
133 {
134         if (!buffer_view_ || !buffer_view_->buffer()) {
135                 greyed_out_ = true;
136                 // The argument here are meaningless.
137                 expose(1,1,1,1);
138                 return;
139         }
140
141         // No need to do anything if this is the current view. The BufferView 
142         // metrics are already up to date.
143         if (&lyx_view_ != theApp()->currentView())
144                 // FIXME: it would be nice to optimize for the off-screen case.
145                 buffer_view_->updateMetrics(false);
146
147         updateScrollbar();
148
149         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
150         greyed_out_ = false;
151
152         LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
153
154         int const ymin = std::max(vi.y1, 0);
155         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
156
157         expose(0, ymin, width(), ymax - ymin);
158
159         //LYXERR(Debug::WORKAREA)
160         //<< "  ymin = " << ymin << "  width() = " << width()
161 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
162
163         if (lyxerr.debugging(Debug::WORKAREA))
164                 buffer_view_->coordCache().dump();
165 }
166
167
168 void WorkArea::processKeySym(LyXKeySymPtr key, 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, key_modifier::state k)
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         FuncRequest cmd;
195
196         if (cmd0.action == LFUN_MOUSE_PRESS) {
197                 if (k == key_modifier::shift)
198                         cmd = FuncRequest(cmd0, "region-select");
199                 else if (k == key_modifier::ctrl)
200                         cmd = FuncRequest(cmd0, "paragraph-select");
201                 else
202                         cmd = cmd0;
203         }
204         else
205                 cmd = cmd0;
206
207         // In order to avoid bad surprise in the middle of an operation, we better stop
208         // the blinking cursor.
209         if (!(cmd.action == LFUN_MOUSE_MOTION 
210                 && cmd.button() == mouse_button::none))
211                 stopBlinkingCursor();
212
213         bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
214
215         if (needRedraw)
216                 redraw();
217         
218         // Skip these when selecting
219         if (cmd.action != LFUN_MOUSE_MOTION) {
220                 lyx_view_.updateLayoutChoice();
221                 lyx_view_.updateMenubar();
222                 lyx_view_.updateToolbars();
223         }
224
225         // GUI tweaks except with mouse motion with no button pressed.
226         if (!(cmd.action == LFUN_MOUSE_MOTION 
227                 && cmd.button() == mouse_button::none)) {
228                 // Slight hack: this is only called currently when we
229                 // clicked somewhere, so we force through the display
230                 // of the new status here.
231                 lyx_view_.clearMessage();
232
233                 // Show the cursor immediately after any operation.
234                 startBlinkingCursor();
235         }
236 }
237
238
239 void WorkArea::resizeBufferView()
240 {
241         lyx_view_.busy(true);
242         lyx_view_.message(_("Formatting document..."));
243         buffer_view_->workAreaResize(width(), height());
244         lyx_view_.updateLayoutChoice();
245         lyx_view_.clearMessage();
246         lyx_view_.busy(false);
247 }
248
249
250 void WorkArea::updateScrollbar()
251 {
252         buffer_view_->updateScrollbar(); 
253         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
254         setScrollbarParams(scroll_.height, scroll_.position,
255                 scroll_.lineScrollHeight);
256 }
257
258
259 void WorkArea::scrollBufferView(int position)
260 {
261         stopBlinkingCursor();
262         buffer_view_->scrollDocView(position);
263         redraw();
264         if (lyxrc.cursor_follows_scrollbar) {
265                 buffer_view_->setCursorFromScrollbar();
266                 lyx_view_.updateLayoutChoice();
267         }
268         // Show the cursor immediately after any operation.
269         startBlinkingCursor();
270 }
271
272
273 void WorkArea::showCursor()
274 {
275         if (cursor_visible_)
276                 return;
277
278         if (!buffer_view_->buffer())
279                 return;
280
281         CursorShape shape = BAR_SHAPE;
282
283         LyXText const & text = *buffer_view_->cursor().innerText();
284         LyXFont const & realfont = text.real_current_font;
285         BufferParams const & bp = buffer_view_->buffer()->params();
286         bool const samelang = realfont.language() == bp.language;
287         bool const isrtl = realfont.isVisibleRightToLeft();
288
289         if (!samelang || isrtl != bp.language->rightToLeft()) {
290                 shape = L_SHAPE;
291                 if (isrtl)
292                         shape = REVERSED_L_SHAPE;
293         }
294
295         // The ERT language hack needs fixing up
296         if (realfont.language() == latex_language)
297                 shape = BAR_SHAPE;
298
299         LyXFont const font = buffer_view_->cursor().getFont();
300         FontMetrics const & fm = theFontMetrics(font);
301         int const asc = fm.maxAscent();
302         int const des = fm.maxDescent();
303         int h = asc + des;
304         int x = 0;
305         int y = 0;
306         buffer_view_->cursor().getPos(x, y);
307         y -= asc;
308
309         // if it doesn't touch the screen, don't try to show it
310         if (y + h < 0 || y >= height())
311                 return;
312
313         cursor_visible_ = true;
314         showCursor(x, y, h, shape);
315 }
316
317
318 void WorkArea::hideCursor()
319 {
320         if (!cursor_visible_)
321                 return;
322
323         cursor_visible_ = false;
324         removeCursor();
325 }
326
327
328 void WorkArea::toggleCursor()
329 {
330         if (buffer_view_->buffer()) {
331
332                 if (cursor_visible_)
333                         hideCursor();
334                 else
335                         showCursor();
336
337                 // Use this opportunity to deal with any child processes that
338                 // have finished but are waiting to communicate this fact
339                 // to the rest of LyX.
340                 ForkedcallsController & fcc = ForkedcallsController::get();
341                 fcc.handleCompletedProcesses();
342         }
343
344         cursor_timeout_.restart();
345 }
346
347
348 void WorkArea::displayMessage(lyx::docstring const & message)
349 {
350         lyx_view_.message(message);
351 }
352
353 } // namespace frontend
354 } // namespace lyx