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>
45 using namespace lyx::support;
51 namespace grfx = lyx::graphics;
55 class SplashScreen : boost::noncopyable, boost::signals::trackable {
57 /// This is a singleton class. Get the instance.
58 static SplashScreen const & get();
60 grfx::Image const * image() const { return loader_.image(); }
62 string const & text() const { return text_; }
64 LyXFont const & font() const { return font_; }
66 void connect(grfx::Loader::slot_type const & slot) const {
67 loader_.connect(slot);
70 void startLoading() const {
71 if (loader_.status() == grfx::WaitingToLoad)
72 loader_.startLoading();
76 /** Make the c-tor private so we can control how many objects
83 /// The text to be written on top of the pixmap
90 SplashScreen const & SplashScreen::get()
92 static SplashScreen singleton;
97 SplashScreen::SplashScreen()
98 : text_(lyx_version ? lyx_version : "unknown")
100 if (!lyxrc.show_banner)
103 string const file = LibFileSearch("images", "banner", "ppm");
107 // The font used to display the version info
108 font_.setFamily(LyXFont::SANS_FAMILY);
109 font_.setSeries(LyXFont::BOLD_SERIES);
110 font_.setSize(LyXFont::SIZE_NORMAL);
111 font_.setColor(LColor::yellow);
113 // Load up the graphics file
120 LyXScreen::LyXScreen()
121 : cursor_visible_(false), greyed_out_(true)
123 // Start loading the pixmap as soon as possible
124 if (lyxrc.show_banner) {
125 SplashScreen const & splash = SplashScreen::get();
126 splash.connect(boost::bind(&LyXScreen::greyOut, this));
127 splash.startLoading();
132 LyXScreen::~LyXScreen()
137 void LyXScreen::showCursor(BufferView & bv)
139 // this is needed to make sure we copy back the right
140 // pixmap on the hide for the Qt frontend
141 lyx_gui::sync_events();
149 Cursor_Shape shape = BAR_SHAPE;
151 LyXText const & text = *bv.getLyXText();
152 LyXFont const & realfont(text.real_current_font);
153 BufferParams const & bp(bv.buffer()->params);
154 bool const samelang = realfont.language() == bp.language;
155 bool const isrtl = realfont.isVisibleRightToLeft();
157 if (!samelang || isrtl != bp.language->RightToLeft()) {
160 shape = REVERSED_L_SHAPE;
163 int ascent = font_metrics::maxAscent(realfont);
164 int descent = font_metrics::maxDescent(realfont);
165 int h = ascent + descent;
168 int const top_y = bv.text->top_y();
170 if (bv.theLockingInset()) {
171 // Would be nice to clean this up to make some understandable sense...
172 UpdatableInset * inset = bv.theLockingInset();
173 inset->getCursor(bv, x, y);
175 // Non-obvious. The reason we have to have these
176 // extra checks is that the ->getCursor() calls rely
177 // on the inset's own knowledge of its screen position.
178 // If we scroll up or down in a big enough increment, the
179 // inset->draw() is not called: this doesn't update
180 // inset.top_baseline, so getCursor() returns an old value.
183 inset->getCursorPos(&bv, bx, by);
184 by += inset->insetInInsetY() + bv.text->cursor.iy();
187 if (by > top_y + workarea().workHeight())
190 x = bv.text->cursor.x();
191 y = bv.text->cursor.y();
197 // if it doesn't fit entirely on the screen, don't try to show it
198 if (y < 0 || y + h > workarea().workHeight())
201 cursor_visible_ = true;
202 showCursor(x, y, h, shape);
206 void LyXScreen::hideCursor()
208 if (!cursor_visible_)
211 cursor_visible_ = false;
216 void LyXScreen::toggleCursor(BufferView & bv)
225 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
226 int /*x*/, int y, int asc, int desc)
228 int const vheight = workarea().workHeight();
229 int newtop = text->top_y();
231 if (y + desc - text->top_y() >= vheight)
232 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
233 else if (y - asc < text->top_y()
234 && text->top_y() > 0) {
235 newtop = y - vheight / 4;
238 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
240 if (newtop != text->top_y()) {
241 draw(text, bv, newtop);
250 unsigned int LyXScreen::topCursorVisible(LyXText * text)
252 LyXCursor const & cursor = text->cursor;
253 int top_y = text->top_y();
255 int const vheight = workarea().workHeight();
257 RowList::iterator row = text->cursorRow();
259 #warning SUPER HACK DISABLED (Lgb)
261 // Is this a hack? Yes, probably... (Lgb)
263 return max(newtop, 0);
265 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
266 if (row->height() < vheight
267 && row->height() > vheight / 4) {
270 - row->baseline() - vheight;
272 // scroll down, the scroll region must be so big!!
273 newtop = cursor.y() - vheight / 2;
276 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
277 top_y && top_y > 0) {
278 if (row->height() < vheight
279 && row->height() > vheight / 4) {
280 newtop = cursor.y() - row->baseline();
283 newtop = cursor.y() - vheight / 2;
284 newtop = min(newtop, top_y);
288 newtop = max(newtop, 0);
294 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
296 // Is a change necessary?
297 int const newtop = topCursorVisible(text);
298 bool const result = (newtop != text->top_y());
300 draw(text, bv, newtop);
307 void LyXScreen::update(BufferView & bv, int yo, int xo)
309 LyXText * text = bv.text;
311 workarea().getPainter().start();
313 if (text->needRefresh()) {
314 int const vwidth = workarea().workWidth();
315 int const vheight = workarea().workHeight();
316 text->updateRowPositions();
318 drawFromTo(text, &bv, y, vheight, yo, xo);
319 expose(0, y, vwidth, vheight - y);
322 workarea().getPainter().end();
326 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
330 // only if there is a selection
331 if (!text->selection.set()) return;
333 int const bottom = min(
334 max(static_cast<int>(text->selection.end.y()
335 - text->getRow(text->selection.end)->baseline()
336 + text->getRow(text->selection.end)->height()),
338 static_cast<int>(text->top_y() + workarea().workHeight()));
340 max(static_cast<int>(text->selection.start.y() -
341 text->getRow(text->selection.start)->baseline()),
343 static_cast<int>(text->top_y() + workarea().workHeight()));
346 text->selection.set(false);
348 workarea().getPainter().start();
350 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(),
352 expose(0, top - text->top_y(),
353 workarea().workWidth(),
354 bottom - text->top_y() - (top - text->top_y()));
356 workarea().getPainter().end();
360 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
363 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
364 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
367 int const top_tmp = text->toggle_cursor.y()
368 - text->getRow(text->toggle_cursor)->baseline();
369 int const bottom_tmp = text->toggle_end_cursor.y()
370 - text->getRow(text->toggle_end_cursor)->baseline()
371 + text->getRow(text->toggle_end_cursor)->height();
373 int const offset = yo < 0 ? yo : 0;
374 int const bottom = min(max(bottom_tmp, text->top_y()),
375 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
376 int const top = min(max(top_tmp, text->top_y()),
377 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
379 workarea().getPainter().start();
381 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(), yo, xo);
382 expose(0, top - text->top_y(), workarea().workWidth(),
383 bottom - text->top_y() - (top - text->top_y()));
385 workarea().getPainter().end();
389 void LyXScreen::redraw(LyXText * text, BufferView * bv)
398 workarea().getPainter().start();
400 text->updateRowPositions();
401 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0);
402 expose(0, 0, workarea().workWidth(), workarea().workHeight());
404 workarea().getPainter().end();
408 void LyXScreen::greyOut()
413 workarea().getPainter().start();
415 workarea().getPainter().fillRectangle(0, 0,
416 workarea().workWidth(),
417 workarea().workHeight(),
420 // Add a splash screen to the centre of the work area
421 SplashScreen const & splash = SplashScreen::get();
422 grfx::Image const * const splash_image = splash.image();
424 int const w = splash_image->getWidth();
425 int const h = splash_image->getHeight();
427 int x = (workarea().workWidth() - w) / 2;
428 int y = (workarea().workHeight() - h) / 2;
430 workarea().getPainter().image(x, y, w, h, *splash_image);
432 string const & splash_text = splash.text();
433 LyXFont const & splash_font = splash.font();
438 workarea().getPainter().text(x, y, splash_text, splash_font);
440 expose(0, 0, workarea().workWidth(), workarea().workHeight());
441 workarea().getPainter().end();
445 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
446 int y1, int y2, int yo, int xo)
448 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
450 int const topy = text->top_y();
451 int y_text = topy + y1;
452 RowList::iterator rit = text->getRowNearY(y_text);
453 int y = y_text - topy;
454 // y1 is now the real beginning of row on the screen
458 RowList::iterator const rend = text->rows().end();
459 while (rit != rend && y < y2) {
460 paintRows(*bv, *text, rit, y + yo, xo, y + topy);
465 // maybe we have to clear the screen at the bottom
466 if ((y < y2) && !text->isInInset()) {
467 workarea().getPainter().fillRectangle(0, y,
468 workarea().workWidth(), y2 - y,