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
18 #include "font_metrics.h"
19 #include "funcrequest.h"
24 #include "BufferView.h"
26 #include "bufferparams.h"
27 #include "coordcache.h"
37 #include "metricsinfo.h"
38 #include "paragraph.h"
39 #include "rowpainter.h"
42 #include "graphics/GraphicsImage.h"
43 #include "graphics/GraphicsLoader.h"
46 #include "support/filetools.h" // LibFileSearch
47 #include "support/forkedcontr.h"
49 #include <boost/utility.hpp>
50 #include <boost/bind.hpp>
51 #include <boost/current_function.hpp>
52 #include <boost/signals/trackable.hpp>
54 using lyx::support::libFileSearch;
55 using lyx::support::ForkedcallsController;
66 // FIXME: The SplashScreen should be transfered to the
67 // LyXView and create a WorkArea only when a new buffer exists. This
68 // will allow to call WorkArea::redraw() in the constructor.
69 class SplashScreen : boost::noncopyable, boost::signals::trackable {
71 /// This is a singleton class. Get the instance.
72 static SplashScreen const & get();
74 lyx::graphics::Image const * image() const { return loader_.image(); }
76 string const & text() const { return text_; }
78 LyXFont const & font() const { return font_; }
80 void connect(lyx::graphics::Loader::slot_type const & slot) const {
81 loader_.connect(slot);
84 void startLoading() const {
85 if (loader_.status() == lyx::graphics::WaitingToLoad)
86 loader_.startLoading();
90 /** Make the c-tor private so we can control how many objects
96 lyx::graphics::Loader loader_;
97 /// The text to be written on top of the pixmap
104 SplashScreen const & SplashScreen::get()
106 static SplashScreen singleton;
111 SplashScreen::SplashScreen()
112 : text_(lyx_version ? lyx_version : "unknown")
114 if (!lyxrc.show_banner)
117 string const file = libFileSearch("images", "banner", "ppm");
121 // The font used to display the version info
122 font_.setFamily(LyXFont::SANS_FAMILY);
123 font_.setSeries(LyXFont::BOLD_SERIES);
124 font_.setSize(LyXFont::SIZE_NORMAL);
125 font_.setColor(LColor::yellow);
127 // Load up the graphics file
133 // All the below connection objects are needed because of a bug in some
134 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
135 // to these connections we avoid a segfault upon startup, and also at exit.
138 boost::signals::connection timecon;
142 WorkArea::WorkArea(LyXView & lyx_view)
143 : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
144 cursor_visible_(false), cursor_timeout_(400)
146 // Start loading the pixmap as soon as possible
147 if (lyxrc.show_banner) {
148 SplashScreen const & splash = SplashScreen::get();
149 splash.connect(boost::bind(&WorkArea::checkAndGreyOut, this));
150 splash.startLoading();
154 timecon = cursor_timeout_.timeout
155 .connect(boost::bind(&WorkArea::toggleCursor, this));
157 cursor_timeout_.start();
161 void WorkArea::setBufferView(BufferView * buffer_view)
164 buffer_view_ = buffer_view;
169 BufferView & WorkArea::bufferView()
171 return *buffer_view_;
175 BufferView const & WorkArea::bufferView() const
177 return *buffer_view_;
181 void WorkArea::checkAndGreyOut()
188 void WorkArea::redraw()
193 if (!buffer_view_->buffer()) {
198 buffer_view_->updateMetrics(false);
202 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
204 getPainter().start();
205 paintText(*buffer_view_, vi, getPainter());
206 lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
207 int const ymin = std::max(vi.y1, 0);
209 ( vi.p2 < vi.size - 1 ? vi.y2 : height() );
210 expose(0, ymin, width(), ymax - ymin);
214 << " ymin = " << ymin << " width() = " << width()
215 << " ymax-ymin = " << ymax-ymin << std::endl;
219 void WorkArea::processKeySym(LyXKeySymPtr key,
220 key_modifier::state state)
223 lyx_view_.getLyXFunc().processKeySym(key, state);
225 /* This is perhaps a bit of a hack. When we move
226 * around, or type, it's nice to be able to see
227 * the cursor immediately after the keypress. So
228 * we reset the toggle timeout and force the visibility
229 * of the cursor. Note we cannot do this inside
230 * dispatch() itself, because that's called recursively.
232 // if (buffer_view_->available())
235 // uneeded "redraw()" call commented out for now.
236 // When/if the call to LyXView::redrawWorkArea() in "lyxfunc.C:1610"
237 // is not needed anymore, this line should be uncommented out
242 void WorkArea::dispatch(FuncRequest const & cmd0)
245 if (cmd0.action == LFUN_FILE_OPEN) {
246 lyx_view_.dispatch(cmd0);
250 buffer_view_->workAreaDispatch(cmd0);
252 // Skip these when selecting
253 if (cmd0.action != LFUN_MOUSE_MOTION) {
254 lyx_view_.updateLayoutChoice();
255 lyx_view_.updateToolbars();
258 // Slight hack: this is only called currently when we
259 // clicked somewhere, so we force through the display
260 // of the new status here.
261 lyx_view_.clearMessage();
267 void WorkArea::resizeBufferView()
269 lyx_view_.busy(true);
270 lyx_view_.message(_("Formatting document..."));
271 buffer_view_->workAreaResize(width(), height());
272 lyx_view_.updateLayoutChoice();
274 lyx_view_.busy(false);
275 lyx_view_.clearMessage();
279 void WorkArea::updateScrollbar()
281 buffer_view_->updateScrollbar();
282 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
283 setScrollbarParams(scroll_.height, scroll_.position,
284 scroll_.lineScrollHeight);
288 void WorkArea::scrollBufferView(int position)
290 buffer_view_->scrollDocView(position);
293 if (lyxrc.cursor_follows_scrollbar) {
294 buffer_view_->setCursorFromScrollbar();
295 lyx_view_.updateLayoutChoice();
301 void WorkArea::greyOut()
304 getPainter().start();
306 getPainter().fillRectangle(0, 0,
311 // Add a splash screen to the centre of the work area
312 SplashScreen const & splash = SplashScreen::get();
313 lyx::graphics::Image const * const splash_image = splash.image();
315 int const w = splash_image->getWidth();
316 int const h = splash_image->getHeight();
318 int x = (width() - w) / 2;
319 int y = (height() - h) / 2;
321 getPainter().image(x, y, w, h, *splash_image);
326 string stext = splash.text();
327 docstring dstext(stext.begin(), stext.end());
328 getPainter().text(x, y, dstext, splash.font());
330 expose(0, 0, width(), height());
335 void WorkArea::showCursor()
340 if (!buffer_view_->available())
343 CursorShape shape = BAR_SHAPE;
345 LyXText const & text = *buffer_view_->getLyXText();
346 LyXFont const & realfont = text.real_current_font;
347 BufferParams const & bp = buffer_view_->buffer()->params();
348 bool const samelang = realfont.language() == bp.language;
349 bool const isrtl = realfont.isVisibleRightToLeft();
351 if (!samelang || isrtl != bp.language->rightToLeft()) {
354 shape = REVERSED_L_SHAPE;
357 // The ERT language hack needs fixing up
358 if (realfont.language() == latex_language)
361 LyXFont const font = buffer_view_->cursor().getFont();
362 int const asc = font_metrics::maxAscent(font);
363 int const des = font_metrics::maxDescent(font);
367 buffer_view_->cursor().getPos(x, y);
370 // if it doesn't touch the screen, don't try to show it
371 if (y + h < 0 || y >= height())
374 cursor_visible_ = true;
375 showCursor(x, y, h, shape);
379 void WorkArea::hideCursor()
381 if (!cursor_visible_)
384 cursor_visible_ = false;
389 void WorkArea::toggleCursor()
391 if (buffer_view_->buffer()) {
398 // Use this opportunity to deal with any child processes that
399 // have finished but are waiting to communicate this fact
400 // to the rest of LyX.
401 ForkedcallsController & fcc = ForkedcallsController::get();
402 fcc.handleCompletedProcesses();
405 cursor_timeout_.restart();
408 } // namespace frontend