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(LyXText const * text)
400 int newtop = text->first_y;
402 Row * row = text->cursor.row();
404 // Is this a hack? Yes, probably... (Lgb)
406 return max(newtop, 0);
408 if (text->cursor.y() - row->baseline() + row->height()
409 - text->first_y >= owner.workHeight()) {
410 if (row->height() < owner.workHeight()
411 && row->height() > owner.workHeight() / 4) {
412 newtop = text->cursor.y()
414 - row->baseline() - owner.workHeight();
417 newtop = text->cursor.y()
418 - owner.workHeight() / 2; /* the scroll region must be so big!! */
421 } else if (static_cast<int>((text->cursor.y()) - row->baseline()) <
422 text->first_y && text->first_y > 0) {
423 if (row->height() < owner.workHeight()
424 && row->height() > owner.workHeight() / 4) {
425 newtop = text->cursor.y() - row->baseline();
428 newtop = text->cursor.y() - owner.workHeight() / 2;
429 newtop = min(newtop, text->first_y);
433 newtop = max(newtop, 0);
439 /* scrolls the screen so that the cursor is visible, if necessary.
440 * returns true if a change was made, otherwise false */
441 bool LScreen::fitCursor(LyXText * text, BufferView * bv)
443 // Is a change necessary?
444 int const newtop = topCursorVisible(text);
445 bool const result = (newtop != text->first_y);
447 draw(text, bv, newtop);
452 void LScreen::update(LyXText * text, BufferView * bv,
453 int y_offset, int x_offset)
455 switch (text->status()) {
456 case LyXText::NEED_MORE_REFRESH:
458 int const y = max(int(text->refresh_y - text->first_y), 0);
459 drawFromTo(text, bv, y, owner.workHeight(), y_offset, x_offset);
461 // otherwise this is called ONLY from BufferView_pimpl(update)
462 // or we should see to set this flag accordingly
463 if (text != bv->text)
464 text->status(bv, LyXText::UNCHANGED);
465 expose(0, y, owner.workWidth(), owner.workHeight() - y);
468 case LyXText::NEED_VERY_LITTLE_REFRESH:
470 // ok I will update the current cursor row
471 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
473 // this because if we had a major update the refresh_row could
474 // have been set to 0!
475 if (text->refresh_row) {
476 // otherwise this is called ONLY from BufferView_pimpl(update)
477 // or we should see to set this flag accordingly
478 if (text != bv->text)
479 text->status(bv, LyXText::UNCHANGED);
480 expose(0, text->refresh_y - text->first_y + y_offset,
481 owner.workWidth(), text->refresh_row->height());
485 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
486 case LyXText::UNCHANGED:
487 // Nothing needs done
493 void LScreen::toggleSelection(LyXText * text, BufferView * bv,
495 int y_offset, int x_offset)
497 // only if there is a selection
498 if (!text->selection.set()) return;
500 int const bottom = min(
501 max(static_cast<int>(text->selection.end.y()
502 - text->selection.end.row()->baseline()
503 + text->selection.end.row()->height()),
505 static_cast<int>(text->first_y + owner.workHeight()));
507 max(static_cast<int>(text->selection.start.y() -
508 text->selection.start.row()->baseline()),
510 static_cast<int>(text->first_y + owner.workHeight()));
513 text->selection.set(false);
514 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
516 expose(0, top - text->first_y,
518 bottom - text->first_y - (top - text->first_y));
522 void LScreen::toggleToggle(LyXText * text, BufferView * bv,
523 int y_offset, int x_offset)
525 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
526 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
529 int const top_tmp = text->toggle_cursor.y()
530 - text->toggle_cursor.row()->baseline();
531 int const bottom_tmp = text->toggle_end_cursor.y()
532 - text->toggle_end_cursor.row()->baseline()
533 + text->toggle_end_cursor.row()->height();
535 int const offset = y_offset < 0 ? y_offset : 0;
536 int const bottom = min(max(bottom_tmp, text->first_y),
537 static_cast<int>(text->first_y + owner.workHeight()))-offset;
538 int const top = min(max(top_tmp, text->first_y),
539 static_cast<int>(text->first_y + owner.workHeight()))-offset;
541 drawFromTo(text, bv, top - text->first_y,
542 bottom - text->first_y, y_offset,
544 expose(0, top - text->first_y, owner.workWidth(),
545 bottom - text->first_y - (top - text->first_y));