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 int const vwidth = workarea().workWidth();
310 int const vheight = workarea().workHeight();
311 LyXText * text = bv.text;
313 workarea().getPainter().start();
315 switch (text->refreshStatus()) {
316 case LyXText::REFRESH_AREA:
318 text->updateRowPositions();
319 int const y = max(int(text->refresh_y - text->top_y()), 0);
320 drawFromTo(text, &bv, y, vheight, yo, xo);
321 expose(0, y, vwidth, vheight - y);
324 case LyXText::REFRESH_ROW:
326 text->updateRowPositions();
327 // ok I will update the current cursor row
328 drawOneRow(text, &bv, text->refresh_row, text->refresh_y,
330 // this because if we had a major update the refresh_row could
331 // have been set to 0!
332 if (text->refresh_row != text->rows().end()) {
333 expose(0, text->refresh_y - text->top_y() + yo,
334 vwidth, text->refresh_row->height());
338 case LyXText::REFRESH_NONE:
339 // Nothing needs done
343 workarea().getPainter().end();
347 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
351 // only if there is a selection
352 if (!text->selection.set()) return;
354 int const bottom = min(
355 max(static_cast<int>(text->selection.end.y()
356 - text->getRow(text->selection.end)->baseline()
357 + text->getRow(text->selection.end)->height()),
359 static_cast<int>(text->top_y() + workarea().workHeight()));
361 max(static_cast<int>(text->selection.start.y() -
362 text->getRow(text->selection.start)->baseline()),
364 static_cast<int>(text->top_y() + workarea().workHeight()));
367 text->selection.set(false);
369 workarea().getPainter().start();
371 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(),
373 expose(0, top - text->top_y(),
374 workarea().workWidth(),
375 bottom - text->top_y() - (top - text->top_y()));
377 workarea().getPainter().end();
381 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
384 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
385 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
388 int const top_tmp = text->toggle_cursor.y()
389 - text->getRow(text->toggle_cursor)->baseline();
390 int const bottom_tmp = text->toggle_end_cursor.y()
391 - text->getRow(text->toggle_end_cursor)->baseline()
392 + text->getRow(text->toggle_end_cursor)->height();
394 int const offset = yo < 0 ? yo : 0;
395 int const bottom = min(max(bottom_tmp, text->top_y()),
396 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
397 int const top = min(max(top_tmp, text->top_y()),
398 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
400 workarea().getPainter().start();
402 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(), yo, xo);
403 expose(0, top - text->top_y(), workarea().workWidth(),
404 bottom - text->top_y() - (top - text->top_y()));
406 workarea().getPainter().end();
410 void LyXScreen::redraw(LyXText * text, BufferView * bv)
419 workarea().getPainter().start();
421 text->updateRowPositions();
422 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0);
423 expose(0, 0, workarea().workWidth(), workarea().workHeight());
425 workarea().getPainter().end();
429 void LyXScreen::greyOut()
434 workarea().getPainter().start();
436 workarea().getPainter().fillRectangle(0, 0,
437 workarea().workWidth(),
438 workarea().workHeight(),
441 // Add a splash screen to the centre of the work area
442 SplashScreen const & splash = SplashScreen::get();
443 grfx::Image const * const splash_image = splash.image();
445 int const w = splash_image->getWidth();
446 int const h = splash_image->getHeight();
448 int x = (workarea().workWidth() - w) / 2;
449 int y = (workarea().workHeight() - h) / 2;
451 workarea().getPainter().image(x, y, w, h, *splash_image);
453 string const & splash_text = splash.text();
454 LyXFont const & splash_font = splash.font();
459 workarea().getPainter().text(x, y, splash_text, splash_font);
461 expose(0, 0, workarea().workWidth(), workarea().workHeight());
462 workarea().getPainter().end();
466 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
467 int y1, int y2, int yo, int xo)
469 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
471 int const topy = text->top_y();
472 int y_text = topy + y1;
473 RowList::iterator rit = text->getRowNearY(y_text);
474 int y = y_text - topy;
475 // y1 is now the real beginning of row on the screen
479 RowList::iterator const rend = text->rows().end();
480 while (rit != rend && y < y2) {
481 RowPainter rp(*bv, *text, rit);
482 rp.paint(y + yo, xo, y + topy);
487 // maybe we have to clear the screen at the bottom
488 if ((y < y2) && !text->isInInset()) {
489 workarea().getPainter().fillRectangle(0, y,
490 workarea().workWidth(), y2 - y,
496 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv,
497 RowList::iterator row,
498 int y_text, int yo, int xo)
500 int const y = y_text - text->top_y() + yo;
502 if (y + row->height() <= 0)
505 if (y - row->height() > workarea().workHeight())
510 RowPainter rp(*bv, *text, row);
511 rp.paint(y, xo, y + text->top_y());