1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1998 The LyX Team
9 * ====================================================== */
14 #pragma implementation "lyxscreen.h"
19 #include "lyxscreen.h"
25 #include "BufferView.h"
27 #include "insets/insettext.h"
28 #include "ColorHandler.h"
39 val.foreground = BlackPixel(fl_get_display(),
40 DefaultScreen(fl_get_display()));
42 val.function = GXcopy;
43 val.graphics_exposures = false;
44 val.line_style = LineSolid;
46 return XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
47 GCForeground | GCFunction | GCGraphicsExposures
48 | GCLineWidth | GCLineStyle , &val);
55 LyXScreen::LyXScreen(WorkArea & o)
56 : owner(o), force_clear(true)
58 // the cursor isnt yet visible
59 cursor_visible = false;
71 LyXScreen::~LyXScreen()
73 XFreeGC(fl_get_display(), gc_copy);
77 void LyXScreen::setCursorColor()
79 if (!lyxColorHandler.get()) return;
81 GC gc = lyxColorHandler->getGCForeground(LColor::cursor);
84 XGetGCValues(fl_get_display(),
85 gc, GCForeground, &val);
86 XChangeGC(fl_get_display(), gc_copy, GCForeground, &val);
90 void LyXScreen::redraw(LyXText * text, BufferView * bv)
92 drawFromTo(text, bv, 0, owner.height(), 0, 0, text == bv->text);
93 expose(0, 0, owner.workWidth(), owner.height());
95 cursor_visible = false;
101 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
103 XCopyArea(fl_get_display(),
108 exp_width, exp_height,
114 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
115 int y1, int y2, int y_offset, int x_offset,
118 int y_text = text->first + y1;
120 // get the first needed row
121 Row * row = text->getRowNearY(y_text);
122 // y_text is now the real beginning of the row
124 int y = y_text - text->first;
125 // y1 is now the real beginning of row on the screen
127 while (row != 0 && y < y2) {
128 LyXText::text_status st = text->status();
129 text->getVisibleRow(bv, y + y_offset,
130 x_offset, row, y + text->first);
131 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
132 while(internal && text->status() == LyXText::CHANGED_IN_DRAW) {
133 if (text->fullRebreak(bv)) {
134 st = LyXText::NEED_MORE_REFRESH;
135 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
137 text->status(bv, st);
138 text->getVisibleRow(bv, y + y_offset,
139 x_offset, row, y + text->first);
146 // maybe we have to clear the screen at the bottom
147 if ((y < y2) && text->bv_owner) {
148 owner.getPainter().fillRectangle(0, y,
156 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
157 int y_text, int y_offset, int x_offset)
159 int const y = y_text - text->first + y_offset;
161 if (((y + row->height()) > 0) &&
162 ((y - row->height()) <= static_cast<int>(owner.height()))) {
163 // ok there is something visible
165 LyXText::text_status st = bv->text->status();
167 bv->text->status(bv, st);
168 text->getVisibleRow(bv, y, x_offset, row,
170 } while (!text->inset_owner &&
171 text->status() == LyXText::CHANGED_IN_DRAW);
172 bv->text->status(bv, st);
174 text->getVisibleRow(bv, y, x_offset, row, y + text->first);
181 /* draws the screen, starting with textposition y. uses as much already
182 * printed pixels as possible */
183 void LyXScreen::draw(LyXText * text, BufferView * bv, unsigned int y)
185 if (cursor_visible) hideCursor();
187 int const old_first = text->first;
188 bool internal = (text == bv->text);
191 // is any optimiziation possible?
192 if ((y - old_first) < owner.height()
193 && (old_first - y) < owner.height())
195 if (text->first < old_first) {
196 drawFromTo(text, bv, 0, old_first - text->first, 0, 0, internal);
197 XCopyArea (fl_get_display(),
204 owner.height() - old_first + text->first,
206 owner.ypos() + old_first - text->first
208 // expose the area drawn
211 old_first - text->first);
214 owner.height() + old_first - text->first,
215 owner.height(), 0, 0, internal);
216 XCopyArea (fl_get_display(),
221 owner.ypos() + text->first - old_first,
223 owner.height() + old_first - text->first,
226 // expose the area drawn
227 expose(0, owner.height() + old_first - text->first,
228 owner.workWidth(), text->first - old_first);
231 // make a dumb new-draw
232 drawFromTo(text, bv, 0, owner.height(), 0, 0, internal);
233 expose(0, 0, owner.workWidth(), owner.height());
238 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
240 if (!cursor_visible) {
241 Cursor_Shape shape = BAR_SHAPE;
242 if (text->real_current_font.language() !=
243 bv->buffer()->params.language
244 || text->real_current_font.isVisibleRightToLeft()
245 != bv->buffer()->params.language->RightToLeft())
246 shape = (text->real_current_font.isVisibleRightToLeft())
247 ? REVERSED_L_SHAPE : L_SHAPE;
248 showManualCursor(text, text->cursor.x(), text->cursor.y(),
249 lyxfont::maxAscent(text->real_current_font),
250 lyxfont::maxDescent(text->real_current_font),
256 /* returns true if first has changed, otherwise false */
257 bool LyXScreen::fitManualCursor(LyXText * text, BufferView * bv,
258 int /*x*/, int y, int asc, int desc)
260 int newtop = text->first;
262 if (y + desc - text->first >= static_cast<int>(owner.height()))
263 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
264 else if (y - asc < static_cast<int>(text->first)
265 && text->first > 0) {
266 newtop = y - owner.height() / 4;
269 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
271 if (newtop != static_cast<int>(text->first)) {
272 draw(text, bv, newtop);
273 text->first = newtop;
280 void LyXScreen::showManualCursor(LyXText const * text, int x, int y,
281 int asc, int desc, Cursor_Shape shape)
283 // Update the cursor color.
286 int const y1 = max(y - text->first - asc, 0);
287 int const y_tmp = min(y - text->first + desc,
288 static_cast<int>(owner.height()));
290 // Secure against very strange situations
291 int const y2 = max(y_tmp, y1);
294 XFreePixmap(fl_get_display(), cursor_pixmap);
298 if (y2 > 0 && y1 < int(owner.height())) {
299 cursor_pixmap_h = y2 - y1 + 1;
300 cursor_pixmap_y = y1;
308 cursor_pixmap_w = cursor_pixmap_h/3;
311 case REVERSED_L_SHAPE:
312 cursor_pixmap_w = cursor_pixmap_h/3;
313 cursor_pixmap_x = x - cursor_pixmap_w + 1;
318 XCreatePixmap (fl_get_display(),
322 fl_get_visual_depth());
323 XCopyArea (fl_get_display(),
327 owner.xpos() + cursor_pixmap_x,
328 owner.ypos() + cursor_pixmap_y,
332 XDrawLine(fl_get_display(),
343 case REVERSED_L_SHAPE:
344 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
345 XFillRectangle(fl_get_display(),
348 cursor_pixmap_x + owner.xpos(),
349 y2 - rectangle_h + 1 + owner.ypos(),
350 cursor_pixmap_w - 1, rectangle_h);
355 cursor_visible = true;
359 void LyXScreen::hideCursor()
361 if (!cursor_visible) return;
364 XCopyArea (fl_get_display(),
369 cursor_pixmap_w, cursor_pixmap_h,
370 cursor_pixmap_x + owner.xpos(),
371 cursor_pixmap_y + owner.ypos());
373 cursor_visible = false;
377 void LyXScreen::cursorToggle(BufferView * bv) const
386 /* returns a new top so that the cursor is visible */
387 unsigned int LyXScreen::topCursorVisible(LyXText const * text)
389 int newtop = text->first;
392 - text->cursor.row()->baseline()
393 + text->cursor.row()->height()
394 - text->first >= owner.height()) {
395 if (text->cursor.row()->height() < owner.height()
396 && text->cursor.row()->height() > owner.height() / 4)
397 newtop = text->cursor.y()
398 + text->cursor.row()->height()
399 - text->cursor.row()->baseline() - owner.height();
401 newtop = text->cursor.y()
402 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
403 } else if (static_cast<int>((text->cursor.y()) - text->cursor.row()->baseline()) <
404 text->first && text->first > 0)
406 if (text->cursor.row()->height() < owner.height()
407 && text->cursor.row()->height() > owner.height() / 4)
408 newtop = text->cursor.y() - text->cursor.row()->baseline();
410 newtop = text->cursor.y() - owner.height() / 4;
411 newtop = min(newtop, int(text->first));
415 newtop = max(newtop, 0);
421 /* scrolls the screen so that the cursor is visible, if necessary.
422 * returns true if a change was made, otherwise false */
423 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
425 // Is a change necessary?
426 int const newtop = topCursorVisible(text);
427 bool const result = (newtop != text->first);
429 draw(text, bv, newtop);
434 void LyXScreen::update(LyXText * text, BufferView * bv,
435 int y_offset, int x_offset)
437 switch (text->status()) {
438 case LyXText::NEED_MORE_REFRESH:
440 int const y = max(int(text->refresh_y - text->first), 0);
441 drawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
443 // otherwise this is called ONLY from BufferView_pimpl(update)
444 // or we should see to set this flag accordingly
445 if (text != bv->text)
446 text->status(bv, LyXText::UNCHANGED);
447 expose(0, y, owner.workWidth(), owner.height() - y);
450 case LyXText::NEED_VERY_LITTLE_REFRESH:
452 // ok I will update the current cursor row
453 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
455 // this because if we had a major update the refresh_row could
456 // have been set to 0!
457 if (text->refresh_row) {
458 // otherwise this is called ONLY from BufferView_pimpl(update)
459 // or we should see to set this flag accordingly
460 if (text != bv->text)
461 text->status(bv, LyXText::UNCHANGED);
462 expose(0, text->refresh_y - text->first + y_offset,
463 owner.workWidth(), text->refresh_row->height());
467 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
468 case LyXText::UNCHANGED:
469 // Nothing needs done
475 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
477 int y_offset, int x_offset)
479 // only if there is a selection
480 if (!text->selection.set()) return;
482 int const bottom = min(
483 max(static_cast<int>(text->selection.end.y()
484 - text->selection.end.row()->baseline()
485 + text->selection.end.row()->height()),
487 static_cast<int>(text->first + owner.height()));
489 max(static_cast<int>(text->selection.start.y() -
490 text->selection.start.row()->baseline()),
492 static_cast<int>(text->first + owner.height()));
495 text->selection.set(false);
496 drawFromTo(text, bv, top - text->first, bottom - text->first,
498 expose(0, top - text->first,
500 bottom - text->first - (top - text->first));
504 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
505 int y_offset, int x_offset)
507 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
508 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
511 int const top_tmp = text->toggle_cursor.y()
512 - text->toggle_cursor.row()->baseline();
513 int const bottom_tmp = text->toggle_end_cursor.y()
514 - text->toggle_end_cursor.row()->baseline()
515 + text->toggle_end_cursor.row()->height();
517 int const bottom = min(max(bottom_tmp, text->first),
518 static_cast<int>(text->first + owner.height()));
519 int const top = min(max(top_tmp, text->first),
520 static_cast<int>(text->first + owner.height()));
522 drawFromTo(text, bv, top - text->first, bottom - text->first, y_offset,
524 expose(0, top - text->first, owner.workWidth(),
525 bottom - text->first - (top - text->first));