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
14 #pragma implementation
23 #include "BufferView.h"
27 #include "font_metrics.h"
31 // Splash screen-specific stuff
35 #include "graphics/GraphicsLoader.h"
36 #include "graphics/GraphicsImage.h"
38 #include "support/filetools.h" // LibFileSearch
40 #include <boost/utility.hpp>
41 #include <boost/bind.hpp>
42 #include <boost/signals/trackable.hpp>
50 class SplashScreen : boost::noncopyable, boost::signals::trackable {
52 /// This is a singleton class. Get the instance.
53 static SplashScreen const & get();
55 grfx::Image const * image() const { return loader_.image(); }
57 string const & text() const { return text_; }
59 LyXFont const & font() const { return font_; }
62 /** Make the c-tor private so we can control how many objects
69 /// The text to be written on top of the pixmap
76 SplashScreen const & SplashScreen::get()
78 static SplashScreen singleton;
83 SplashScreen::SplashScreen()
84 : text_(lyx_version ? lyx_version : "unknown")
86 if (!lyxrc.show_banner)
89 string const file = LibFileSearch("images", "banner", "xpm");
93 // The font used to display the version info
94 font_.setFamily(LyXFont::SANS_FAMILY);
95 font_.setSeries(LyXFont::BOLD_SERIES);
96 font_.setSize(LyXFont::SIZE_NORMAL);
97 font_.setColor(LColor::yellow);
99 // Load up the graphics file
101 // We aren't interested here in when the image is loaded.
102 // If it isn't ready when we want it, then we ignore it.
103 // loader_->statusChanged.connect(
104 // boost::bind(&SplashScreen::statusChanged, this));
105 if (loader_.status() == grfx::WaitingToLoad)
106 loader_.startLoading();
112 LyXScreen::LyXScreen()
113 : force_clear_(true), cursor_visible_(false)
115 // Start loading the pixmap as soon as possible
120 LyXScreen::~LyXScreen()
124 // FIXME: GUII these cursor methods need to decide
125 // whether the workarea is focused or not
127 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
132 workarea().getPainter().start();
134 Cursor_Shape shape = BAR_SHAPE;
135 BufferParams const & bp(bv->buffer()->params);
136 LyXFont const & realfont(text->real_current_font);
138 if (realfont.language() != bp.language
139 || realfont.isVisibleRightToLeft()
140 != bp.language->RightToLeft()) {
141 shape = (realfont.isVisibleRightToLeft())
142 ? REVERSED_L_SHAPE : L_SHAPE;
145 showManualCursor(text, text->cursor.x(), text->cursor.y(),
146 font_metrics::maxAscent(realfont),
147 font_metrics::maxDescent(realfont),
150 workarea().getPainter().end();
154 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
155 int /*x*/, int y, int asc, int desc)
157 int const vheight = workarea().workHeight();
158 int newtop = text->first_y;
160 if (y + desc - text->first_y >= vheight)
161 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
162 else if (y - asc < text->first_y
163 && text->first_y > 0) {
164 newtop = y - vheight / 4;
167 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
169 if (newtop != text->first_y) {
170 draw(text, bv, newtop);
171 text->first_y = newtop;
178 void LyXScreen::cursorToggle(BufferView * bv) const
187 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
189 int const vheight = workarea().workHeight();
192 Row * row = cursor.row();
194 // Is this a hack? Yes, probably... (Lgb)
196 return max(newtop, 0);
198 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
199 if (row->height() < vheight
200 && row->height() > vheight / 4) {
203 - row->baseline() - vheight;
207 - vheight / 2; /* the scroll region must be so big!! */
210 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
211 top_y && top_y > 0) {
212 if (row->height() < vheight
213 && row->height() > vheight / 4) {
214 newtop = cursor.y() - row->baseline();
217 newtop = cursor.y() - vheight / 2;
218 newtop = min(newtop, top_y);
222 newtop = max(newtop, 0);
228 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
230 // Is a change necessary?
231 int const newtop = topCursorVisible(text->cursor, text->first_y);
232 bool const result = (newtop != text->first_y);
234 draw(text, bv, newtop);
239 void LyXScreen::update(LyXText * text, BufferView * bv,
242 int const vwidth = workarea().workWidth();
243 int const vheight = workarea().workHeight();
245 workarea().getPainter().start();
247 switch (text->status()) {
248 case LyXText::NEED_MORE_REFRESH:
250 int const y = max(int(text->refresh_y - text->first_y), 0);
251 drawFromTo(text, bv, y, vheight, yo, xo);
253 // otherwise this is called ONLY from BufferView_pimpl(update)
254 // or we should see to set this flag accordingly
255 if (text != bv->text)
256 text->status(bv, LyXText::UNCHANGED);
257 expose(0, y, vwidth, vheight - y);
260 case LyXText::NEED_VERY_LITTLE_REFRESH:
262 // ok I will update the current cursor row
263 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
265 // this because if we had a major update the refresh_row could
266 // have been set to 0!
267 if (text->refresh_row) {
268 // otherwise this is called ONLY from BufferView_pimpl(update)
269 // or we should see to set this flag accordingly
270 if (text != bv->text)
271 text->status(bv, LyXText::UNCHANGED);
272 expose(0, text->refresh_y - text->first_y + yo,
273 vwidth, text->refresh_row->height());
277 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
278 case LyXText::UNCHANGED:
279 // Nothing needs done
283 workarea().getPainter().end();
287 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
291 // only if there is a selection
292 if (!text->selection.set()) return;
294 int const bottom = min(
295 max(static_cast<int>(text->selection.end.y()
296 - text->selection.end.row()->baseline()
297 + text->selection.end.row()->height()),
299 static_cast<int>(text->first_y + workarea().workHeight()));
301 max(static_cast<int>(text->selection.start.y() -
302 text->selection.start.row()->baseline()),
304 static_cast<int>(text->first_y + workarea().workHeight()));
307 text->selection.set(false);
309 workarea().getPainter().start();
311 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
313 expose(0, top - text->first_y,
314 workarea().workWidth(),
315 bottom - text->first_y - (top - text->first_y));
317 workarea().getPainter().end();
321 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
324 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
325 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
328 int const top_tmp = text->toggle_cursor.y()
329 - text->toggle_cursor.row()->baseline();
330 int const bottom_tmp = text->toggle_end_cursor.y()
331 - text->toggle_end_cursor.row()->baseline()
332 + text->toggle_end_cursor.row()->height();
334 int const offset = yo < 0 ? yo : 0;
335 int const bottom = min(max(bottom_tmp, text->first_y),
336 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
337 int const top = min(max(top_tmp, text->first_y),
338 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
340 workarea().getPainter().start();
342 drawFromTo(text, bv, top - text->first_y,
343 bottom - text->first_y, yo,
345 expose(0, top - text->first_y, workarea().workWidth(),
346 bottom - text->first_y - (top - text->first_y));
348 workarea().getPainter().end();
352 void LyXScreen::redraw(LyXText * text, BufferView * bv)
354 workarea().getPainter().start();
358 expose(0, 0, workarea().workWidth(), workarea().workHeight());
359 workarea().getPainter().end();
363 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
364 expose(0, 0, workarea().workWidth(), workarea().workHeight());
366 workarea().getPainter().end();
368 if (cursor_visible_) {
369 cursor_visible_ = false;
375 void LyXScreen::greyOut()
377 workarea().getPainter().fillRectangle(0, 0,
378 workarea().workWidth(),
379 workarea().workHeight(),
382 // Add a splash screen to the centre of the work area
383 SplashScreen const & splash = SplashScreen::get();
384 grfx::Image const * const splash_image = splash.image();
386 int const w = splash_image->getWidth();
387 int const h = splash_image->getHeight();
389 int x = (workarea().workWidth() - w) / 2;
390 int y = (workarea().workHeight() - h) / 2;
392 workarea().getPainter().image(x, y, w, h, *splash_image);
394 string const & splash_text = splash.text();
395 LyXFont const & splash_font = splash.font();
400 workarea().getPainter().text(x, y, splash_text, splash_font);
405 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
406 int y1, int y2, int yo, int xo,
409 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
411 int y_text = text->first_y + y1;
413 // get the first needed row
414 Row * row = text->getRowNearY(y_text);
415 // y_text is now the real beginning of the row
417 int y = y_text - text->first_y;
418 // y1 is now the real beginning of row on the screen
420 while (row != 0 && y < y2) {
421 LyXText::text_status st = text->status();
422 text->getVisibleRow(bv, y + yo,
423 xo, row, y + text->first_y);
424 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
425 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
426 text->fullRebreak(bv);
427 st = LyXText::NEED_MORE_REFRESH;
428 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
429 text->status(bv, st);
430 text->getVisibleRow(bv, y + yo,
431 xo, row, y + text->first_y);
436 force_clear_ = false;
438 // maybe we have to clear the screen at the bottom
439 if ((y < y2) && text->bv_owner) {
440 workarea().getPainter().fillRectangle(0, y,
441 workarea().workWidth(), y2 - y,
447 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
448 int y_text, int yo, int xo)
450 int const y = y_text - text->first_y + yo;
452 if (((y + row->height()) > 0) &&
453 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
454 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
456 force_clear_ = false;