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"
25 #include "BufferView.h"
27 #include "bufferparams.h"
28 #include "coordcache.h"
38 #include "metricsinfo.h"
39 #include "paragraph.h"
40 #include "rowpainter.h"
43 #include "support/filetools.h" // LibFileSearch
44 #include "support/forkedcontr.h"
46 #include <boost/utility.hpp>
47 #include <boost/bind.hpp>
48 #include <boost/current_function.hpp>
50 using lyx::support::libFileSearch;
51 using lyx::support::ForkedcallsController;
61 // All the below connection objects are needed because of a bug in some
62 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
63 // to these connections we avoid a segfault upon startup, and also at exit.
66 boost::signals::connection timecon;
73 WorkArea::WorkArea(int id, LyXView & lyx_view)
74 : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
75 id_(id), cursor_visible_(false), cursor_timeout_(400)
77 // Start loading the pixmap as soon as possible
78 //if (lyxrc.show_banner) {
83 timecon = cursor_timeout_.timeout
84 .connect(boost::bind(&WorkArea::toggleCursor, this));
86 cursor_timeout_.start();
90 void WorkArea::setBufferView(BufferView * buffer_view)
93 message_connection_.disconnect();
94 lyx_view_.disconnectBufferView();
98 buffer_view_ = buffer_view;
101 message_connection_ = buffer_view_->message.connect(
102 boost::bind(&WorkArea::displayMessage, this, _1));
104 lyx_view_.connectBufferView(*buffer_view);
108 BufferView & WorkArea::bufferView()
110 return *buffer_view_;
114 BufferView const & WorkArea::bufferView() const
116 return *buffer_view_;
120 void WorkArea::stopBlinkingCursor()
122 cursor_timeout_.stop();
127 void WorkArea::startBlinkingCursor()
130 cursor_timeout_.restart();
134 void WorkArea::redraw()
136 if (!buffer_view_ || !buffer_view_->buffer()) {
138 // The argument here are meaningless.
143 // No need to do anything if this is the current view. The BufferView
144 // metrics are already up to date.
145 if (&lyx_view_ != theApp()->currentView())
146 // FIXME: it would be nice to optimize for the off-screen case.
147 buffer_view_->updateMetrics(false);
151 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
154 if (lyxerr.debugging(Debug::WORKAREA)) {
155 lyxerr[Debug::WORKAREA] << "WorkArea::redraw screen" << endl;
157 int const ymin = std::max(vi.y1, 0);
158 int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
160 expose(0, ymin, width(), ymax - ymin);
162 //lyxerr[Debug::WORKAREA]
163 //<< " ymin = " << ymin << " width() = " << width()
164 // << " ymax-ymin = " << ymax-ymin << std::endl;
166 if (lyxerr.debugging(Debug::WORKAREA))
167 buffer_view_->coordCache().dump();
171 void WorkArea::processKeySym(LyXKeySymPtr key,
172 key_modifier::state state)
174 // In order to avoid bad surprise in the middle of an operation, we better stop
175 // the blinking cursor.
176 stopBlinkingCursor();
178 theLyXFunc().setLyXView(&lyx_view_);
179 theLyXFunc().processKeySym(key, state);
181 /* When we move around, or type, it's nice to be able to see
182 * the cursor immediately after the keypress.
184 startBlinkingCursor();
188 void WorkArea::dispatch(FuncRequest const & cmd0)
191 if (cmd0.action == LFUN_FILE_OPEN) {
192 lyx_view_.dispatch(cmd0);
196 theLyXFunc().setLyXView(&lyx_view_);
198 std::pair<bool, bool> needRedraw = buffer_view_->workAreaDispatch(cmd0);
200 // Skip these when selecting
201 if (cmd0.action != LFUN_MOUSE_MOTION) {
202 lyx_view_.updateLayoutChoice();
203 lyx_view_.updateMenubar();
204 lyx_view_.updateToolbars();
208 // GUI tweaks except with mouse motion with no button pressed.
209 if (!(cmd0.action == LFUN_MOUSE_MOTION
210 && cmd0.button() == mouse_button::none)) {
211 // Slight hack: this is only called currently when we
212 // clicked somewhere, so we force through the display
213 // of the new status here.
214 lyx_view_.clearMessage();
216 // Show the cursor immediately after any operation.
221 if (needRedraw.first)
226 void WorkArea::resizeBufferView()
228 lyx_view_.busy(true);
229 lyx_view_.message(_("Formatting document..."));
230 buffer_view_->workAreaResize(width(), height());
231 lyx_view_.updateLayoutChoice();
233 lyx_view_.busy(false);
234 lyx_view_.clearMessage();
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::scrollBufferView(int position)
249 buffer_view_->scrollDocView(position);
252 if (lyxrc.cursor_follows_scrollbar) {
253 buffer_view_->setCursorFromScrollbar();
254 lyx_view_.updateLayoutChoice();
260 void WorkArea::showCursor()
265 if (!buffer_view_->buffer())
268 CursorShape shape = BAR_SHAPE;
270 LyXText const & text = *buffer_view_->getLyXText();
271 LyXFont const & realfont = text.real_current_font;
272 BufferParams const & bp = buffer_view_->buffer()->params();
273 bool const samelang = realfont.language() == bp.language;
274 bool const isrtl = realfont.isVisibleRightToLeft();
276 if (!samelang || isrtl != bp.language->rightToLeft()) {
279 shape = REVERSED_L_SHAPE;
282 // The ERT language hack needs fixing up
283 if (realfont.language() == latex_language)
286 LyXFont const font = buffer_view_->cursor().getFont();
287 FontMetrics const & fm = theFontMetrics(font);
288 int const asc = fm.maxAscent();
289 int const des = fm.maxDescent();
293 buffer_view_->cursor().getPos(x, y);
296 // if it doesn't touch the screen, don't try to show it
297 if (y + h < 0 || y >= height())
300 cursor_visible_ = true;
301 showCursor(x, y, h, shape);
305 void WorkArea::hideCursor()
307 if (!cursor_visible_)
310 cursor_visible_ = false;
315 void WorkArea::toggleCursor()
317 if (buffer_view_->buffer()) {
324 // Use this opportunity to deal with any child processes that
325 // have finished but are waiting to communicate this fact
326 // to the rest of LyX.
327 ForkedcallsController & fcc = ForkedcallsController::get();
328 fcc.handleCompletedProcesses();
331 cursor_timeout_.restart();
335 void WorkArea::displayMessage(lyx::docstring const & message)
337 lyx_view_.message(message);
340 } // namespace frontend