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 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
150 LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
152 int const ymin = std::max(vi.y1, 0);
153 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
155 expose(0, ymin, width(), ymax - ymin);
157 //LYXERR(Debug::WORKAREA)
158 //<< " ymin = " << ymin << " width() = " << width()
159 // << " ymax-ymin = " << ymax-ymin << std::endl;
161 if (lyxerr.debugging(Debug::WORKAREA))
162 buffer_view_->coordCache().dump();
166 void WorkArea::processKeySym(KeySymbolPtr key, key_modifier::state state)
168 // In order to avoid bad surprise in the middle of an operation, we better stop
169 // the blinking cursor.
170 stopBlinkingCursor();
172 theLyXFunc().setLyXView(&lyx_view_);
173 theLyXFunc().processKeySym(key, state);
175 /* When we move around, or type, it's nice to be able to see
176 * the cursor immediately after the keypress.
178 startBlinkingCursor();
182 void WorkArea::dispatch(FuncRequest const & cmd0, key_modifier::state k)
185 if (cmd0.action == LFUN_FILE_OPEN) {
186 lyx_view_.dispatch(cmd0);
190 theLyXFunc().setLyXView(&lyx_view_);
194 if (cmd0.action == LFUN_MOUSE_PRESS) {
195 if (k == key_modifier::shift)
196 cmd = FuncRequest(cmd0, "region-select");
197 else if (k == key_modifier::ctrl)
198 cmd = FuncRequest(cmd0, "paragraph-select");
205 // In order to avoid bad surprise in the middle of an operation, we better stop
206 // the blinking cursor.
207 if (!(cmd.action == LFUN_MOUSE_MOTION
208 && cmd.button() == mouse_button::none))
209 stopBlinkingCursor();
211 bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
216 // Skip these when selecting
217 if (cmd.action != LFUN_MOUSE_MOTION) {
218 lyx_view_.updateLayoutChoice();
219 lyx_view_.updateMenubar();
220 lyx_view_.updateToolbars();
223 // GUI tweaks except with mouse motion with no button pressed.
224 if (!(cmd.action == LFUN_MOUSE_MOTION
225 && cmd.button() == mouse_button::none)) {
226 // Slight hack: this is only called currently when we
227 // clicked somewhere, so we force through the display
228 // of the new status here.
229 lyx_view_.clearMessage();
231 // Show the cursor immediately after any operation.
232 startBlinkingCursor();
237 void WorkArea::resizeBufferView()
239 lyx_view_.busy(true);
240 lyx_view_.message(_("Formatting document..."));
241 buffer_view_->workAreaResize(width(), height());
242 lyx_view_.updateLayoutChoice();
243 lyx_view_.clearMessage();
244 lyx_view_.busy(false);
248 void WorkArea::updateScrollbar()
250 buffer_view_->updateScrollbar();
251 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
252 setScrollbarParams(scroll_.height, scroll_.position,
253 scroll_.lineScrollHeight);
257 void WorkArea::scrollBufferView(int position)
259 stopBlinkingCursor();
260 buffer_view_->scrollDocView(position);
262 if (lyxrc.cursor_follows_scrollbar) {
263 buffer_view_->setCursorFromScrollbar();
264 lyx_view_.updateLayoutChoice();
266 // Show the cursor immediately after any operation.
267 startBlinkingCursor();
271 void WorkArea::showCursor()
276 if (!buffer_view_->buffer())
279 CursorShape shape = BAR_SHAPE;
281 Text const & text = *buffer_view_->cursor().innerText();
282 Font const & realfont = text.real_current_font;
283 BufferParams const & bp = buffer_view_->buffer()->params();
284 bool const samelang = realfont.language() == bp.language;
285 bool const isrtl = realfont.isVisibleRightToLeft();
287 if (!samelang || isrtl != bp.language->rightToLeft()) {
290 shape = REVERSED_L_SHAPE;
293 // The ERT language hack needs fixing up
294 if (realfont.language() == latex_language)
297 Font const font = buffer_view_->cursor().getFont();
298 FontMetrics const & fm = theFontMetrics(font);
299 int const asc = fm.maxAscent();
300 int const des = fm.maxDescent();
304 buffer_view_->cursor().getPos(x, y);
307 // if it doesn't touch the screen, don't try to show it
308 if (y + h < 0 || y >= height())
311 cursor_visible_ = true;
312 showCursor(x, y, h, shape);
316 void WorkArea::hideCursor()
318 if (!cursor_visible_)
321 cursor_visible_ = false;
326 void WorkArea::toggleCursor()
328 if (buffer_view_->buffer()) {
335 // Use this opportunity to deal with any child processes that
336 // have finished but are waiting to communicate this fact
337 // to the rest of LyX.
338 ForkedcallsController & fcc = ForkedcallsController::get();
339 fcc.handleCompletedProcesses();
342 cursor_timeout_.restart();
346 void WorkArea::displayMessage(lyx::docstring const & message)
348 lyx_view_.message(message);
351 } // namespace frontend