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"
31 #include "KeySymbol.h"
35 #include "MetricsInfo.h"
38 #include "support/ForkedcallsController.h"
39 #include "support/FileName.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(Buffer & buffer, LyXView & lv)
68 : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
69 cursor_visible_(false), cursor_timeout_(400)
72 timecon = cursor_timeout_.timeout
73 .connect(boost::bind(&WorkArea::toggleCursor, this));
75 bufferChangedConnection_ =
76 buffer.changed.connect(
77 boost::bind(&WorkArea::redraw, this));
79 bufferClosingConnection_ =
80 buffer.closing.connect(
81 boost::bind(&WorkArea::close, this));
83 cursor_timeout_.start();
89 bufferChangedConnection_.disconnect();
90 bufferClosingConnection_.disconnect();
96 void WorkArea::close()
98 lyx_view_->removeWorkArea(this);
101 //void WorkArea::setLyXView(LyXView * lyx_view)
103 // lyx_view_ = lyx_view;
107 BufferView & WorkArea::bufferView()
109 return *buffer_view_;
113 BufferView const & WorkArea::bufferView() const
115 return *buffer_view_;
119 void WorkArea::stopBlinkingCursor()
121 cursor_timeout_.stop();
126 void WorkArea::startBlinkingCursor()
129 cursor_timeout_.restart();
133 void WorkArea::redraw()
136 // No need to redraw in this case.
139 // No need to do anything if this is the current view. The BufferView
140 // metrics are already up to date.
141 if (lyx_view_ != theApp()->currentView()) {
142 // FIXME: it would be nice to optimize for the off-screen case.
143 buffer_view_->updateMetrics(false);
144 buffer_view_->cursor().fixIfBroken();
149 // update cursor position, because otherwise it has to wait until
150 // the blinking interval is over
151 if (cursor_visible_) {
156 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
158 LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
160 int const ymin = std::max(vi.y1, 0);
161 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
163 expose(0, ymin, width(), ymax - ymin);
165 //LYXERR(Debug::WORKAREA)
166 //<< " ymin = " << ymin << " width() = " << width()
167 // << " ymax-ymin = " << ymax-ymin << std::endl;
169 if (lyxerr.debugging(Debug::WORKAREA))
170 buffer_view_->coordCache().dump();
174 void WorkArea::processKeySym(KeySymbol const & key, key_modifier::state state)
176 // In order to avoid bad surprise in the middle of an operation, we better stop
177 // the blinking cursor.
178 stopBlinkingCursor();
180 theLyXFunc().setLyXView(lyx_view_);
181 theLyXFunc().processKeySym(key, state);
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 // In order to avoid bad surprise in the middle of an operation, we better stop
209 // the blinking cursor.
210 if (!(cmd.action == LFUN_MOUSE_MOTION
211 && cmd.button() == mouse_button::none))
212 stopBlinkingCursor();
214 bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
217 buffer_view_->buffer().changed();
219 // Skip these when selecting
220 if (cmd.action != LFUN_MOUSE_MOTION) {
221 lyx_view_->updateLayoutChoice();
222 lyx_view_->updateToolbars();
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();
233 // Show the cursor immediately after any operation.
234 startBlinkingCursor();
239 void WorkArea::resizeBufferView()
241 // WARNING: Please don't put any code that will trigger a repaint here!
242 // We are already inside a paint event.
243 lyx_view_->busy(true);
244 buffer_view_->resize(width(), height());
245 lyx_view_->updateLayoutChoice();
246 lyx_view_->busy(false);
250 void WorkArea::updateScrollbar()
252 buffer_view_->updateScrollbar();
253 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
254 setScrollbarParams(scroll_.height, scroll_.position,
255 scroll_.lineScrollHeight);
259 void WorkArea::showCursor()
264 CursorShape shape = BAR_SHAPE;
266 Font const & realfont = buffer_view_->cursor().real_current_font;
267 BufferParams const & bp = buffer_view_->buffer().params();
268 bool const samelang = realfont.language() == bp.language;
269 bool const isrtl = realfont.isVisibleRightToLeft();
271 if (!samelang || isrtl != bp.language->rightToLeft()) {
274 shape = REVERSED_L_SHAPE;
277 // The ERT language hack needs fixing up
278 if (realfont.language() == latex_language)
281 Font const font = buffer_view_->cursor().getFont();
282 FontMetrics const & fm = theFontMetrics(font);
283 int const asc = fm.maxAscent();
284 int const des = fm.maxDescent();
288 buffer_view_->cursor().getPos(x, y);
291 // if it doesn't touch the screen, don't try to show it
292 if (y + h < 0 || y >= height())
295 cursor_visible_ = true;
296 showCursor(x, y, h, shape);
300 void WorkArea::hideCursor()
302 if (!cursor_visible_)
305 cursor_visible_ = false;
310 void WorkArea::toggleCursor()
317 // Use this opportunity to deal with any child processes that
318 // have finished but are waiting to communicate this fact
319 // to the rest of LyX.
320 ForkedcallsController & fcc = ForkedcallsController::get();
321 fcc.handleCompletedProcesses();
323 cursor_timeout_.restart();
326 } // namespace frontend