3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
10 #pragma implementation
18 #include "BufferView.h"
22 #include "font_metrics.h"
31 LyXScreen::LyXScreen()
32 : force_clear_(true), cursor_visible_(false)
37 LyXScreen::~LyXScreen()
41 // FIXME: GUII these cursor methods need to decide
42 // whether the workarea is focused or not
44 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
49 workarea().getPainter().start();
51 Cursor_Shape shape = BAR_SHAPE;
52 BufferParams const & bp(bv->buffer()->params);
53 LyXFont const & realfont(text->real_current_font);
55 if (realfont.language() != bp.language
56 || realfont.isVisibleRightToLeft()
57 != bp.language->RightToLeft()) {
58 shape = (realfont.isVisibleRightToLeft())
59 ? REVERSED_L_SHAPE : L_SHAPE;
62 showManualCursor(text, text->cursor.x(), text->cursor.y(),
63 font_metrics::maxAscent(realfont),
64 font_metrics::maxDescent(realfont),
67 workarea().getPainter().end();
71 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
72 int /*x*/, int y, int asc, int desc)
74 int const vheight = workarea().workHeight();
75 int newtop = text->first_y;
77 if (y + desc - text->first_y >= vheight)
78 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
79 else if (y - asc < text->first_y
80 && text->first_y > 0) {
81 newtop = y - vheight / 4;
84 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
86 if (newtop != text->first_y) {
87 draw(text, bv, newtop);
88 text->first_y = newtop;
95 void LyXScreen::cursorToggle(BufferView * bv) const
104 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
106 int const vheight = workarea().workHeight();
109 Row * row = cursor.row();
111 // Is this a hack? Yes, probably... (Lgb)
113 return max(newtop, 0);
115 if (cursor.y() - row->baseline() + row->height()
116 - top_y >= vheight) {
117 if (row->height() < vheight
118 && row->height() > vheight / 4) {
121 - row->baseline() - vheight;
125 - vheight / 2; /* the scroll region must be so big!! */
128 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
129 top_y && top_y > 0) {
130 if (row->height() < vheight
131 && row->height() > vheight / 4) {
132 newtop = cursor.y() - row->baseline();
135 newtop = cursor.y() - vheight / 2;
136 newtop = min(newtop, top_y);
140 newtop = max(newtop, 0);
146 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
148 // Is a change necessary?
149 int const newtop = topCursorVisible(text->cursor, text->first_y);
150 bool const result = (newtop != text->first_y);
152 draw(text, bv, newtop);
157 void LyXScreen::update(LyXText * text, BufferView * bv,
160 int const vwidth = workarea().workWidth();
161 int const vheight = workarea().workHeight();
163 workarea().getPainter().start();
165 switch (text->status()) {
166 case LyXText::NEED_MORE_REFRESH:
168 int const y = max(int(text->refresh_y - text->first_y), 0);
169 drawFromTo(text, bv, y, vheight, yo, xo);
171 // otherwise this is called ONLY from BufferView_pimpl(update)
172 // or we should see to set this flag accordingly
173 if (text != bv->text)
174 text->status(bv, LyXText::UNCHANGED);
175 expose(0, y, vwidth, vheight - y);
178 case LyXText::NEED_VERY_LITTLE_REFRESH:
180 // ok I will update the current cursor row
181 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
183 // this because if we had a major update the refresh_row could
184 // have been set to 0!
185 if (text->refresh_row) {
186 // otherwise this is called ONLY from BufferView_pimpl(update)
187 // or we should see to set this flag accordingly
188 if (text != bv->text)
189 text->status(bv, LyXText::UNCHANGED);
190 expose(0, text->refresh_y - text->first_y + yo,
191 vwidth, text->refresh_row->height());
195 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
196 case LyXText::UNCHANGED:
197 // Nothing needs done
201 workarea().getPainter().end();
205 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
209 // only if there is a selection
210 if (!text->selection.set()) return;
212 int const bottom = min(
213 max(static_cast<int>(text->selection.end.y()
214 - text->selection.end.row()->baseline()
215 + text->selection.end.row()->height()),
217 static_cast<int>(text->first_y + workarea().workHeight()));
219 max(static_cast<int>(text->selection.start.y() -
220 text->selection.start.row()->baseline()),
222 static_cast<int>(text->first_y + workarea().workHeight()));
225 text->selection.set(false);
227 workarea().getPainter().start();
229 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
231 expose(0, top - text->first_y,
232 workarea().workWidth(),
233 bottom - text->first_y - (top - text->first_y));
235 workarea().getPainter().end();
239 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
242 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
243 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
246 int const top_tmp = text->toggle_cursor.y()
247 - text->toggle_cursor.row()->baseline();
248 int const bottom_tmp = text->toggle_end_cursor.y()
249 - text->toggle_end_cursor.row()->baseline()
250 + text->toggle_end_cursor.row()->height();
252 int const offset = yo < 0 ? yo : 0;
253 int const bottom = min(max(bottom_tmp, text->first_y),
254 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
255 int const top = min(max(top_tmp, text->first_y),
256 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
258 workarea().getPainter().start();
260 drawFromTo(text, bv, top - text->first_y,
261 bottom - text->first_y, yo,
263 expose(0, top - text->first_y, workarea().workWidth(),
264 bottom - text->first_y - (top - text->first_y));
266 workarea().getPainter().end();
270 void LyXScreen::redraw(LyXText * text, BufferView * bv)
272 workarea().getPainter().start();
276 expose(0, 0, workarea().workWidth(), workarea().workHeight());
277 workarea().getPainter().end();
281 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
282 expose(0, 0, workarea().workWidth(), workarea().workHeight());
284 workarea().getPainter().end();
286 if (cursor_visible_) {
287 cursor_visible_ = false;
293 void LyXScreen::greyOut()
295 workarea().getPainter().fillRectangle(0, 0,
296 workarea().workWidth(),
297 workarea().workHeight(),
300 // FIXME: pending GUIIzation / cleanup of graphics cache.
301 // We should be using something like this.
303 static bool first = true;
307 splash_file_ = (lyxrc.show_banner) ?
308 LibFileSearch("images", "banner", "xpm") : string();
310 grfx::GCache & gc = grfx::GCache::get();
311 gc.add(splash_file_);
312 gc.startLoading(splash_file_);
316 // Add a splash screen to the centre of the work area
317 grfx::GCache & gc = grfx::GCache::get();
318 grfx::ImagePtr const splash = gc.image(splash_file_);
320 int const w = splash->getWidth();
321 int const h = splash->getHeight();
323 int const x = 0.5 * (workarea().workWidth() - w);
324 int const y = 0.5 * (workarea().workHeight() - h);
326 workarea().getPainter().image(x, y, w, h, splash->getPixmap());
329 // Alternatively, we should compile this into the code.
330 // I think that that is better here (so that the pixmap is displayed on
332 // Would need a new method
333 // virtual Pixmap splashPixmap() = 0;
335 // Angus 21 June 2002
339 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
340 int y1, int y2, int yo, int xo,
343 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
345 int y_text = text->first_y + y1;
347 // get the first needed row
348 Row * row = text->getRowNearY(y_text);
349 // y_text is now the real beginning of the row
351 int y = y_text - text->first_y;
352 // y1 is now the real beginning of row on the screen
354 while (row != 0 && y < y2) {
355 LyXText::text_status st = text->status();
356 text->getVisibleRow(bv, y + yo,
357 xo, row, y + text->first_y);
358 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
359 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
360 text->fullRebreak(bv);
361 st = LyXText::NEED_MORE_REFRESH;
362 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
363 text->status(bv, st);
364 text->getVisibleRow(bv, y + yo,
365 xo, row, y + text->first_y);
370 force_clear_ = false;
372 // maybe we have to clear the screen at the bottom
373 if ((y < y2) && text->bv_owner) {
374 workarea().getPainter().fillRectangle(0, y,
375 workarea().workWidth(), y2 - y,
381 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
382 int y_text, int yo, int xo)
384 int const y = y_text - text->first_y + yo;
386 if (((y + row->height()) > 0) &&
387 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
388 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
390 force_clear_ = false;