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"
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/noncopyable.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)
71 buffer.workAreaManager().add(this);
73 timecon = cursor_timeout_.timeout
74 .connect(boost::bind(&WorkArea::toggleCursor, this));
76 cursor_timeout_.start();
82 buffer_view_->buffer().workAreaManager().remove(this);
87 void WorkArea::close()
89 lyx_view_->removeWorkArea(this);
92 //void WorkArea::setLyXView(LyXView * lyx_view)
94 // lyx_view_ = lyx_view;
98 BufferView & WorkArea::bufferView()
100 return *buffer_view_;
104 BufferView const & WorkArea::bufferView() const
106 return *buffer_view_;
110 void WorkArea::stopBlinkingCursor()
112 cursor_timeout_.stop();
117 void WorkArea::startBlinkingCursor()
120 cursor_timeout_.restart();
124 void WorkArea::redraw()
127 // No need to redraw in this case.
130 // No need to do anything if this is the current view. The BufferView
131 // metrics are already up to date.
132 if (lyx_view_ != theApp()->currentView()) {
133 // FIXME: it would be nice to optimize for the off-screen case.
134 buffer_view_->updateMetrics(false);
135 buffer_view_->cursor().fixIfBroken();
140 // update cursor position, because otherwise it has to wait until
141 // the blinking interval is over
142 if (cursor_visible_) {
147 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
149 LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
151 int const ymin = std::max(vi.y1, 0);
152 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
154 expose(0, ymin, width(), ymax - ymin);
156 //LYXERR(Debug::WORKAREA)
157 //<< " ymin = " << ymin << " width() = " << width()
158 // << " ymax-ymin = " << ymax-ymin << std::endl;
160 if (lyxerr.debugging(Debug::WORKAREA))
161 buffer_view_->coordCache().dump();
165 void WorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
167 // In order to avoid bad surprise in the middle of an operation, we better stop
168 // the blinking cursor.
169 stopBlinkingCursor();
171 theLyXFunc().setLyXView(lyx_view_);
172 theLyXFunc().processKeySym(key, mod);
176 void WorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
179 if (cmd0.action == LFUN_FILE_OPEN) {
180 lyx_view_->dispatch(cmd0);
184 theLyXFunc().setLyXView(lyx_view_);
188 if (cmd0.action == LFUN_MOUSE_PRESS) {
189 if (mod == ShiftModifier)
190 cmd = FuncRequest(cmd0, "region-select");
191 else if (mod == ControlModifier)
192 cmd = FuncRequest(cmd0, "paragraph-select");
199 // In order to avoid bad surprise in the middle of an operation, we better stop
200 // the blinking cursor.
201 if (!(cmd.action == LFUN_MOUSE_MOTION
202 && cmd.button() == mouse_button::none))
203 stopBlinkingCursor();
205 buffer_view_->mouseEventDispatch(cmd);
207 // Skip these when selecting
208 if (cmd.action != LFUN_MOUSE_MOTION) {
209 lyx_view_->updateLayoutChoice();
210 lyx_view_->updateToolbars();
213 // GUI tweaks except with mouse motion with no button pressed.
214 if (!(cmd.action == LFUN_MOUSE_MOTION
215 && cmd.button() == mouse_button::none)) {
216 // Slight hack: this is only called currently when we
217 // clicked somewhere, so we force through the display
218 // of the new status here.
219 lyx_view_->clearMessage();
221 // Show the cursor immediately after any operation.
222 startBlinkingCursor();
227 void WorkArea::resizeBufferView()
229 // WARNING: Please don't put any code that will trigger a repaint here!
230 // We are already inside a paint event.
231 lyx_view_->setBusy(true);
232 buffer_view_->resize(width(), height());
233 lyx_view_->updateLayoutChoice();
234 lyx_view_->setBusy(false);
238 void WorkArea::updateScrollbar()
240 buffer_view_->updateScrollbar();
241 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
242 setScrollbarParams(scroll_.height, scroll_.position,
243 scroll_.lineScrollHeight);
247 void WorkArea::showCursor()
252 CursorShape shape = BAR_SHAPE;
254 Font const & realfont = buffer_view_->cursor().real_current_font;
255 BufferParams const & bp = buffer_view_->buffer().params();
256 bool const samelang = realfont.language() == bp.language;
257 bool const isrtl = realfont.isVisibleRightToLeft();
259 if (!samelang || isrtl != bp.language->rightToLeft()) {
262 shape = REVERSED_L_SHAPE;
265 // The ERT language hack needs fixing up
266 if (realfont.language() == latex_language)
269 Font const font = buffer_view_->cursor().getFont();
270 FontMetrics const & fm = theFontMetrics(font);
271 int const asc = fm.maxAscent();
272 int const des = fm.maxDescent();
276 buffer_view_->cursor().getPos(x, y);
279 // if it doesn't touch the screen, don't try to show it
280 if (y + h < 0 || y >= height())
283 cursor_visible_ = true;
284 showCursor(x, y, h, shape);
288 void WorkArea::hideCursor()
290 if (!cursor_visible_)
293 cursor_visible_ = false;
298 void WorkArea::toggleCursor()
305 // Use this opportunity to deal with any child processes that
306 // have finished but are waiting to communicate this fact
307 // to the rest of LyX.
308 ForkedcallsController & fcc = ForkedcallsController::get();
309 fcc.handleCompletedProcesses();
311 cursor_timeout_.restart();
314 } // namespace frontend