3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
8 * Splash screen code added by Angus Leeming
12 #pragma implementation
21 #include "BufferView.h"
25 #include "font_metrics.h"
29 // Splash screen-specific stuff
33 #include "graphics/GraphicsLoader.h"
34 #include "graphics/GraphicsImage.h"
36 #include "support/filetools.h" // LibFileSearch
38 #include <boost/utility.hpp>
39 #include <boost/bind.hpp>
40 #include <boost/signals/trackable.hpp>
48 class SplashScreen : boost::noncopyable, boost::signals::trackable {
50 /// This is a singleton class. Get the instance.
51 static SplashScreen const & get();
53 grfx::Image const * image() const { return loader_.image(); }
55 string const & text() const { return text_; }
57 LyXFont const & font() const { return font_; }
60 /** Make the c-tor private so we can control how many objects
67 /// The text to be written on top of the pixmap
74 SplashScreen const & SplashScreen::get()
76 static SplashScreen singleton;
81 SplashScreen::SplashScreen()
82 : text_(lyx_version ? lyx_version : "unknown")
84 if (!lyxrc.show_banner)
87 string const file = LibFileSearch("images", "banner", "xpm");
91 // The font used to display the version info
92 font_.setFamily(LyXFont::SANS_FAMILY);
93 font_.setSeries(LyXFont::BOLD_SERIES);
94 font_.setSize(LyXFont::SIZE_NORMAL);
95 font_.setColor(LColor::yellow);
97 // Load up the graphics file
99 // We aren't interested here in when the image is loaded.
100 // If it isn't ready when we want it, then we ignore it.
101 // loader_->statusChanged.connect(
102 // boost::bind(&SplashScreen::statusChanged, this));
103 if (loader_.status() == grfx::WaitingToLoad)
104 loader_.startLoading();
110 LyXScreen::LyXScreen()
111 : force_clear_(true), cursor_visible_(false)
113 // Start loading the pixmap as soon as possible
118 LyXScreen::~LyXScreen()
122 // FIXME: GUII these cursor methods need to decide
123 // whether the workarea is focused or not
125 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
130 workarea().getPainter().start();
132 Cursor_Shape shape = BAR_SHAPE;
133 BufferParams const & bp(bv->buffer()->params);
134 LyXFont const & realfont(text->real_current_font);
136 if (realfont.language() != bp.language
137 || realfont.isVisibleRightToLeft()
138 != bp.language->RightToLeft()) {
139 shape = (realfont.isVisibleRightToLeft())
140 ? REVERSED_L_SHAPE : L_SHAPE;
143 showManualCursor(text, text->cursor.x(), text->cursor.y(),
144 font_metrics::maxAscent(realfont),
145 font_metrics::maxDescent(realfont),
148 workarea().getPainter().end();
152 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
153 int /*x*/, int y, int asc, int desc)
155 int const vheight = workarea().workHeight();
156 int newtop = text->first_y;
158 if (y + desc - text->first_y >= vheight)
159 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
160 else if (y - asc < text->first_y
161 && text->first_y > 0) {
162 newtop = y - vheight / 4;
165 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
167 if (newtop != text->first_y) {
168 draw(text, bv, newtop);
169 text->first_y = newtop;
176 void LyXScreen::cursorToggle(BufferView * bv) const
185 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
187 int const vheight = workarea().workHeight();
190 Row * row = cursor.row();
192 // Is this a hack? Yes, probably... (Lgb)
194 return max(newtop, 0);
196 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
197 if (row->height() < vheight
198 && row->height() > vheight / 4) {
201 - row->baseline() - vheight;
205 - vheight / 2; /* the scroll region must be so big!! */
208 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
209 top_y && top_y > 0) {
210 if (row->height() < vheight
211 && row->height() > vheight / 4) {
212 newtop = cursor.y() - row->baseline();
215 newtop = cursor.y() - vheight / 2;
216 newtop = min(newtop, top_y);
220 newtop = max(newtop, 0);
226 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
228 // Is a change necessary?
229 int const newtop = topCursorVisible(text->cursor, text->first_y);
230 bool const result = (newtop != text->first_y);
232 draw(text, bv, newtop);
237 void LyXScreen::update(LyXText * text, BufferView * bv,
240 int const vwidth = workarea().workWidth();
241 int const vheight = workarea().workHeight();
243 workarea().getPainter().start();
245 switch (text->status()) {
246 case LyXText::NEED_MORE_REFRESH:
248 int const y = max(int(text->refresh_y - text->first_y), 0);
249 drawFromTo(text, bv, y, vheight, yo, xo);
251 // otherwise this is called ONLY from BufferView_pimpl(update)
252 // or we should see to set this flag accordingly
253 if (text != bv->text)
254 text->status(bv, LyXText::UNCHANGED);
255 expose(0, y, vwidth, vheight - y);
258 case LyXText::NEED_VERY_LITTLE_REFRESH:
260 // ok I will update the current cursor row
261 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
263 // this because if we had a major update the refresh_row could
264 // have been set to 0!
265 if (text->refresh_row) {
266 // otherwise this is called ONLY from BufferView_pimpl(update)
267 // or we should see to set this flag accordingly
268 if (text != bv->text)
269 text->status(bv, LyXText::UNCHANGED);
270 expose(0, text->refresh_y - text->first_y + yo,
271 vwidth, text->refresh_row->height());
275 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
276 case LyXText::UNCHANGED:
277 // Nothing needs done
281 workarea().getPainter().end();
285 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
289 // only if there is a selection
290 if (!text->selection.set()) return;
292 int const bottom = min(
293 max(static_cast<int>(text->selection.end.y()
294 - text->selection.end.row()->baseline()
295 + text->selection.end.row()->height()),
297 static_cast<int>(text->first_y + workarea().workHeight()));
299 max(static_cast<int>(text->selection.start.y() -
300 text->selection.start.row()->baseline()),
302 static_cast<int>(text->first_y + workarea().workHeight()));
305 text->selection.set(false);
307 workarea().getPainter().start();
309 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
311 expose(0, top - text->first_y,
312 workarea().workWidth(),
313 bottom - text->first_y - (top - text->first_y));
315 workarea().getPainter().end();
319 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
322 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
323 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
326 int const top_tmp = text->toggle_cursor.y()
327 - text->toggle_cursor.row()->baseline();
328 int const bottom_tmp = text->toggle_end_cursor.y()
329 - text->toggle_end_cursor.row()->baseline()
330 + text->toggle_end_cursor.row()->height();
332 int const offset = yo < 0 ? yo : 0;
333 int const bottom = min(max(bottom_tmp, text->first_y),
334 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
335 int const top = min(max(top_tmp, text->first_y),
336 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
338 workarea().getPainter().start();
340 drawFromTo(text, bv, top - text->first_y,
341 bottom - text->first_y, yo,
343 expose(0, top - text->first_y, workarea().workWidth(),
344 bottom - text->first_y - (top - text->first_y));
346 workarea().getPainter().end();
350 void LyXScreen::redraw(LyXText * text, BufferView * bv)
352 workarea().getPainter().start();
356 expose(0, 0, workarea().workWidth(), workarea().workHeight());
357 workarea().getPainter().end();
361 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
362 expose(0, 0, workarea().workWidth(), workarea().workHeight());
364 workarea().getPainter().end();
366 if (cursor_visible_) {
367 cursor_visible_ = false;
373 void LyXScreen::greyOut()
375 workarea().getPainter().fillRectangle(0, 0,
376 workarea().workWidth(),
377 workarea().workHeight(),
380 // Add a splash screen to the centre of the work area
381 SplashScreen const & splash = SplashScreen::get();
382 grfx::Image const * const splash_image = splash.image();
384 int const w = splash_image->getWidth();
385 int const h = splash_image->getHeight();
387 int x = (workarea().workWidth() - w) / 2;
388 int y = (workarea().workHeight() - h) / 2;
390 workarea().getPainter().image(x, y, w, h, *splash_image);
392 string const & splash_text = splash.text();
393 LyXFont const & splash_font = splash.font();
398 workarea().getPainter().text(x, y, splash_text, splash_font);
403 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
404 int y1, int y2, int yo, int xo,
407 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
409 int y_text = text->first_y + y1;
411 // get the first needed row
412 Row * row = text->getRowNearY(y_text);
413 // y_text is now the real beginning of the row
415 int y = y_text - text->first_y;
416 // y1 is now the real beginning of row on the screen
418 while (row != 0 && y < y2) {
419 LyXText::text_status st = text->status();
420 text->getVisibleRow(bv, y + yo,
421 xo, row, y + text->first_y);
422 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
423 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
424 text->fullRebreak(bv);
425 st = LyXText::NEED_MORE_REFRESH;
426 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
427 text->status(bv, st);
428 text->getVisibleRow(bv, y + yo,
429 xo, row, y + text->first_y);
434 force_clear_ = false;
436 // maybe we have to clear the screen at the bottom
437 if ((y < y2) && text->bv_owner) {
438 workarea().getPainter().fillRectangle(0, y,
439 workarea().workWidth(), y2 - y,
445 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
446 int y_text, int yo, int xo)
448 int const y = y_text - text->first_y + yo;
450 if (((y + row->height()) > 0) &&
451 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
452 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
454 force_clear_ = false;