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"
28 // Splash screen-specific stuff
32 #include "graphics/GraphicsLoader.h"
33 #include "graphics/GraphicsImage.h"
35 #include "support/filetools.h" // LibFileSearch
37 #include <boost/utility.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/signals/trackable.hpp>
47 class SplashScreen : boost::noncopyable, boost::signals::trackable {
49 /// This is a singleton class. Get the instance.
50 static SplashScreen const & get();
52 grfx::Image const * image() const { return loader_.image(); }
54 string const & text() const { return text_; }
56 LyXFont const & font() const { return font_; }
58 void connect(grfx::Loader::slot_type const & slot) const {
59 loader_.connect(slot);
62 void startLoading() const {
63 if (loader_.status() == grfx::WaitingToLoad)
64 loader_.startLoading();
68 /** Make the c-tor private so we can control how many objects
75 /// The text to be written on top of the pixmap
82 SplashScreen const & SplashScreen::get()
84 static SplashScreen singleton;
89 SplashScreen::SplashScreen()
90 : text_(lyx_version ? lyx_version : "unknown")
92 if (!lyxrc.show_banner)
95 string const file = LibFileSearch("images", "banner", "ppm");
99 // The font used to display the version info
100 font_.setFamily(LyXFont::SANS_FAMILY);
101 font_.setSeries(LyXFont::BOLD_SERIES);
102 font_.setSize(LyXFont::SIZE_NORMAL);
103 font_.setColor(LColor::yellow);
105 // Load up the graphics file
112 LyXScreen::LyXScreen()
113 : cursor_visible_(false), force_clear_(true), greyed_out_(false)
115 // Start loading the pixmap as soon as possible
116 if (lyxrc.show_banner) {
117 SplashScreen const & splash = SplashScreen::get();
118 splash.connect(boost::bind(&LyXScreen::greyOut, this));
119 splash.startLoading();
124 LyXScreen::~LyXScreen()
128 // FIXME: GUII these cursor methods need to decide
129 // whether the workarea is focused or not
131 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
136 workarea().getPainter().start();
138 Cursor_Shape shape = BAR_SHAPE;
139 BufferParams const & bp(bv->buffer()->params);
140 LyXFont const & realfont(text->real_current_font);
142 if (realfont.language() != bp.language
143 || realfont.isVisibleRightToLeft()
144 != bp.language->RightToLeft()) {
145 shape = (realfont.isVisibleRightToLeft())
146 ? REVERSED_L_SHAPE : L_SHAPE;
149 showManualCursor(text, text->cursor.x(), text->cursor.y(),
150 font_metrics::maxAscent(realfont),
151 font_metrics::maxDescent(realfont),
154 workarea().getPainter().end();
158 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
159 int /*x*/, int y, int asc, int desc)
161 int const vheight = workarea().workHeight();
162 int newtop = text->first_y;
164 if (y + desc - text->first_y >= vheight)
165 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
166 else if (y - asc < text->first_y
167 && text->first_y > 0) {
168 newtop = y - vheight / 4;
171 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
173 if (newtop != text->first_y) {
174 draw(text, bv, newtop);
175 text->first_y = newtop;
183 void LyXScreen::cursorToggle(BufferView * bv) const
192 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
194 int const vheight = workarea().workHeight();
197 Row * row = cursor.row();
199 // Is this a hack? Yes, probably... (Lgb)
201 return max(newtop, 0);
203 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
204 if (row->height() < vheight
205 && row->height() > vheight / 4) {
208 - row->baseline() - vheight;
212 - vheight / 2; /* the scroll region must be so big!! */
215 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
216 top_y && top_y > 0) {
217 if (row->height() < vheight
218 && row->height() > vheight / 4) {
219 newtop = cursor.y() - row->baseline();
222 newtop = cursor.y() - vheight / 2;
223 newtop = min(newtop, top_y);
227 newtop = max(newtop, 0);
233 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
235 // Is a change necessary?
236 int const newtop = topCursorVisible(text->cursor, text->first_y);
237 bool const result = (newtop != text->first_y);
239 draw(text, bv, newtop);
246 void LyXScreen::update(LyXText * text, BufferView * bv,
249 int const vwidth = workarea().workWidth();
250 int const vheight = workarea().workHeight();
252 workarea().getPainter().start();
254 switch (text->status()) {
255 case LyXText::NEED_MORE_REFRESH:
257 int const y = max(int(text->refresh_y - text->first_y), 0);
258 drawFromTo(text, bv, y, vheight, yo, xo);
260 // otherwise this is called ONLY from BufferView_pimpl(update)
261 // or we should see to set this flag accordingly
262 if (text != bv->text)
263 text->status(bv, LyXText::UNCHANGED);
264 expose(0, y, vwidth, vheight - y);
267 case LyXText::NEED_VERY_LITTLE_REFRESH:
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) {
275 // otherwise this is called ONLY from BufferView_pimpl(update)
276 // or we should see to set this flag accordingly
277 if (text != bv->text)
278 text->status(bv, LyXText::UNCHANGED);
279 expose(0, text->refresh_y - text->first_y + yo,
280 vwidth, text->refresh_row->height());
284 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
285 case LyXText::UNCHANGED:
286 // Nothing needs done
290 workarea().getPainter().end();
294 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
298 // only if there is a selection
299 if (!text->selection.set()) return;
301 int const bottom = min(
302 max(static_cast<int>(text->selection.end.y()
303 - text->selection.end.row()->baseline()
304 + text->selection.end.row()->height()),
306 static_cast<int>(text->first_y + workarea().workHeight()));
308 max(static_cast<int>(text->selection.start.y() -
309 text->selection.start.row()->baseline()),
311 static_cast<int>(text->first_y + workarea().workHeight()));
314 text->selection.set(false);
316 workarea().getPainter().start();
318 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
320 expose(0, top - text->first_y,
321 workarea().workWidth(),
322 bottom - text->first_y - (top - text->first_y));
324 workarea().getPainter().end();
328 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
331 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
332 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
335 int const top_tmp = text->toggle_cursor.y()
336 - text->toggle_cursor.row()->baseline();
337 int const bottom_tmp = text->toggle_end_cursor.y()
338 - text->toggle_end_cursor.row()->baseline()
339 + text->toggle_end_cursor.row()->height();
341 int const offset = yo < 0 ? yo : 0;
342 int const bottom = min(max(bottom_tmp, text->first_y),
343 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
344 int const top = min(max(top_tmp, text->first_y),
345 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
347 workarea().getPainter().start();
349 drawFromTo(text, bv, top - text->first_y,
350 bottom - text->first_y, yo,
352 expose(0, top - text->first_y, workarea().workWidth(),
353 bottom - text->first_y - (top - text->first_y));
355 workarea().getPainter().end();
359 void LyXScreen::redraw(LyXText * text, BufferView * bv)
363 workarea().getPainter().start();
370 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
371 expose(0, 0, workarea().workWidth(), workarea().workHeight());
373 workarea().getPainter().end();
375 if (cursor_visible_) {
376 cursor_visible_ = false;
382 void LyXScreen::greyOut()
387 workarea().getPainter().fillRectangle(0, 0,
388 workarea().workWidth(),
389 workarea().workHeight(),
392 // Add a splash screen to the centre of the work area
393 SplashScreen const & splash = SplashScreen::get();
394 grfx::Image const * const splash_image = splash.image();
396 int const w = splash_image->getWidth();
397 int const h = splash_image->getHeight();
399 int x = (workarea().workWidth() - w) / 2;
400 int y = (workarea().workHeight() - h) / 2;
402 workarea().getPainter().image(x, y, w, h, *splash_image);
404 string const & splash_text = splash.text();
405 LyXFont const & splash_font = splash.font();
410 workarea().getPainter().text(x, y, splash_text, splash_font);
412 expose(0, 0, workarea().workWidth(), workarea().workHeight());
413 workarea().getPainter().end();
417 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
418 int y1, int y2, int yo, int xo,
421 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
423 int y_text = text->first_y + y1;
425 // get the first needed row
426 Row * row = text->getRowNearY(y_text);
427 // y_text is now the real beginning of the row
429 int y = y_text - text->first_y;
430 // y1 is now the real beginning of row on the screen
432 while (row != 0 && y < y2) {
433 LyXText::text_status st = text->status();
434 text->getVisibleRow(bv, y + yo,
435 xo, row, y + text->first_y);
436 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
437 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
438 text->fullRebreak(bv);
439 st = LyXText::NEED_MORE_REFRESH;
440 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
441 text->status(bv, st);
442 text->getVisibleRow(bv, y + yo,
443 xo, row, y + text->first_y);
448 force_clear_ = false;
450 // maybe we have to clear the screen at the bottom
451 if ((y < y2) && text->isTopLevel()) {
452 workarea().getPainter().fillRectangle(0, y,
453 workarea().workWidth(), y2 - y,
459 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
460 int y_text, int yo, int xo)
462 int const y = y_text - text->first_y + yo;
464 if (((y + row->height()) > 0) &&
465 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
466 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
468 force_clear_ = false;