3 * Read the file COPYING
7 * Full author contact details are available in file CREDITS
9 * Splash screen code added by Angus Leeming
13 #pragma implementation
22 #include "BufferView.h"
26 #include "font_metrics.h"
30 // Splash screen-specific stuff
34 #include "graphics/GraphicsLoader.h"
35 #include "graphics/GraphicsImage.h"
37 #include "support/filetools.h" // LibFileSearch
39 #include <boost/utility.hpp>
40 #include <boost/bind.hpp>
41 #include <boost/signals/trackable.hpp>
49 class SplashScreen : boost::noncopyable, boost::signals::trackable {
51 /// This is a singleton class. Get the instance.
52 static SplashScreen const & get();
54 grfx::Image const * image() const { return loader_.image(); }
56 string const & text() const { return text_; }
58 LyXFont const & font() const { return font_; }
61 /** Make the c-tor private so we can control how many objects
68 /// The text to be written on top of the pixmap
75 SplashScreen const & SplashScreen::get()
77 static SplashScreen singleton;
82 SplashScreen::SplashScreen()
83 : text_(lyx_version ? lyx_version : "unknown")
85 if (!lyxrc.show_banner)
88 string const file = LibFileSearch("images", "banner", "xpm");
92 // The font used to display the version info
93 font_.setFamily(LyXFont::SANS_FAMILY);
94 font_.setSeries(LyXFont::BOLD_SERIES);
95 font_.setSize(LyXFont::SIZE_NORMAL);
96 font_.setColor(LColor::yellow);
98 // Load up the graphics file
100 // We aren't interested here in when the image is loaded.
101 // If it isn't ready when we want it, then we ignore it.
102 // loader_->statusChanged.connect(
103 // boost::bind(&SplashScreen::statusChanged, this));
104 if (loader_.status() == grfx::WaitingToLoad)
105 loader_.startLoading();
111 LyXScreen::LyXScreen()
112 : force_clear_(true), cursor_visible_(false)
114 // Start loading the pixmap as soon as possible
119 LyXScreen::~LyXScreen()
123 // FIXME: GUII these cursor methods need to decide
124 // whether the workarea is focused or not
126 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
131 workarea().getPainter().start();
133 Cursor_Shape shape = BAR_SHAPE;
134 BufferParams const & bp(bv->buffer()->params);
135 LyXFont const & realfont(text->real_current_font);
137 if (realfont.language() != bp.language
138 || realfont.isVisibleRightToLeft()
139 != bp.language->RightToLeft()) {
140 shape = (realfont.isVisibleRightToLeft())
141 ? REVERSED_L_SHAPE : L_SHAPE;
144 showManualCursor(text, text->cursor.x(), text->cursor.y(),
145 font_metrics::maxAscent(realfont),
146 font_metrics::maxDescent(realfont),
149 workarea().getPainter().end();
153 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
154 int /*x*/, int y, int asc, int desc)
156 int const vheight = workarea().workHeight();
157 int newtop = text->first_y;
159 if (y + desc - text->first_y >= vheight)
160 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
161 else if (y - asc < text->first_y
162 && text->first_y > 0) {
163 newtop = y - vheight / 4;
166 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
168 if (newtop != text->first_y) {
169 draw(text, bv, newtop);
170 text->first_y = newtop;
177 void LyXScreen::cursorToggle(BufferView * bv) const
186 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
188 int const vheight = workarea().workHeight();
191 Row * row = cursor.row();
193 // Is this a hack? Yes, probably... (Lgb)
195 return max(newtop, 0);
197 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
198 if (row->height() < vheight
199 && row->height() > vheight / 4) {
202 - row->baseline() - vheight;
206 - vheight / 2; /* the scroll region must be so big!! */
209 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
210 top_y && top_y > 0) {
211 if (row->height() < vheight
212 && row->height() > vheight / 4) {
213 newtop = cursor.y() - row->baseline();
216 newtop = cursor.y() - vheight / 2;
217 newtop = min(newtop, top_y);
221 newtop = max(newtop, 0);
227 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
229 // Is a change necessary?
230 int const newtop = topCursorVisible(text->cursor, text->first_y);
231 bool const result = (newtop != text->first_y);
233 draw(text, bv, newtop);
238 void LyXScreen::update(LyXText * text, BufferView * bv,
241 int const vwidth = workarea().workWidth();
242 int const vheight = workarea().workHeight();
244 workarea().getPainter().start();
246 switch (text->status()) {
247 case LyXText::NEED_MORE_REFRESH:
249 int const y = max(int(text->refresh_y - text->first_y), 0);
250 drawFromTo(text, bv, y, vheight, yo, xo);
252 // otherwise this is called ONLY from BufferView_pimpl(update)
253 // or we should see to set this flag accordingly
254 if (text != bv->text)
255 text->status(bv, LyXText::UNCHANGED);
256 expose(0, y, vwidth, vheight - y);
259 case LyXText::NEED_VERY_LITTLE_REFRESH:
261 // ok I will update the current cursor row
262 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
264 // this because if we had a major update the refresh_row could
265 // have been set to 0!
266 if (text->refresh_row) {
267 // otherwise this is called ONLY from BufferView_pimpl(update)
268 // or we should see to set this flag accordingly
269 if (text != bv->text)
270 text->status(bv, LyXText::UNCHANGED);
271 expose(0, text->refresh_y - text->first_y + yo,
272 vwidth, text->refresh_row->height());
276 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
277 case LyXText::UNCHANGED:
278 // Nothing needs done
282 workarea().getPainter().end();
286 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
290 // only if there is a selection
291 if (!text->selection.set()) return;
293 int const bottom = min(
294 max(static_cast<int>(text->selection.end.y()
295 - text->selection.end.row()->baseline()
296 + text->selection.end.row()->height()),
298 static_cast<int>(text->first_y + workarea().workHeight()));
300 max(static_cast<int>(text->selection.start.y() -
301 text->selection.start.row()->baseline()),
303 static_cast<int>(text->first_y + workarea().workHeight()));
306 text->selection.set(false);
308 workarea().getPainter().start();
310 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
312 expose(0, top - text->first_y,
313 workarea().workWidth(),
314 bottom - text->first_y - (top - text->first_y));
316 workarea().getPainter().end();
320 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
323 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
324 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
327 int const top_tmp = text->toggle_cursor.y()
328 - text->toggle_cursor.row()->baseline();
329 int const bottom_tmp = text->toggle_end_cursor.y()
330 - text->toggle_end_cursor.row()->baseline()
331 + text->toggle_end_cursor.row()->height();
333 int const offset = yo < 0 ? yo : 0;
334 int const bottom = min(max(bottom_tmp, text->first_y),
335 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
336 int const top = min(max(top_tmp, text->first_y),
337 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
339 workarea().getPainter().start();
341 drawFromTo(text, bv, top - text->first_y,
342 bottom - text->first_y, yo,
344 expose(0, top - text->first_y, workarea().workWidth(),
345 bottom - text->first_y - (top - text->first_y));
347 workarea().getPainter().end();
351 void LyXScreen::redraw(LyXText * text, BufferView * bv)
353 workarea().getPainter().start();
357 expose(0, 0, workarea().workWidth(), workarea().workHeight());
358 workarea().getPainter().end();
362 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
363 expose(0, 0, workarea().workWidth(), workarea().workHeight());
365 workarea().getPainter().end();
367 if (cursor_visible_) {
368 cursor_visible_ = false;
374 void LyXScreen::greyOut()
376 workarea().getPainter().fillRectangle(0, 0,
377 workarea().workWidth(),
378 workarea().workHeight(),
381 // Add a splash screen to the centre of the work area
382 SplashScreen const & splash = SplashScreen::get();
383 grfx::Image const * const splash_image = splash.image();
385 int const w = splash_image->getWidth();
386 int const h = splash_image->getHeight();
388 int x = (workarea().workWidth() - w) / 2;
389 int y = (workarea().workHeight() - h) / 2;
391 workarea().getPainter().image(x, y, w, h, *splash_image);
393 string const & splash_text = splash.text();
394 LyXFont const & splash_font = splash.font();
399 workarea().getPainter().text(x, y, splash_text, splash_font);
404 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
405 int y1, int y2, int yo, int xo,
408 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
410 int y_text = text->first_y + y1;
412 // get the first needed row
413 Row * row = text->getRowNearY(y_text);
414 // y_text is now the real beginning of the row
416 int y = y_text - text->first_y;
417 // y1 is now the real beginning of row on the screen
419 while (row != 0 && y < y2) {
420 LyXText::text_status st = text->status();
421 text->getVisibleRow(bv, y + yo,
422 xo, row, y + text->first_y);
423 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
424 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
425 text->fullRebreak(bv);
426 st = LyXText::NEED_MORE_REFRESH;
427 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
428 text->status(bv, st);
429 text->getVisibleRow(bv, y + yo,
430 xo, row, y + text->first_y);
435 force_clear_ = false;
437 // maybe we have to clear the screen at the bottom
438 if ((y < y2) && text->bv_owner) {
439 workarea().getPainter().fillRectangle(0, y,
440 workarea().workWidth(), y2 - y,
446 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
447 int y_text, int yo, int xo)
449 int const y = y_text - text->first_y + yo;
451 if (((y + row->height()) > 0) &&
452 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
453 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
455 force_clear_ = false;