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"
20 #include "frontends/LyXView.h"
22 #include "BufferView.h"
24 #include "BufferParams.h"
26 #include "CoordCache.h"
30 #include "FuncRequest.h"
35 #include "MetricsInfo.h"
39 #include "support/ForkedcallsController.h"
40 #include "support/FileName.h"
42 #include <boost/utility.hpp>
43 #include <boost/bind.hpp>
44 #include <boost/current_function.hpp>
46 using lyx::support::ForkedcallsController;
56 // All the below connection objects are needed because of a bug in some
57 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
58 // to these connections we avoid a segfault upon startup, and also at exit.
61 boost::signals::connection timecon;
68 WorkArea::WorkArea(Buffer & buffer, LyXView & lv)
69 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
70 cursor_visible_(false), cursor_timeout_(400)
73 timecon = cursor_timeout_.timeout
74 .connect(boost::bind(&WorkArea::toggleCursor, this));
76 bufferChangedConnection_ =
77 buffer.changed.connect(
78 boost::bind(&WorkArea::redraw, this));
80 bufferClosingConnection_ =
81 buffer.closing.connect(
82 boost::bind(&WorkArea::close, this));
84 cursor_timeout_.start();
90 bufferChangedConnection_.disconnect();
91 bufferClosingConnection_.disconnect();
93 // current buffer is going to be switched-off, save cursor pos
94 // Ideally, the whole cursor stack should be saved, but session
95 // currently can only handle bottom (whole document) level pit and pos.
96 // That is to say, if a cursor is in a nested inset, it will be
97 // restore to the left of the top level inset.
98 Cursor & cur = buffer_view_->cursor();
99 LyX::ref().session().lastFilePos().save(
100 support::FileName(buffer_view_->buffer().fileName()),
101 boost::tie(cur.bottom().pit(), cur.bottom().pos()) );
107 void WorkArea::close()
109 lyx_view_->removeWorkArea(this);
112 //void WorkArea::setLyXView(LyXView * lyx_view)
114 // lyx_view_ = lyx_view;
118 BufferView & WorkArea::bufferView()
120 return *buffer_view_;
124 BufferView const & WorkArea::bufferView() const
126 return *buffer_view_;
130 void WorkArea::stopBlinkingCursor()
132 cursor_timeout_.stop();
137 void WorkArea::startBlinkingCursor()
140 cursor_timeout_.restart();
144 void WorkArea::redraw()
147 // No need to redraw in this case.
150 // No need to do anything if this is the current view. The BufferView
151 // metrics are already up to date.
152 if (lyx_view_ != theApp()->currentView()) {
153 // FIXME: it would be nice to optimize for the off-screen case.
154 buffer_view_->updateMetrics(false);
155 buffer_view_->cursor().fixIfBroken();
160 // update cursor position, because otherwise it has to wait until
161 // the blinking interval is over
162 if (cursor_visible_) {
167 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
169 LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
171 int const ymin = std::max(vi.y1, 0);
172 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
174 expose(0, ymin, width(), ymax - ymin);
176 //LYXERR(Debug::WORKAREA)
177 //<< " ymin = " << ymin << " width() = " << width()
178 // << " ymax-ymin = " << ymax-ymin << std::endl;
180 if (lyxerr.debugging(Debug::WORKAREA))
181 buffer_view_->coordCache().dump();
185 void WorkArea::processKeySym(KeySymbolPtr key, key_modifier::state state)
187 // In order to avoid bad surprise in the middle of an operation, we better stop
188 // the blinking cursor.
189 stopBlinkingCursor();
191 theLyXFunc().setLyXView(lyx_view_);
192 theLyXFunc().processKeySym(key, state);
196 void WorkArea::dispatch(FuncRequest const & cmd0, key_modifier::state k)
199 if (cmd0.action == LFUN_FILE_OPEN) {
200 lyx_view_->dispatch(cmd0);
204 theLyXFunc().setLyXView(lyx_view_);
208 if (cmd0.action == LFUN_MOUSE_PRESS) {
209 if (k == key_modifier::shift)
210 cmd = FuncRequest(cmd0, "region-select");
211 else if (k == key_modifier::ctrl)
212 cmd = FuncRequest(cmd0, "paragraph-select");
219 // In order to avoid bad surprise in the middle of an operation, we better stop
220 // the blinking cursor.
221 if (!(cmd.action == LFUN_MOUSE_MOTION
222 && cmd.button() == mouse_button::none))
223 stopBlinkingCursor();
225 bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
230 // Skip these when selecting
231 if (cmd.action != LFUN_MOUSE_MOTION) {
232 lyx_view_->updateLayoutChoice();
233 lyx_view_->updateMenubar();
234 lyx_view_->updateToolbars();
237 // GUI tweaks except with mouse motion with no button pressed.
238 if (!(cmd.action == LFUN_MOUSE_MOTION
239 && cmd.button() == mouse_button::none)) {
240 // Slight hack: this is only called currently when we
241 // clicked somewhere, so we force through the display
242 // of the new status here.
243 lyx_view_->clearMessage();
245 // Show the cursor immediately after any operation.
246 startBlinkingCursor();
251 void WorkArea::resizeBufferView()
253 lyx_view_->busy(true);
254 lyx_view_->message(_("Formatting document..."));
255 buffer_view_->workAreaResize(width(), height());
256 lyx_view_->updateLayoutChoice();
257 lyx_view_->clearMessage();
258 lyx_view_->busy(false);
262 void WorkArea::updateScrollbar()
264 buffer_view_->updateScrollbar();
265 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
266 setScrollbarParams(scroll_.height, scroll_.position,
267 scroll_.lineScrollHeight);
271 void WorkArea::scrollBufferView(int position)
273 stopBlinkingCursor();
274 buffer_view_->scrollDocView(position);
276 if (lyxrc.cursor_follows_scrollbar) {
277 buffer_view_->setCursorFromScrollbar();
278 lyx_view_->updateLayoutChoice();
280 // Show the cursor immediately after any operation.
281 startBlinkingCursor();
285 void WorkArea::showCursor()
290 CursorShape shape = BAR_SHAPE;
292 Text const & text = *buffer_view_->cursor().innerText();
293 Font const & realfont = text.real_current_font;
294 BufferParams const & bp = buffer_view_->buffer().params();
295 bool const samelang = realfont.language() == bp.language;
296 bool const isrtl = realfont.isVisibleRightToLeft();
298 if (!samelang || isrtl != bp.language->rightToLeft()) {
301 shape = REVERSED_L_SHAPE;
304 // The ERT language hack needs fixing up
305 if (realfont.language() == latex_language)
308 Font const font = buffer_view_->cursor().getFont();
309 FontMetrics const & fm = theFontMetrics(font);
310 int const asc = fm.maxAscent();
311 int const des = fm.maxDescent();
315 buffer_view_->cursor().getPos(x, y);
318 // if it doesn't touch the screen, don't try to show it
319 if (y + h < 0 || y >= height())
322 cursor_visible_ = true;
323 showCursor(x, y, h, shape);
327 void WorkArea::hideCursor()
329 if (!cursor_visible_)
332 cursor_visible_ = false;
337 void WorkArea::toggleCursor()
344 // Use this opportunity to deal with any child processes that
345 // have finished but are waiting to communicate this fact
346 // to the rest of LyX.
347 ForkedcallsController & fcc = ForkedcallsController::get();
348 fcc.handleCompletedProcesses();
350 cursor_timeout_.restart();
353 } // namespace frontend