3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
10 * Splash screen code added by Angus Leeming
20 #include "BufferView.h"
24 #include "font_metrics.h"
27 #include "rowpainter.h"
29 // Splash screen-specific stuff
33 #include "graphics/GraphicsLoader.h"
34 #include "graphics/GraphicsImage.h"
36 #include "support/filetools.h" // LibFileSearch
38 #include <boost/utility.hpp>
39 #include <boost/bind.hpp>
40 #include <boost/signals/trackable.hpp>
48 class SplashScreen : boost::noncopyable, boost::signals::trackable {
50 /// This is a singleton class. Get the instance.
51 static SplashScreen const & get();
53 grfx::Image const * image() const { return loader_.image(); }
55 string const & text() const { return text_; }
57 LyXFont const & font() const { return font_; }
59 void connect(grfx::Loader::slot_type const & slot) const {
60 loader_.connect(slot);
63 void startLoading() const {
64 if (loader_.status() == grfx::WaitingToLoad)
65 loader_.startLoading();
69 /** Make the c-tor private so we can control how many objects
76 /// The text to be written on top of the pixmap
83 SplashScreen const & SplashScreen::get()
85 static SplashScreen singleton;
90 SplashScreen::SplashScreen()
91 : text_(lyx_version ? lyx_version : "unknown")
93 if (!lyxrc.show_banner)
96 string const file = LibFileSearch("images", "banner", "ppm");
100 // The font used to display the version info
101 font_.setFamily(LyXFont::SANS_FAMILY);
102 font_.setSeries(LyXFont::BOLD_SERIES);
103 font_.setSize(LyXFont::SIZE_NORMAL);
104 font_.setColor(LColor::yellow);
106 // Load up the graphics file
113 LyXScreen::LyXScreen()
114 : cursor_visible_(false), greyed_out_(true)
116 // Start loading the pixmap as soon as possible
117 if (lyxrc.show_banner) {
118 SplashScreen const & splash = SplashScreen::get();
119 splash.connect(boost::bind(&LyXScreen::greyOut, this));
120 splash.startLoading();
125 LyXScreen::~LyXScreen()
129 // FIXME: GUII these cursor methods need to decide
130 // whether the workarea is focused or not
132 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
137 workarea().getPainter().start();
139 Cursor_Shape shape = BAR_SHAPE;
140 BufferParams const & bp(bv->buffer()->params);
141 LyXFont const & realfont(text->real_current_font);
143 if (realfont.language() != bp.language
144 || realfont.isVisibleRightToLeft()
145 != bp.language->RightToLeft()) {
146 shape = (realfont.isVisibleRightToLeft())
147 ? REVERSED_L_SHAPE : L_SHAPE;
150 showManualCursor(text, text->cursor.x(), text->cursor.y(),
151 font_metrics::maxAscent(realfont),
152 font_metrics::maxDescent(realfont),
155 workarea().getPainter().end();
159 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
160 int /*x*/, int y, int asc, int desc)
162 int const vheight = workarea().workHeight();
163 int newtop = text->top_y();
165 if (y + desc - text->top_y() >= vheight)
166 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
167 else if (y - asc < text->top_y()
168 && text->top_y() > 0) {
169 newtop = y - vheight / 4;
172 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
174 if (newtop != text->top_y()) {
175 draw(text, bv, newtop);
184 void LyXScreen::cursorToggle(BufferView * bv) const
193 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
195 int const vheight = workarea().workHeight();
198 RowList::iterator row = cursor.row();
200 #warning SUPER HACK DISABLED (Lgb)
202 // Is this a hack? Yes, probably... (Lgb)
204 return max(newtop, 0);
206 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
207 if (row->height() < vheight
208 && row->height() > vheight / 4) {
211 - row->baseline() - vheight;
215 - vheight / 2; /* the scroll region must be so big!! */
218 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
219 top_y && top_y > 0) {
220 if (row->height() < vheight
221 && row->height() > vheight / 4) {
222 newtop = cursor.y() - row->baseline();
225 newtop = cursor.y() - vheight / 2;
226 newtop = min(newtop, top_y);
230 newtop = max(newtop, 0);
236 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
238 // Is a change necessary?
239 int const newtop = topCursorVisible(text->cursor, text->top_y());
240 bool const result = (newtop != text->top_y());
242 draw(text, bv, newtop);
249 void LyXScreen::update(BufferView & bv, int yo, int xo)
251 int const vwidth = workarea().workWidth();
252 int const vheight = workarea().workHeight();
253 LyXText * text = bv.text;
255 workarea().getPainter().start();
257 switch (text->refreshStatus()) {
258 case LyXText::REFRESH_AREA:
260 text->updateRowPositions();
261 int const y = max(int(text->refresh_y - text->top_y()), 0);
262 drawFromTo(text, &bv, y, vheight, yo, xo);
263 expose(0, y, vwidth, vheight - y);
266 case LyXText::REFRESH_ROW:
268 text->updateRowPositions();
269 // ok I will update the current cursor row
270 drawOneRow(text, &bv, text->refresh_row, text->refresh_y,
272 // this because if we had a major update the refresh_row could
273 // have been set to 0!
274 if (text->refresh_row != text->rows().end()) {
275 expose(0, text->refresh_y - text->top_y() + yo,
276 vwidth, text->refresh_row->height());
280 case LyXText::REFRESH_NONE:
281 // Nothing needs done
285 workarea().getPainter().end();
289 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
293 // only if there is a selection
294 if (!text->selection.set()) return;
296 int const bottom = min(
297 max(static_cast<int>(text->selection.end.y()
298 - text->selection.end.row()->baseline()
299 + text->selection.end.row()->height()),
301 static_cast<int>(text->top_y() + workarea().workHeight()));
303 max(static_cast<int>(text->selection.start.y() -
304 text->selection.start.row()->baseline()),
306 static_cast<int>(text->top_y() + workarea().workHeight()));
309 text->selection.set(false);
311 workarea().getPainter().start();
313 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(),
315 expose(0, top - text->top_y(),
316 workarea().workWidth(),
317 bottom - text->top_y() - (top - text->top_y()));
319 workarea().getPainter().end();
323 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
326 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
327 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
330 int const top_tmp = text->toggle_cursor.y()
331 - text->toggle_cursor.row()->baseline();
332 int const bottom_tmp = text->toggle_end_cursor.y()
333 - text->toggle_end_cursor.row()->baseline()
334 + text->toggle_end_cursor.row()->height();
336 int const offset = yo < 0 ? yo : 0;
337 int const bottom = min(max(bottom_tmp, text->top_y()),
338 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
339 int const top = min(max(top_tmp, text->top_y()),
340 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
342 workarea().getPainter().start();
344 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(), yo, xo);
345 expose(0, top - text->top_y(), workarea().workWidth(),
346 bottom - text->top_y() - (top - text->top_y()));
348 workarea().getPainter().end();
352 void LyXScreen::redraw(LyXText * text, BufferView * bv)
361 workarea().getPainter().start();
363 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0);
364 expose(0, 0, workarea().workWidth(), workarea().workHeight());
366 workarea().getPainter().end();
368 if (cursor_visible_) {
369 cursor_visible_ = false;
375 void LyXScreen::greyOut()
380 workarea().getPainter().start();
382 workarea().getPainter().fillRectangle(0, 0,
383 workarea().workWidth(),
384 workarea().workHeight(),
387 // Add a splash screen to the centre of the work area
388 SplashScreen const & splash = SplashScreen::get();
389 grfx::Image const * const splash_image = splash.image();
391 int const w = splash_image->getWidth();
392 int const h = splash_image->getHeight();
394 int x = (workarea().workWidth() - w) / 2;
395 int y = (workarea().workHeight() - h) / 2;
397 workarea().getPainter().image(x, y, w, h, *splash_image);
399 string const & splash_text = splash.text();
400 LyXFont const & splash_font = splash.font();
405 workarea().getPainter().text(x, y, splash_text, splash_font);
407 expose(0, 0, workarea().workWidth(), workarea().workHeight());
408 workarea().getPainter().end();
412 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
413 int y1, int y2, int yo, int xo)
415 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
417 int const topy = text->top_y();
418 int y_text = topy + y1;
419 RowList::iterator rit = text->getRowNearY(y_text);
420 int y = y_text - topy;
421 // y1 is now the real beginning of row on the screen
423 RowList::iterator const rend = text->rows().end();
424 while (rit != rend && y < y2) {
425 RowPainter rp(*bv, *text, rit);
426 rp.paint(y + yo, xo, y + topy);
431 // maybe we have to clear the screen at the bottom
432 if ((y < y2) && !text->isInInset()) {
433 workarea().getPainter().fillRectangle(0, y,
434 workarea().workWidth(), y2 - y,
440 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv,
441 RowList::iterator row,
442 int y_text, int yo, int xo)
444 int const y = y_text - text->top_y() + yo;
446 if (y + row->height() <= 0)
449 if (y - row->height() > workarea().workHeight())
452 RowPainter rp(*bv, *text, row);
453 rp.paint(y, xo, y + text->top_y());