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
22 #include "frontends/Painter.h"
23 #include "frontends/WorkArea.h"
25 #include "BufferView.h"
26 #include "font_metrics.h"
27 #include "insets/insettext.h"
28 #include "frontends/xforms/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 LScreen::LScreen(WorkArea & o)
56 : owner(o), force_clear(true)
58 // the cursor isnt yet visible
59 cursor_visible = false;
73 XFreeGC(fl_get_display(), gc_copy);
79 XFreeGC(fl_get_display(), gc_copy);
80 // the cursor isnt yet visible
81 cursor_visible = false;
93 void LScreen::setCursorColor()
95 if (!lyxColorHandler.get()) return;
97 GC gc = lyxColorHandler->getGCForeground(LColor::cursor);
100 XGetGCValues(fl_get_display(),
101 gc, GCForeground, &val);
102 XChangeGC(fl_get_display(), gc_copy, GCForeground, &val);
106 void LScreen::redraw(LyXText * text, BufferView * bv)
108 drawFromTo(text, bv, 0, owner.workHeight(), 0, 0, text == bv->text);
109 expose(0, 0, owner.workWidth(), owner.workHeight());
110 if (cursor_visible) {
111 cursor_visible = false;
117 void LScreen::expose(int x, int y, int exp_width, int exp_height)
119 XCopyArea(fl_get_display(),
124 exp_width, exp_height,
130 void LScreen::drawFromTo(LyXText * text, BufferView * bv,
131 int y1, int y2, int y_offset, int x_offset,
134 int y_text = text->first_y + y1;
136 // get the first needed row
137 Row * row = text->getRowNearY(y_text);
138 // y_text is now the real beginning of the row
140 int y = y_text - text->first_y;
141 // y1 is now the real beginning of row on the screen
143 while (row != 0 && y < y2) {
144 LyXText::text_status st = text->status();
145 text->getVisibleRow(bv, y + y_offset,
146 x_offset, row, y + text->first_y);
147 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
148 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
149 text->fullRebreak(bv);
150 st = LyXText::NEED_MORE_REFRESH;
151 text->setCursor(bv, text->cursor.par(),
153 text->status(bv, st);
154 // we should be sure our row-pointer is still valid, so it's
155 // better to recompute it.
156 y_text = y + text->first_y;
157 row = text->getRowNearY(y_text);
158 y = y_text - text->first_y;
159 text->getVisibleRow(bv, y + y_offset,
160 x_offset, row, y + text->first_y);
167 // maybe we have to clear the screen at the bottom
168 if ((y < y2) && text->bv_owner) {
169 owner.getPainter().fillRectangle(0, y,
177 void LScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
178 int y_text, int y_offset, int x_offset)
180 int const y = y_text - text->first_y + y_offset;
182 if (((y + row->height()) > 0) &&
183 ((y - row->height()) <= static_cast<int>(owner.workHeight()))) {
184 // ok there is something visible
185 text->getVisibleRow(bv, y, x_offset, row, y + text->first_y);
191 /* draws the screen, starting with textposition y. uses as much already
192 * printed pixels as possible */
193 void LScreen::draw(LyXText * text, BufferView * bv, unsigned int y)
195 if (cursor_visible) hideCursor();
197 int const old_first = text->first_y;
198 bool internal = (text == bv->text);
201 // is any optimiziation possible?
202 if ((y - old_first) < owner.workHeight()
203 && (old_first - y) < owner.workHeight())
205 if (text->first_y < old_first) {
206 drawFromTo(text, bv, 0,
207 old_first - text->first_y, 0, 0, internal);
208 XCopyArea (fl_get_display(),
215 owner.workHeight() - old_first + text->first_y,
217 owner.ypos() + old_first - text->first_y
219 // expose the area drawn
222 old_first - text->first_y);
225 owner.workHeight() + old_first - text->first_y,
226 owner.workHeight(), 0, 0, internal);
227 XCopyArea (fl_get_display(),
232 owner.ypos() + text->first_y - old_first,
234 owner.workHeight() + old_first - text->first_y,
237 // expose the area drawn
238 expose(0, owner.workHeight() + old_first - text->first_y,
239 owner.workWidth(), text->first_y - old_first);
242 // make a dumb new-draw
243 drawFromTo(text, bv, 0, owner.workHeight(), 0, 0, internal);
244 expose(0, 0, owner.workWidth(), owner.workHeight());
249 void LScreen::showCursor(LyXText const * text, BufferView const * bv)
251 if (!cursor_visible) {
252 Cursor_Shape shape = BAR_SHAPE;
253 if (text->real_current_font.language() !=
254 bv->buffer()->params.language
255 || text->real_current_font.isVisibleRightToLeft()
256 != bv->buffer()->params.language->RightToLeft())
257 shape = (text->real_current_font.isVisibleRightToLeft())
258 ? REVERSED_L_SHAPE : L_SHAPE;
259 showManualCursor(text, text->cursor.x(), text->cursor.y(),
260 font_metrics::maxAscent(text->real_current_font),
261 font_metrics::maxDescent(text->real_current_font),
267 /* returns true if first has changed, otherwise false */
268 bool LScreen::fitManualCursor(LyXText * text, BufferView * bv,
269 int /*x*/, int y, int asc, int desc)
271 int newtop = text->first_y;
273 if (y + desc - text->first_y >= static_cast<int>(owner.workHeight()))
274 newtop = y - 3 * owner.workHeight() / 4; // the scroll region must be so big!!
275 else if (y - asc < text->first_y
276 && text->first_y > 0) {
277 newtop = y - owner.workHeight() / 4;
280 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
282 if (newtop != text->first_y) {
283 draw(text, bv, newtop);
284 text->first_y = newtop;
291 void LScreen::showManualCursor(LyXText const * text, int x, int y,
292 int asc, int desc, Cursor_Shape shape)
294 // Update the cursor color.
297 int const y1 = max(y - text->first_y - asc, 0);
298 int const y_tmp = min(y - text->first_y + desc,
299 static_cast<int>(owner.workHeight()));
301 // Secure against very strange situations
302 int const y2 = max(y_tmp, y1);
305 XFreePixmap(fl_get_display(), cursor_pixmap);
309 if (y2 > 0 && y1 < int(owner.workHeight())) {
310 cursor_pixmap_h = y2 - y1 + 1;
311 cursor_pixmap_y = y1;
319 cursor_pixmap_w = cursor_pixmap_h/3;
322 case REVERSED_L_SHAPE:
323 cursor_pixmap_w = cursor_pixmap_h/3;
324 cursor_pixmap_x = x - cursor_pixmap_w + 1;
329 XCreatePixmap (fl_get_display(),
333 fl_get_visual_depth());
334 XCopyArea (fl_get_display(),
338 owner.xpos() + cursor_pixmap_x,
339 owner.ypos() + cursor_pixmap_y,
343 XDrawLine(fl_get_display(),
354 case REVERSED_L_SHAPE:
355 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
356 XFillRectangle(fl_get_display(),
359 cursor_pixmap_x + owner.xpos(),
360 y2 - rectangle_h + 1 + owner.ypos(),
361 cursor_pixmap_w - 1, rectangle_h);
366 cursor_visible = true;
370 void LScreen::hideCursor()
372 if (!cursor_visible) return;
375 XCopyArea (fl_get_display(),
380 cursor_pixmap_w, cursor_pixmap_h,
381 cursor_pixmap_x + owner.xpos(),
382 cursor_pixmap_y + owner.ypos());
384 cursor_visible = false;
388 void LScreen::cursorToggle(BufferView * bv) const
397 /* returns a new top so that the cursor is visible */
398 unsigned int LScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
400 int const vheight = owner.workHeight();
403 Row * row = cursor.row();
405 // Is this a hack? Yes, probably... (Lgb)
407 return max(newtop, 0);
409 if (cursor.y() - row->baseline() + row->height()
410 - top_y >= vheight) {
411 if (row->height() < vheight
412 && row->height() > vheight / 4) {
415 - row->baseline() - vheight;
419 - vheight / 2; /* the scroll region must be so big!! */
422 } else if (static_cast<int>(cursor.y() - row->baseline()) <
423 top_y && top_y > 0) {
424 if (row->height() < vheight
425 && row->height() > vheight / 4) {
426 newtop = cursor.y() - row->baseline();
429 newtop = cursor.y() - vheight / 2;
430 newtop = min(newtop, top_y);
434 newtop = max(newtop, 0);
440 /* scrolls the screen so that the cursor is visible, if necessary.
441 * returns true if a change was made, otherwise false */
442 bool LScreen::fitCursor(LyXText * text, BufferView * bv)
444 // Is a change necessary?
445 int const newtop = topCursorVisible(text->cursor, text->first_y);
446 bool const result = (newtop != text->first_y);
448 draw(text, bv, newtop);
453 void LScreen::update(LyXText * text, BufferView * bv,
454 int y_offset, int x_offset)
456 switch (text->status()) {
457 case LyXText::NEED_MORE_REFRESH:
459 int const y = max(int(text->refresh_y - text->first_y), 0);
460 drawFromTo(text, bv, y, owner.workHeight(), y_offset, x_offset);
462 // otherwise this is called ONLY from BufferView_pimpl(update)
463 // or we should see to set this flag accordingly
464 if (text != bv->text)
465 text->status(bv, LyXText::UNCHANGED);
466 expose(0, y, owner.workWidth(), owner.workHeight() - y);
469 case LyXText::NEED_VERY_LITTLE_REFRESH:
471 // ok I will update the current cursor row
472 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
474 // this because if we had a major update the refresh_row could
475 // have been set to 0!
476 if (text->refresh_row) {
477 // otherwise this is called ONLY from BufferView_pimpl(update)
478 // or we should see to set this flag accordingly
479 if (text != bv->text)
480 text->status(bv, LyXText::UNCHANGED);
481 expose(0, text->refresh_y - text->first_y + y_offset,
482 owner.workWidth(), text->refresh_row->height());
486 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
487 case LyXText::UNCHANGED:
488 // Nothing needs done
494 void LScreen::toggleSelection(LyXText * text, BufferView * bv,
496 int y_offset, int x_offset)
498 // only if there is a selection
499 if (!text->selection.set()) return;
501 int const bottom = min(
502 max(static_cast<int>(text->selection.end.y()
503 - text->selection.end.row()->baseline()
504 + text->selection.end.row()->height()),
506 static_cast<int>(text->first_y + owner.workHeight()));
508 max(static_cast<int>(text->selection.start.y() -
509 text->selection.start.row()->baseline()),
511 static_cast<int>(text->first_y + owner.workHeight()));
514 text->selection.set(false);
515 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
517 expose(0, top - text->first_y,
519 bottom - text->first_y - (top - text->first_y));
523 void LScreen::toggleToggle(LyXText * text, BufferView * bv,
524 int y_offset, int x_offset)
526 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
527 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
530 int const top_tmp = text->toggle_cursor.y()
531 - text->toggle_cursor.row()->baseline();
532 int const bottom_tmp = text->toggle_end_cursor.y()
533 - text->toggle_end_cursor.row()->baseline()
534 + text->toggle_end_cursor.row()->height();
536 int const offset = y_offset < 0 ? y_offset : 0;
537 int const bottom = min(max(bottom_tmp, text->first_y),
538 static_cast<int>(text->first_y + owner.workHeight()))-offset;
539 int const top = min(max(top_tmp, text->first_y),
540 static_cast<int>(text->first_y + owner.workHeight()))-offset;
542 drawFromTo(text, bv, top - text->first_y,
543 bottom - text->first_y, y_offset,
545 expose(0, top - text->first_y, owner.workWidth(),
546 bottom - text->first_y - (top - text->first_y));