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"
25 #include "BufferView.h"
27 #include "bufferparams.h"
28 #include "coordcache.h"
38 #include "metricsinfo.h"
39 #include "paragraph.h"
40 #include "rowpainter.h"
43 #include "support/forkedcontr.h"
45 #include <boost/utility.hpp>
46 #include <boost/bind.hpp>
47 #include <boost/current_function.hpp>
49 using lyx::support::ForkedcallsController;
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.
64 boost::signals::connection timecon;
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)
75 // Start loading the pixmap as soon as possible
76 //if (lyxrc.show_banner) {
81 timecon = cursor_timeout_.timeout
82 .connect(boost::bind(&WorkArea::toggleCursor, this));
84 cursor_timeout_.start();
88 void WorkArea::setBufferView(BufferView * buffer_view)
91 message_connection_.disconnect();
92 lyx_view_.disconnectBufferView();
96 buffer_view_ = buffer_view;
99 message_connection_ = buffer_view_->message.connect(
100 boost::bind(&WorkArea::displayMessage, this, _1));
102 lyx_view_.connectBufferView(*buffer_view);
106 BufferView & WorkArea::bufferView()
108 return *buffer_view_;
112 BufferView const & WorkArea::bufferView() const
114 return *buffer_view_;
118 void WorkArea::stopBlinkingCursor()
120 cursor_timeout_.stop();
125 void WorkArea::startBlinkingCursor()
128 cursor_timeout_.restart();
132 void WorkArea::redraw()
134 if (!buffer_view_ || !buffer_view_->buffer()) {
136 // The argument here are meaningless.
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);
149 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
152 if (lyxerr.debugging(Debug::WORKAREA)) {
153 lyxerr[Debug::WORKAREA] << "WorkArea::redraw screen" << endl;
155 int const ymin = std::max(vi.y1, 0);
156 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
158 expose(0, ymin, width(), ymax - ymin);
160 //lyxerr[Debug::WORKAREA]
161 //<< " ymin = " << ymin << " width() = " << width()
162 // << " ymax-ymin = " << ymax-ymin << std::endl;
164 if (lyxerr.debugging(Debug::WORKAREA))
165 buffer_view_->coordCache().dump();
169 void WorkArea::processKeySym(LyXKeySymPtr key, key_modifier::state state)
171 // In order to avoid bad surprise in the middle of an operation, we better stop
172 // the blinking cursor.
173 stopBlinkingCursor();
175 theLyXFunc().setLyXView(&lyx_view_);
176 theLyXFunc().processKeySym(key, state);
178 /* When we move around, or type, it's nice to be able to see
179 * the cursor immediately after the keypress.
181 startBlinkingCursor();
185 void WorkArea::dispatch(FuncRequest const & cmd0, key_modifier::state k)
188 if (cmd0.action == LFUN_FILE_OPEN) {
189 lyx_view_.dispatch(cmd0);
193 theLyXFunc().setLyXView(&lyx_view_);
197 if (cmd0.action == LFUN_MOUSE_PRESS) {
198 if (k == key_modifier::shift)
199 cmd = FuncRequest(cmd0, "region-select");
200 else if (k == key_modifier::ctrl)
201 cmd = FuncRequest(cmd0, "paragraph-select");
208 bool needRedraw = buffer_view_->workAreaDispatch(cmd);
210 // Skip these when selecting
211 if (cmd.action != LFUN_MOUSE_MOTION) {
212 lyx_view_.updateLayoutChoice();
213 lyx_view_.updateMenubar();
214 lyx_view_.updateToolbars();
218 // GUI tweaks except with mouse motion with no button pressed.
219 if (!(cmd.action == LFUN_MOUSE_MOTION
220 && cmd.button() == mouse_button::none)) {
221 // Slight hack: this is only called currently when we
222 // clicked somewhere, so we force through the display
223 // of the new status here.
224 lyx_view_.clearMessage();
226 // Show the cursor immediately after any operation.
236 void WorkArea::resizeBufferView()
238 lyx_view_.busy(true);
239 lyx_view_.message(_("Formatting document..."));
240 buffer_view_->workAreaResize(width(), height());
241 lyx_view_.updateLayoutChoice();
242 lyx_view_.busy(false);
243 lyx_view_.clearMessage();
247 void WorkArea::updateScrollbar()
249 buffer_view_->updateScrollbar();
250 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
251 setScrollbarParams(scroll_.height, scroll_.position,
252 scroll_.lineScrollHeight);
256 void WorkArea::scrollBufferView(int position)
258 buffer_view_->scrollDocView(position);
261 if (lyxrc.cursor_follows_scrollbar) {
262 buffer_view_->setCursorFromScrollbar();
263 lyx_view_.updateLayoutChoice();
269 void WorkArea::showCursor()
274 if (!buffer_view_->buffer())
277 CursorShape shape = BAR_SHAPE;
279 LyXText const & text = *buffer_view_->getLyXText();
280 LyXFont const & realfont = text.real_current_font;
281 BufferParams const & bp = buffer_view_->buffer()->params();
282 bool const samelang = realfont.language() == bp.language;
283 bool const isrtl = realfont.isVisibleRightToLeft();
285 if (!samelang || isrtl != bp.language->rightToLeft()) {
288 shape = REVERSED_L_SHAPE;
291 // The ERT language hack needs fixing up
292 if (realfont.language() == latex_language)
295 LyXFont const font = buffer_view_->cursor().getFont();
296 FontMetrics const & fm = theFontMetrics(font);
297 int const asc = fm.maxAscent();
298 int const des = fm.maxDescent();
302 buffer_view_->cursor().getPos(x, y);
305 // if it doesn't touch the screen, don't try to show it
306 if (y + h < 0 || y >= height())
309 cursor_visible_ = true;
310 showCursor(x, y, h, shape);
314 void WorkArea::hideCursor()
316 if (!cursor_visible_)
319 cursor_visible_ = false;
324 void WorkArea::toggleCursor()
326 if (buffer_view_->buffer()) {
333 // Use this opportunity to deal with any child processes that
334 // have finished but are waiting to communicate this fact
335 // to the rest of LyX.
336 ForkedcallsController & fcc = ForkedcallsController::get();
337 fcc.handleCompletedProcesses();
340 cursor_timeout_.restart();
344 void WorkArea::displayMessage(lyx::docstring const & message)
346 lyx_view_.message(message);
349 } // namespace frontend