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"
21 #include "frontends/WorkAreaManager.h"
23 #include "BufferView.h"
25 #include "BufferParams.h"
27 #include "CoordCache.h"
31 #include "FuncRequest.h"
32 #include "KeySymbol.h"
36 #include "MetricsInfo.h"
39 #include "support/ForkedcallsController.h"
40 #include "support/FileName.h"
42 #include <boost/noncopyable.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)
72 buffer.workAreaManager().add(this);
74 timecon = cursor_timeout_.timeout
75 .connect(boost::bind(&WorkArea::toggleCursor, this));
77 cursor_timeout_.start();
83 buffer_view_->buffer().workAreaManager().remove(this);
88 void WorkArea::close()
90 lyx_view_->removeWorkArea(this);
93 //void WorkArea::setLyXView(LyXView * lyx_view)
95 // lyx_view_ = lyx_view;
99 BufferView & WorkArea::bufferView()
101 return *buffer_view_;
105 BufferView const & WorkArea::bufferView() const
107 return *buffer_view_;
111 void WorkArea::stopBlinkingCursor()
113 cursor_timeout_.stop();
118 void WorkArea::startBlinkingCursor()
121 cursor_timeout_.restart();
125 void WorkArea::redraw()
128 // No need to redraw in this case.
131 // No need to do anything if this is the current view. The BufferView
132 // metrics are already up to date.
133 if (lyx_view_ != theApp()->currentView()) {
134 // FIXME: it would be nice to optimize for the off-screen case.
135 buffer_view_->updateMetrics(false);
136 buffer_view_->cursor().fixIfBroken();
141 // update cursor position, because otherwise it has to wait until
142 // the blinking interval is over
143 if (cursor_visible_) {
148 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(KeySymbol const & key, KeyModifier mod)
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, mod);
177 void WorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
180 if (cmd0.action == LFUN_FILE_OPEN) {
181 lyx_view_->dispatch(cmd0);
185 theLyXFunc().setLyXView(lyx_view_);
189 if (cmd0.action == LFUN_MOUSE_PRESS) {
190 if (mod == ShiftModifier)
191 cmd = FuncRequest(cmd0, "region-select");
192 else if (mod == ControlModifier)
193 cmd = FuncRequest(cmd0, "paragraph-select");
200 // In order to avoid bad surprise in the middle of an operation, we better stop
201 // the blinking cursor.
202 if (!(cmd.action == LFUN_MOUSE_MOTION
203 && cmd.button() == mouse_button::none))
204 stopBlinkingCursor();
206 bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
209 buffer_view_->buffer().changed();
211 // Skip these when selecting
212 if (cmd.action != LFUN_MOUSE_MOTION) {
213 lyx_view_->updateLayoutChoice();
214 lyx_view_->updateToolbars();
217 // GUI tweaks except with mouse motion with no button pressed.
218 if (!(cmd.action == LFUN_MOUSE_MOTION
219 && cmd.button() == mouse_button::none)) {
220 // Slight hack: this is only called currently when we
221 // clicked somewhere, so we force through the display
222 // of the new status here.
223 lyx_view_->clearMessage();
225 // Show the cursor immediately after any operation.
226 startBlinkingCursor();
231 void WorkArea::resizeBufferView()
233 // WARNING: Please don't put any code that will trigger a repaint here!
234 // We are already inside a paint event.
235 lyx_view_->busy(true);
236 buffer_view_->resize(width(), height());
237 lyx_view_->updateLayoutChoice();
238 lyx_view_->busy(false);
242 void WorkArea::updateScrollbar()
244 buffer_view_->updateScrollbar();
245 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
246 setScrollbarParams(scroll_.height, scroll_.position,
247 scroll_.lineScrollHeight);
251 void WorkArea::showCursor()
256 CursorShape shape = BAR_SHAPE;
258 Font const & realfont = buffer_view_->cursor().real_current_font;
259 BufferParams const & bp = buffer_view_->buffer().params();
260 bool const samelang = realfont.language() == bp.language;
261 bool const isrtl = realfont.isVisibleRightToLeft();
263 if (!samelang || isrtl != bp.language->rightToLeft()) {
266 shape = REVERSED_L_SHAPE;
269 // The ERT language hack needs fixing up
270 if (realfont.language() == latex_language)
273 Font const font = buffer_view_->cursor().getFont();
274 FontMetrics const & fm = theFontMetrics(font);
275 int const asc = fm.maxAscent();
276 int const des = fm.maxDescent();
280 buffer_view_->cursor().getPos(x, y);
283 // if it doesn't touch the screen, don't try to show it
284 if (y + h < 0 || y >= height())
287 cursor_visible_ = true;
288 showCursor(x, y, h, shape);
292 void WorkArea::hideCursor()
294 if (!cursor_visible_)
297 cursor_visible_ = false;
302 void WorkArea::toggleCursor()
309 // Use this opportunity to deal with any child processes that
310 // have finished but are waiting to communicate this fact
311 // to the rest of LyX.
312 ForkedcallsController & fcc = ForkedcallsController::get();
313 fcc.handleCompletedProcesses();
315 cursor_timeout_.restart();
318 } // namespace frontend