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
16 #include "font_metrics.h"
21 #include "BufferView.h"
23 #include "bufferparams.h"
31 #include "metricsinfo.h"
32 #include "paragraph.h"
33 #include "rowpainter.h"
36 #include "insets/updatableinset.h"
38 #include "graphics/GraphicsImage.h"
39 #include "graphics/GraphicsLoader.h"
41 #include "support/filetools.h" // LibFileSearch
43 #include <boost/utility.hpp>
44 #include <boost/bind.hpp>
45 #include <boost/signals/trackable.hpp>
47 using lyx::support::LibFileSearch;
57 class SplashScreen : boost::noncopyable, boost::signals::trackable {
59 /// This is a singleton class. Get the instance.
60 static SplashScreen const & get();
62 lyx::graphics::Image const * image() const { return loader_.image(); }
64 string const & text() const { return text_; }
66 LyXFont const & font() const { return font_; }
68 void connect(lyx::graphics::Loader::slot_type const & slot) const {
69 loader_.connect(slot);
72 void startLoading() const {
73 if (loader_.status() == lyx::graphics::WaitingToLoad)
74 loader_.startLoading();
78 /** Make the c-tor private so we can control how many objects
84 lyx::graphics::Loader loader_;
85 /// The text to be written on top of the pixmap
92 SplashScreen const & SplashScreen::get()
94 static SplashScreen singleton;
99 SplashScreen::SplashScreen()
100 : text_(lyx_version ? lyx_version : "unknown")
102 if (!lyxrc.show_banner)
105 string const file = LibFileSearch("images", "banner", "ppm");
109 // The font used to display the version info
110 font_.setFamily(LyXFont::SANS_FAMILY);
111 font_.setSeries(LyXFont::BOLD_SERIES);
112 font_.setSize(LyXFont::SIZE_NORMAL);
113 font_.setColor(LColor::yellow);
115 // Load up the graphics file
122 LyXScreen::LyXScreen()
123 : cursor_visible_(false), greyed_out_(true)
125 // Start loading the pixmap as soon as possible
126 if (lyxrc.show_banner) {
127 SplashScreen const & splash = SplashScreen::get();
128 splash.connect(boost::bind(&LyXScreen::greyOut, this));
129 splash.startLoading();
134 LyXScreen::~LyXScreen()
139 void LyXScreen::showCursor(BufferView & bv)
141 // this is needed to make sure we copy back the right
142 // pixmap on the hide for the Qt frontend
143 lyx_gui::sync_events();
151 Cursor_Shape shape = BAR_SHAPE;
153 LyXText const & text = *bv.getLyXText();
154 LyXFont const & realfont = text.real_current_font;
155 BufferParams const & bp = bv.buffer()->params();
156 bool const samelang = realfont.language() == bp.language;
157 bool const isrtl = realfont.isVisibleRightToLeft();
159 if (!samelang || isrtl != bp.language->RightToLeft()) {
162 shape = REVERSED_L_SHAPE;
165 // The ERT language hack needs fixing up
166 if (realfont.language() == latex_language)
169 int ascent = font_metrics::maxAscent(realfont);
170 int descent = font_metrics::maxDescent(realfont);
171 int h = ascent + descent;
174 int const top_y = bv.top_y();
176 if (bv.theLockingInset()) {
177 // Would be nice to clean this up to make some understandable sense...
178 UpdatableInset * inset = bv.theLockingInset();
179 inset->getCursor(bv, x, y);
181 // Non-obvious. The reason we have to have these
182 // extra checks is that the ->getCursor() calls rely
183 // on the inset's own knowledge of its screen position.
184 // If we scroll up or down in a big enough increment, the
185 // inset->draw() is not called: this doesn't update
186 // inset.top_baseline, so getCursor() returns an old value.
189 inset->getCursorPos(&bv, bx, by);
190 by += inset->insetInInsetY() + bv.text->cursor.y();
193 if (by > top_y + workarea().workHeight())
196 x = bv.text->cursor.x();
197 y = bv.text->cursor.y();
203 // if it doesn't fit entirely on the screen, don't try to show it
204 if (y < 0 || y + h > workarea().workHeight())
207 cursor_visible_ = true;
208 showCursor(x, y, h, shape);
212 void LyXScreen::hideCursor()
214 if (!cursor_visible_)
217 cursor_visible_ = false;
222 void LyXScreen::toggleCursor(BufferView & bv)
231 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText *,
232 int /*x*/, int y, int asc, int desc)
234 int const vheight = workarea().workHeight();
235 int const topy = bv->top_y();
239 if (y + desc - topy >= vheight)
240 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
241 else if (y - asc < topy && topy > 0)
242 newtop = y - vheight / 4;
244 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
254 unsigned int LyXScreen::topCursorVisible(LyXText * text)
256 LyXCursor const & cursor = text->cursor;
257 int top_y = text->bv()->top_y();
259 unsigned int const vheight = workarea().workHeight();
261 Row & row = *text->cursorPar()->getRow(cursor.pos());
263 if (int(cursor.y() - row.baseline() + row.height() - top_y) >= vheight) {
264 if (row.height() < vheight
265 && row.height() > vheight / 4) {
268 - row.baseline() - vheight;
270 // scroll down, the scroll region must be so big!!
271 newtop = cursor.y() - vheight / 2;
274 } else if (int(cursor.y() - row.baseline()) < top_y && top_y > 0) {
275 if (row.height() < vheight && row.height() > vheight / 4) {
276 newtop = cursor.y() - row.baseline();
279 newtop = cursor.y() - vheight / 2;
280 newtop = min(newtop, top_y);
284 return max(newtop, 0);
288 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
290 // Is a change necessary?
291 int const newtop = topCursorVisible(text);
292 bool const result = (newtop != bv->top_y());
298 void LyXScreen::redraw(BufferView & bv)
300 greyed_out_ = !bv.text;
307 workarea().getPainter().start();
311 int const y = paintText(bv);
313 // maybe we have to clear the screen at the bottom
314 int const y2 = workarea().workHeight();
315 if (y < y2 && !bv.text->isInInset()) {
316 workarea().getPainter().fillRectangle(0, y,
317 workarea().workWidth(), y2 - y,
321 lyxerr << "Redraw screen" << endl;
323 expose(0, 0, workarea().workWidth(), workarea().workHeight());
325 workarea().getPainter().end();
329 void LyXScreen::greyOut()
334 workarea().getPainter().start();
336 workarea().getPainter().fillRectangle(0, 0,
337 workarea().workWidth(),
338 workarea().workHeight(),
341 // Add a splash screen to the centre of the work area
342 SplashScreen const & splash = SplashScreen::get();
343 lyx::graphics::Image const * const splash_image = splash.image();
345 int const w = splash_image->getWidth();
346 int const h = splash_image->getHeight();
348 int x = (workarea().workWidth() - w) / 2;
349 int y = (workarea().workHeight() - h) / 2;
351 workarea().getPainter().image(x, y, w, h, *splash_image);
356 workarea().getPainter().text(x, y, splash.text(), splash.font());
358 expose(0, 0, workarea().workWidth(), workarea().workHeight());
359 workarea().getPainter().end();