3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
11 * Splash screen code added by Angus Leeming
16 #include "frontends/WorkArea.h"
18 #include "frontends/Application.h"
19 #include "frontends/FontMetrics.h"
21 #include "FuncRequest.h"
24 #include "BufferView.h"
26 #include "BufferParams.h"
27 #include "CoordCache.h"
36 #include "MetricsInfo.h"
39 #include "support/ForkedcallsController.h"
41 #include <boost/utility.hpp>
42 #include <boost/bind.hpp>
43 #include <boost/current_function.hpp>
45 using lyx::support::ForkedcallsController;
55 // All the below connection objects are needed because of a bug in some
56 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
57 // to these connections we avoid a segfault upon startup, and also at exit.
60 boost::signals::connection timecon;
67 WorkArea::WorkArea(int id, LyXView & lyx_view)
68 : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
69 id_(id), cursor_visible_(false), cursor_timeout_(400)
71 // Start loading the pixmap as soon as possible
72 //if (lyxrc.show_banner) {
77 timecon = cursor_timeout_.timeout
78 .connect(boost::bind(&WorkArea::toggleCursor, this));
80 cursor_timeout_.start();
84 void WorkArea::setBufferView(BufferView * buffer_view)
87 message_connection_.disconnect();
88 lyx_view_.disconnectBufferView();
92 buffer_view_ = buffer_view;
95 message_connection_ = buffer_view_->message.connect(
96 boost::bind(&WorkArea::displayMessage, this, _1));
98 lyx_view_.connectBufferView(*buffer_view);
102 BufferView & WorkArea::bufferView()
104 return *buffer_view_;
108 BufferView const & WorkArea::bufferView() const
110 return *buffer_view_;
114 void WorkArea::stopBlinkingCursor()
116 cursor_timeout_.stop();
121 void WorkArea::startBlinkingCursor()
124 cursor_timeout_.restart();
128 void WorkArea::redraw()
130 if (!buffer_view_ || !buffer_view_->buffer()) {
132 // The argument here are meaningless.
137 // No need to do anything if this is the current view. The BufferView
138 // metrics are already up to date.
139 if (&lyx_view_ != theApp()->currentView()) {
140 // FIXME: it would be nice to optimize for the off-screen case.
141 buffer_view_->updateMetrics(false);
142 buffer_view_->cursor().fixIfBroken();
147 // update cursor position, because otherwise it has to wait until
148 // the blinking interval is over
149 if (cursor_visible_) {
154 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
157 LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
159 int const ymin = std::max(vi.y1, 0);
160 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
162 expose(0, ymin, width(), ymax - ymin);
164 //LYXERR(Debug::WORKAREA)
165 //<< " ymin = " << ymin << " width() = " << width()
166 // << " ymax-ymin = " << ymax-ymin << std::endl;
168 if (lyxerr.debugging(Debug::WORKAREA))
169 buffer_view_->coordCache().dump();
173 void WorkArea::processKeySym(KeySymbolPtr key, key_modifier::state state)
175 // In order to avoid bad surprise in the middle of an operation, we better stop
176 // the blinking cursor.
177 stopBlinkingCursor();
179 theLyXFunc().setLyXView(&lyx_view_);
180 theLyXFunc().processKeySym(key, state);
182 /* When we move around, or type, it's nice to be able to see
183 * the cursor immediately after the keypress.
185 startBlinkingCursor();
189 void WorkArea::dispatch(FuncRequest const & cmd0, key_modifier::state k)
192 if (cmd0.action == LFUN_FILE_OPEN) {
193 lyx_view_.dispatch(cmd0);
197 theLyXFunc().setLyXView(&lyx_view_);
201 if (cmd0.action == LFUN_MOUSE_PRESS) {
202 if (k == key_modifier::shift)
203 cmd = FuncRequest(cmd0, "region-select");
204 else if (k == key_modifier::ctrl)
205 cmd = FuncRequest(cmd0, "paragraph-select");
212 // In order to avoid bad surprise in the middle of an operation, we better stop
213 // the blinking cursor.
214 if (!(cmd.action == LFUN_MOUSE_MOTION
215 && cmd.button() == mouse_button::none))
216 stopBlinkingCursor();
218 bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
223 // Skip these when selecting
224 if (cmd.action != LFUN_MOUSE_MOTION) {
225 lyx_view_.updateLayoutChoice();
226 lyx_view_.updateMenubar();
227 lyx_view_.updateToolbars();
230 // GUI tweaks except with mouse motion with no button pressed.
231 if (!(cmd.action == LFUN_MOUSE_MOTION
232 && cmd.button() == mouse_button::none)) {
233 // Slight hack: this is only called currently when we
234 // clicked somewhere, so we force through the display
235 // of the new status here.
236 lyx_view_.clearMessage();
238 // Show the cursor immediately after any operation.
239 startBlinkingCursor();
244 void WorkArea::resizeBufferView()
246 lyx_view_.busy(true);
247 lyx_view_.message(_("Formatting document..."));
248 buffer_view_->workAreaResize(width(), height());
249 lyx_view_.updateLayoutChoice();
250 lyx_view_.clearMessage();
251 lyx_view_.busy(false);
255 void WorkArea::updateScrollbar()
257 buffer_view_->updateScrollbar();
258 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
259 setScrollbarParams(scroll_.height, scroll_.position,
260 scroll_.lineScrollHeight);
264 void WorkArea::scrollBufferView(int position)
266 stopBlinkingCursor();
267 buffer_view_->scrollDocView(position);
269 if (lyxrc.cursor_follows_scrollbar) {
270 buffer_view_->setCursorFromScrollbar();
271 lyx_view_.updateLayoutChoice();
273 // Show the cursor immediately after any operation.
274 startBlinkingCursor();
278 void WorkArea::showCursor()
283 if (!buffer_view_->buffer())
286 CursorShape shape = BAR_SHAPE;
288 Text const & text = *buffer_view_->cursor().innerText();
289 Font const & realfont = text.real_current_font;
290 BufferParams const & bp = buffer_view_->buffer()->params();
291 bool const samelang = realfont.language() == bp.language;
292 bool const isrtl = realfont.isVisibleRightToLeft();
294 if (!samelang || isrtl != bp.language->rightToLeft()) {
297 shape = REVERSED_L_SHAPE;
300 // The ERT language hack needs fixing up
301 if (realfont.language() == latex_language)
304 Font const font = buffer_view_->cursor().getFont();
305 FontMetrics const & fm = theFontMetrics(font);
306 int const asc = fm.maxAscent();
307 int const des = fm.maxDescent();
311 buffer_view_->cursor().getPos(x, y);
314 // if it doesn't touch the screen, don't try to show it
315 if (y + h < 0 || y >= height())
318 cursor_visible_ = true;
319 showCursor(x, y, h, shape);
323 void WorkArea::hideCursor()
325 if (!cursor_visible_)
328 cursor_visible_ = false;
333 void WorkArea::toggleCursor()
335 if (buffer_view_->buffer()) {
342 // Use this opportunity to deal with any child processes that
343 // have finished but are waiting to communicate this fact
344 // to the rest of LyX.
345 ForkedcallsController & fcc = ForkedcallsController::get();
346 fcc.handleCompletedProcesses();
349 cursor_timeout_.restart();
353 void WorkArea::displayMessage(lyx::docstring const & message)
355 lyx_view_.message(message);
358 } // namespace frontend