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_y + 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_y;
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_y);
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_y);
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 + 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_y);
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_y;
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_y < old_first) {
196 drawFromTo(text, bv, 0,
197 old_first - text->first_y, 0, 0, internal);
198 XCopyArea (fl_get_display(),
205 owner.height() - old_first + text->first_y,
207 owner.ypos() + old_first - text->first_y
209 // expose the area drawn
212 old_first - text->first_y);
215 owner.height() + old_first - text->first_y,
216 owner.height(), 0, 0, internal);
217 XCopyArea (fl_get_display(),
222 owner.ypos() + text->first_y - old_first,
224 owner.height() + old_first - text->first_y,
227 // expose the area drawn
228 expose(0, owner.height() + old_first - text->first_y,
229 owner.workWidth(), text->first_y - old_first);
232 // make a dumb new-draw
233 drawFromTo(text, bv, 0, owner.height(), 0, 0, internal);
234 expose(0, 0, owner.workWidth(), owner.height());
239 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
241 if (!cursor_visible) {
242 Cursor_Shape shape = BAR_SHAPE;
243 if (text->real_current_font.language() !=
244 bv->buffer()->params.language
245 || text->real_current_font.isVisibleRightToLeft()
246 != bv->buffer()->params.language->RightToLeft())
247 shape = (text->real_current_font.isVisibleRightToLeft())
248 ? REVERSED_L_SHAPE : L_SHAPE;
249 showManualCursor(text, text->cursor.x(), text->cursor.y(),
250 lyxfont::maxAscent(text->real_current_font),
251 lyxfont::maxDescent(text->real_current_font),
257 /* returns true if first has changed, otherwise false */
258 bool LyXScreen::fitManualCursor(LyXText * text, BufferView * bv,
259 int /*x*/, int y, int asc, int desc)
261 int newtop = text->first_y;
263 if (y + desc - text->first_y >= static_cast<int>(owner.height()))
264 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
265 else if (y - asc < text->first_y
266 && text->first_y > 0) {
267 newtop = y - owner.height() / 4;
270 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
272 if (newtop != text->first_y) {
273 draw(text, bv, newtop);
274 text->first_y = newtop;
281 void LyXScreen::showManualCursor(LyXText const * text, int x, int y,
282 int asc, int desc, Cursor_Shape shape)
284 // Update the cursor color.
287 int const y1 = max(y - text->first_y - asc, 0);
288 int const y_tmp = min(y - text->first_y + desc,
289 static_cast<int>(owner.height()));
291 // Secure against very strange situations
292 int const y2 = max(y_tmp, y1);
295 XFreePixmap(fl_get_display(), cursor_pixmap);
299 if (y2 > 0 && y1 < int(owner.height())) {
300 cursor_pixmap_h = y2 - y1 + 1;
301 cursor_pixmap_y = y1;
309 cursor_pixmap_w = cursor_pixmap_h/3;
312 case REVERSED_L_SHAPE:
313 cursor_pixmap_w = cursor_pixmap_h/3;
314 cursor_pixmap_x = x - cursor_pixmap_w + 1;
319 XCreatePixmap (fl_get_display(),
323 fl_get_visual_depth());
324 XCopyArea (fl_get_display(),
328 owner.xpos() + cursor_pixmap_x,
329 owner.ypos() + cursor_pixmap_y,
333 XDrawLine(fl_get_display(),
344 case REVERSED_L_SHAPE:
345 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
346 XFillRectangle(fl_get_display(),
349 cursor_pixmap_x + owner.xpos(),
350 y2 - rectangle_h + 1 + owner.ypos(),
351 cursor_pixmap_w - 1, rectangle_h);
356 cursor_visible = true;
360 void LyXScreen::hideCursor()
362 if (!cursor_visible) return;
365 XCopyArea (fl_get_display(),
370 cursor_pixmap_w, cursor_pixmap_h,
371 cursor_pixmap_x + owner.xpos(),
372 cursor_pixmap_y + owner.ypos());
374 cursor_visible = false;
378 void LyXScreen::cursorToggle(BufferView * bv) const
387 /* returns a new top so that the cursor is visible */
388 unsigned int LyXScreen::topCursorVisible(LyXText const * text)
390 int newtop = text->first_y;
392 Row * row = text->cursor.row();
394 // Is this a hack? Yes, probably... (Lgb)
396 return max(newtop, 0);
398 if (text->cursor.y() - row->baseline() + row->height()
399 - text->first_y >= owner.height()) {
400 if (row->height() < owner.height()
401 && row->height() > owner.height() / 4) {
402 newtop = text->cursor.y()
404 - row->baseline() - owner.height();
407 newtop = text->cursor.y()
408 - owner.height() / 2; /* the scroll region must be so big!! */
411 } else if (static_cast<int>((text->cursor.y()) - row->baseline()) <
412 text->first_y && text->first_y > 0) {
413 if (row->height() < owner.height()
414 && row->height() > owner.height() / 4) {
415 newtop = text->cursor.y() - row->baseline();
418 newtop = text->cursor.y() - owner.height() / 2;
419 newtop = min(newtop, text->first_y);
423 newtop = max(newtop, 0);
429 /* scrolls the screen so that the cursor is visible, if necessary.
430 * returns true if a change was made, otherwise false */
431 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
433 // Is a change necessary?
434 int const newtop = topCursorVisible(text);
435 bool const result = (newtop != text->first_y);
437 draw(text, bv, newtop);
442 void LyXScreen::update(LyXText * text, BufferView * bv,
443 int y_offset, int x_offset)
445 switch (text->status()) {
446 case LyXText::NEED_MORE_REFRESH:
448 int const y = max(int(text->refresh_y - text->first_y), 0);
449 drawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
451 // otherwise this is called ONLY from BufferView_pimpl(update)
452 // or we should see to set this flag accordingly
453 if (text != bv->text)
454 text->status(bv, LyXText::UNCHANGED);
455 expose(0, y, owner.workWidth(), owner.height() - y);
458 case LyXText::NEED_VERY_LITTLE_REFRESH:
460 // ok I will update the current cursor row
461 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
463 // this because if we had a major update the refresh_row could
464 // have been set to 0!
465 if (text->refresh_row) {
466 // otherwise this is called ONLY from BufferView_pimpl(update)
467 // or we should see to set this flag accordingly
468 if (text != bv->text)
469 text->status(bv, LyXText::UNCHANGED);
470 expose(0, text->refresh_y - text->first_y + y_offset,
471 owner.workWidth(), text->refresh_row->height());
475 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
476 case LyXText::UNCHANGED:
477 // Nothing needs done
483 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
485 int y_offset, int x_offset)
487 // only if there is a selection
488 if (!text->selection.set()) return;
490 int const bottom = min(
491 max(static_cast<int>(text->selection.end.y()
492 - text->selection.end.row()->baseline()
493 + text->selection.end.row()->height()),
495 static_cast<int>(text->first_y + owner.height()));
497 max(static_cast<int>(text->selection.start.y() -
498 text->selection.start.row()->baseline()),
500 static_cast<int>(text->first_y + owner.height()));
503 text->selection.set(false);
504 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
506 expose(0, top - text->first_y,
508 bottom - text->first_y - (top - text->first_y));
512 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
513 int y_offset, int x_offset)
515 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
516 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
519 int const top_tmp = text->toggle_cursor.y()
520 - text->toggle_cursor.row()->baseline();
521 int const bottom_tmp = text->toggle_end_cursor.y()
522 - text->toggle_end_cursor.row()->baseline()
523 + text->toggle_end_cursor.row()->height();
525 int const offset = y_offset < 0 ? y_offset : 0;
526 int const bottom = min(max(bottom_tmp, text->first_y),
527 static_cast<int>(text->first_y + owner.height()))-offset;
528 int const top = min(max(top_tmp, text->first_y),
529 static_cast<int>(text->first_y + owner.height()))-offset;
531 drawFromTo(text, bv, top - text->first_y,
532 bottom - text->first_y, y_offset,
534 expose(0, top - text->first_y, owner.workWidth(),
535 bottom - text->first_y - (top - text->first_y));