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"
28 #include "insets/updatableinset.h"
29 #include "mathed/formulabase.h"
32 // Splash screen-specific stuff
36 #include "graphics/GraphicsLoader.h"
37 #include "graphics/GraphicsImage.h"
39 #include "support/filetools.h" // LibFileSearch
41 #include <boost/utility.hpp>
42 #include <boost/bind.hpp>
43 #include <boost/signals/trackable.hpp>
51 class SplashScreen : boost::noncopyable, boost::signals::trackable {
53 /// This is a singleton class. Get the instance.
54 static SplashScreen const & get();
56 grfx::Image const * image() const { return loader_.image(); }
58 string const & text() const { return text_; }
60 LyXFont const & font() const { return font_; }
62 void connect(grfx::Loader::slot_type const & slot) const {
63 loader_.connect(slot);
66 void startLoading() const {
67 if (loader_.status() == grfx::WaitingToLoad)
68 loader_.startLoading();
72 /** Make the c-tor private so we can control how many objects
79 /// The text to be written on top of the pixmap
86 SplashScreen const & SplashScreen::get()
88 static SplashScreen singleton;
93 SplashScreen::SplashScreen()
94 : text_(lyx_version ? lyx_version : "unknown")
96 if (!lyxrc.show_banner)
99 string const file = LibFileSearch("images", "banner", "ppm");
103 // The font used to display the version info
104 font_.setFamily(LyXFont::SANS_FAMILY);
105 font_.setSeries(LyXFont::BOLD_SERIES);
106 font_.setSize(LyXFont::SIZE_NORMAL);
107 font_.setColor(LColor::yellow);
109 // Load up the graphics file
116 LyXScreen::LyXScreen()
117 : cursor_visible_(false), greyed_out_(true)
119 // Start loading the pixmap as soon as possible
120 if (lyxrc.show_banner) {
121 SplashScreen const & splash = SplashScreen::get();
122 splash.connect(boost::bind(&LyXScreen::greyOut, this));
123 splash.startLoading();
128 LyXScreen::~LyXScreen()
133 void LyXScreen::showCursor(BufferView & bv)
135 // this is needed to make sure we copy back the right
136 // pixmap on the hide for the Qt frontend
137 lyx_gui::sync_events();
145 Cursor_Shape shape = BAR_SHAPE;
147 LyXText const & text = *bv.getLyXText();
148 LyXFont const & realfont(text.real_current_font);
149 BufferParams const & bp(bv.buffer()->params);
150 bool const samelang = realfont.language() == bp.language;
151 bool const isrtl = realfont.isVisibleRightToLeft();
153 if (!samelang || isrtl != bp.language->RightToLeft()) {
156 shape = REVERSED_L_SHAPE;
159 int ascent = font_metrics::maxAscent(realfont);
160 int descent = font_metrics::maxDescent(realfont);
161 int h = ascent + descent;
164 int const top_y = bv.text->top_y();
166 if (bv.theLockingInset()) {
167 // Would be nice to clean this up to make some understandable sense...
168 UpdatableInset * inset = bv.theLockingInset();
169 inset->getCursor(bv, x, y);
171 // Non-obvious. The reason we have to have these
172 // extra checks is that the ->getCursor() calls rely
173 // on the inset's own knowledge of its screen position.
174 // If we scroll up or down in a big enough increment, the
175 // inset->draw() is not called: this doesn't update
176 // inset.top_baseline, so getCursor() returns an old value.
179 inset->getCursorPos(&bv, bx, by);
180 by += inset->insetInInsetY() + bv.text->cursor.iy();
183 if (by > top_y + workarea().workHeight())
186 x = bv.text->cursor.x();
187 y = bv.text->cursor.y();
193 // if it doesn't fit entirely on the screen, don't try to show it
194 if (y < 0 || y + h > workarea().workHeight())
197 cursor_visible_ = true;
198 showCursor(x, y, h, shape);
202 void LyXScreen::hideCursor()
204 if (!cursor_visible_)
207 cursor_visible_ = false;
212 void LyXScreen::toggleCursor(BufferView & bv)
221 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
222 int /*x*/, int y, int asc, int desc)
224 int const vheight = workarea().workHeight();
225 int newtop = text->top_y();
227 if (y + desc - text->top_y() >= vheight)
228 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
229 else if (y - asc < text->top_y()
230 && text->top_y() > 0) {
231 newtop = y - vheight / 4;
234 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
236 if (newtop != text->top_y()) {
237 draw(text, bv, newtop);
246 unsigned int LyXScreen::topCursorVisible(LyXText * text)
248 LyXCursor const & cursor = text->cursor;
249 int top_y = text->top_y();
251 int const vheight = workarea().workHeight();
253 RowList::iterator row = text->cursorRow();
255 #warning SUPER HACK DISABLED (Lgb)
257 // Is this a hack? Yes, probably... (Lgb)
259 return max(newtop, 0);
261 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
262 if (row->height() < vheight
263 && row->height() > vheight / 4) {
266 - row->baseline() - vheight;
268 // scroll down, the scroll region must be so big!!
269 newtop = cursor.y() - vheight / 2;
272 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
273 top_y && top_y > 0) {
274 if (row->height() < vheight
275 && row->height() > vheight / 4) {
276 newtop = cursor.y() - row->baseline();
279 newtop = cursor.y() - vheight / 2;
280 newtop = min(newtop, top_y);
284 newtop = max(newtop, 0);
290 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
292 // Is a change necessary?
293 int const newtop = topCursorVisible(text);
294 bool const result = (newtop != text->top_y());
296 draw(text, bv, newtop);
303 void LyXScreen::update(BufferView & bv, int yo, int xo)
305 int const vwidth = workarea().workWidth();
306 int const vheight = workarea().workHeight();
307 LyXText * text = bv.text;
309 workarea().getPainter().start();
311 switch (text->refreshStatus()) {
312 case LyXText::REFRESH_AREA:
314 text->updateRowPositions();
315 int const y = max(int(text->refresh_y - text->top_y()), 0);
316 drawFromTo(text, &bv, y, vheight, yo, xo);
317 expose(0, y, vwidth, vheight - y);
320 case LyXText::REFRESH_ROW:
322 text->updateRowPositions();
323 // ok I will update the current cursor row
324 drawOneRow(text, &bv, text->refresh_row, text->refresh_y,
326 // this because if we had a major update the refresh_row could
327 // have been set to 0!
328 if (text->refresh_row != text->rows().end()) {
329 expose(0, text->refresh_y - text->top_y() + yo,
330 vwidth, text->refresh_row->height());
334 case LyXText::REFRESH_NONE:
335 // Nothing needs done
339 workarea().getPainter().end();
343 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
347 // only if there is a selection
348 if (!text->selection.set()) return;
350 int const bottom = min(
351 max(static_cast<int>(text->selection.end.y()
352 - text->getRow(text->selection.end)->baseline()
353 + text->getRow(text->selection.end)->height()),
355 static_cast<int>(text->top_y() + workarea().workHeight()));
357 max(static_cast<int>(text->selection.start.y() -
358 text->getRow(text->selection.start)->baseline()),
360 static_cast<int>(text->top_y() + workarea().workHeight()));
363 text->selection.set(false);
365 workarea().getPainter().start();
367 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(),
369 expose(0, top - text->top_y(),
370 workarea().workWidth(),
371 bottom - text->top_y() - (top - text->top_y()));
373 workarea().getPainter().end();
377 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
380 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
381 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
384 int const top_tmp = text->toggle_cursor.y()
385 - text->getRow(text->toggle_cursor)->baseline();
386 int const bottom_tmp = text->toggle_end_cursor.y()
387 - text->getRow(text->toggle_end_cursor)->baseline()
388 + text->getRow(text->toggle_end_cursor)->height();
390 int const offset = yo < 0 ? yo : 0;
391 int const bottom = min(max(bottom_tmp, text->top_y()),
392 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
393 int const top = min(max(top_tmp, text->top_y()),
394 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
396 workarea().getPainter().start();
398 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(), yo, xo);
399 expose(0, top - text->top_y(), workarea().workWidth(),
400 bottom - text->top_y() - (top - text->top_y()));
402 workarea().getPainter().end();
406 void LyXScreen::redraw(LyXText * text, BufferView * bv)
415 workarea().getPainter().start();
417 text->updateRowPositions();
418 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0);
419 expose(0, 0, workarea().workWidth(), workarea().workHeight());
421 workarea().getPainter().end();
425 void LyXScreen::greyOut()
430 workarea().getPainter().start();
432 workarea().getPainter().fillRectangle(0, 0,
433 workarea().workWidth(),
434 workarea().workHeight(),
437 // Add a splash screen to the centre of the work area
438 SplashScreen const & splash = SplashScreen::get();
439 grfx::Image const * const splash_image = splash.image();
441 int const w = splash_image->getWidth();
442 int const h = splash_image->getHeight();
444 int x = (workarea().workWidth() - w) / 2;
445 int y = (workarea().workHeight() - h) / 2;
447 workarea().getPainter().image(x, y, w, h, *splash_image);
449 string const & splash_text = splash.text();
450 LyXFont const & splash_font = splash.font();
455 workarea().getPainter().text(x, y, splash_text, splash_font);
457 expose(0, 0, workarea().workWidth(), workarea().workHeight());
458 workarea().getPainter().end();
462 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
463 int y1, int y2, int yo, int xo)
465 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
467 int const topy = text->top_y();
468 int y_text = topy + y1;
469 RowList::iterator rit = text->getRowNearY(y_text);
470 int y = y_text - topy;
471 // y1 is now the real beginning of row on the screen
475 RowList::iterator const rend = text->rows().end();
476 while (rit != rend && y < y2) {
477 RowPainter rp(*bv, *text, rit);
478 rp.paint(y + yo, xo, y + topy);
483 // maybe we have to clear the screen at the bottom
484 if ((y < y2) && !text->isInInset()) {
485 workarea().getPainter().fillRectangle(0, y,
486 workarea().workWidth(), y2 - y,
492 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv,
493 RowList::iterator row,
494 int y_text, int yo, int xo)
496 int const y = y_text - text->top_y() + yo;
498 if (y + row->height() <= 0)
501 if (y - row->height() > workarea().workHeight())
506 RowPainter rp(*bv, *text, row);
507 rp.paint(y, xo, y + text->top_y());