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 text->fullRebreak(bv);
134 st = LyXText::NEED_MORE_REFRESH;
135 text->setCursor(bv, text->cursor.par(),
137 text->status(bv, st);
138 // we should be sure our row-pointer is still valid, so it's
139 // better to recompute it.
140 y_text = y + text->first_y;
141 row = text->getRowNearY(y_text);
142 y = y_text - text->first_y;
143 text->getVisibleRow(bv, y + y_offset,
144 x_offset, row, y + text->first_y);
151 // maybe we have to clear the screen at the bottom
152 if ((y < y2) && text->bv_owner) {
153 owner.getPainter().fillRectangle(0, y,
161 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
162 int y_text, int y_offset, int x_offset)
164 int const y = y_text - text->first_y + y_offset;
166 if (((y + row->height()) > 0) &&
167 ((y - row->height()) <= static_cast<int>(owner.height()))) {
168 // ok there is something visible
169 text->getVisibleRow(bv, y, x_offset, row, y + text->first_y);
175 /* draws the screen, starting with textposition y. uses as much already
176 * printed pixels as possible */
177 void LyXScreen::draw(LyXText * text, BufferView * bv, unsigned int y)
179 if (cursor_visible) hideCursor();
181 int const old_first = text->first_y;
182 bool internal = (text == bv->text);
185 // is any optimiziation possible?
186 if ((y - old_first) < owner.height()
187 && (old_first - y) < owner.height())
189 if (text->first_y < old_first) {
190 drawFromTo(text, bv, 0,
191 old_first - text->first_y, 0, 0, internal);
192 XCopyArea (fl_get_display(),
199 owner.height() - old_first + text->first_y,
201 owner.ypos() + old_first - text->first_y
203 // expose the area drawn
206 old_first - text->first_y);
209 owner.height() + old_first - text->first_y,
210 owner.height(), 0, 0, internal);
211 XCopyArea (fl_get_display(),
216 owner.ypos() + text->first_y - old_first,
218 owner.height() + old_first - text->first_y,
221 // expose the area drawn
222 expose(0, owner.height() + old_first - text->first_y,
223 owner.workWidth(), text->first_y - old_first);
226 // make a dumb new-draw
227 drawFromTo(text, bv, 0, owner.height(), 0, 0, internal);
228 expose(0, 0, owner.workWidth(), owner.height());
233 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
235 if (!cursor_visible) {
236 Cursor_Shape shape = BAR_SHAPE;
237 if (text->real_current_font.language() !=
238 bv->buffer()->params.language
239 || text->real_current_font.isVisibleRightToLeft()
240 != bv->buffer()->params.language->RightToLeft())
241 shape = (text->real_current_font.isVisibleRightToLeft())
242 ? REVERSED_L_SHAPE : L_SHAPE;
243 showManualCursor(text, text->cursor.x(), text->cursor.y(),
244 lyxfont::maxAscent(text->real_current_font),
245 lyxfont::maxDescent(text->real_current_font),
251 /* returns true if first has changed, otherwise false */
252 bool LyXScreen::fitManualCursor(LyXText * text, BufferView * bv,
253 int /*x*/, int y, int asc, int desc)
255 int newtop = text->first_y;
257 if (y + desc - text->first_y >= static_cast<int>(owner.height()))
258 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
259 else if (y - asc < text->first_y
260 && text->first_y > 0) {
261 newtop = y - owner.height() / 4;
264 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
266 if (newtop != text->first_y) {
267 draw(text, bv, newtop);
268 text->first_y = newtop;
275 void LyXScreen::showManualCursor(LyXText const * text, int x, int y,
276 int asc, int desc, Cursor_Shape shape)
278 // Update the cursor color.
281 int const y1 = max(y - text->first_y - asc, 0);
282 int const y_tmp = min(y - text->first_y + desc,
283 static_cast<int>(owner.height()));
285 // Secure against very strange situations
286 int const y2 = max(y_tmp, y1);
289 XFreePixmap(fl_get_display(), cursor_pixmap);
293 if (y2 > 0 && y1 < int(owner.height())) {
294 cursor_pixmap_h = y2 - y1 + 1;
295 cursor_pixmap_y = y1;
303 cursor_pixmap_w = cursor_pixmap_h/3;
306 case REVERSED_L_SHAPE:
307 cursor_pixmap_w = cursor_pixmap_h/3;
308 cursor_pixmap_x = x - cursor_pixmap_w + 1;
313 XCreatePixmap (fl_get_display(),
317 fl_get_visual_depth());
318 XCopyArea (fl_get_display(),
322 owner.xpos() + cursor_pixmap_x,
323 owner.ypos() + cursor_pixmap_y,
327 XDrawLine(fl_get_display(),
338 case REVERSED_L_SHAPE:
339 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
340 XFillRectangle(fl_get_display(),
343 cursor_pixmap_x + owner.xpos(),
344 y2 - rectangle_h + 1 + owner.ypos(),
345 cursor_pixmap_w - 1, rectangle_h);
350 cursor_visible = true;
354 void LyXScreen::hideCursor()
356 if (!cursor_visible) return;
359 XCopyArea (fl_get_display(),
364 cursor_pixmap_w, cursor_pixmap_h,
365 cursor_pixmap_x + owner.xpos(),
366 cursor_pixmap_y + owner.ypos());
368 cursor_visible = false;
372 void LyXScreen::cursorToggle(BufferView * bv) const
381 /* returns a new top so that the cursor is visible */
382 unsigned int LyXScreen::topCursorVisible(LyXText const * text)
384 int newtop = text->first_y;
386 Row * row = text->cursor.row();
388 // Is this a hack? Yes, probably... (Lgb)
390 return max(newtop, 0);
392 if (text->cursor.y() - row->baseline() + row->height()
393 - text->first_y >= owner.height()) {
394 if (row->height() < owner.height()
395 && row->height() > owner.height() / 4) {
396 newtop = text->cursor.y()
398 - row->baseline() - owner.height();
401 newtop = text->cursor.y()
402 - owner.height() / 2; /* the scroll region must be so big!! */
405 } else if (static_cast<int>((text->cursor.y()) - row->baseline()) <
406 text->first_y && text->first_y > 0) {
407 if (row->height() < owner.height()
408 && row->height() > owner.height() / 4) {
409 newtop = text->cursor.y() - row->baseline();
412 newtop = text->cursor.y() - owner.height() / 2;
413 newtop = min(newtop, text->first_y);
417 newtop = max(newtop, 0);
423 /* scrolls the screen so that the cursor is visible, if necessary.
424 * returns true if a change was made, otherwise false */
425 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
427 // Is a change necessary?
428 int const newtop = topCursorVisible(text);
429 bool const result = (newtop != text->first_y);
431 draw(text, bv, newtop);
436 void LyXScreen::update(LyXText * text, BufferView * bv,
437 int y_offset, int x_offset)
439 switch (text->status()) {
440 case LyXText::NEED_MORE_REFRESH:
442 int const y = max(int(text->refresh_y - text->first_y), 0);
443 drawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
445 // otherwise this is called ONLY from BufferView_pimpl(update)
446 // or we should see to set this flag accordingly
447 if (text != bv->text)
448 text->status(bv, LyXText::UNCHANGED);
449 expose(0, y, owner.workWidth(), owner.height() - y);
452 case LyXText::NEED_VERY_LITTLE_REFRESH:
454 // ok I will update the current cursor row
455 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
457 // this because if we had a major update the refresh_row could
458 // have been set to 0!
459 if (text->refresh_row) {
460 // otherwise this is called ONLY from BufferView_pimpl(update)
461 // or we should see to set this flag accordingly
462 if (text != bv->text)
463 text->status(bv, LyXText::UNCHANGED);
464 expose(0, text->refresh_y - text->first_y + y_offset,
465 owner.workWidth(), text->refresh_row->height());
469 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
470 case LyXText::UNCHANGED:
471 // Nothing needs done
477 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
479 int y_offset, int x_offset)
481 // only if there is a selection
482 if (!text->selection.set()) return;
484 int const bottom = min(
485 max(static_cast<int>(text->selection.end.y()
486 - text->selection.end.row()->baseline()
487 + text->selection.end.row()->height()),
489 static_cast<int>(text->first_y + owner.height()));
491 max(static_cast<int>(text->selection.start.y() -
492 text->selection.start.row()->baseline()),
494 static_cast<int>(text->first_y + owner.height()));
497 text->selection.set(false);
498 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
500 expose(0, top - text->first_y,
502 bottom - text->first_y - (top - text->first_y));
506 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
507 int y_offset, int x_offset)
509 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
510 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
513 int const top_tmp = text->toggle_cursor.y()
514 - text->toggle_cursor.row()->baseline();
515 int const bottom_tmp = text->toggle_end_cursor.y()
516 - text->toggle_end_cursor.row()->baseline()
517 + text->toggle_end_cursor.row()->height();
519 int const offset = y_offset < 0 ? y_offset : 0;
520 int const bottom = min(max(bottom_tmp, text->first_y),
521 static_cast<int>(text->first_y + owner.height()))-offset;
522 int const top = min(max(top_tmp, text->first_y),
523 static_cast<int>(text->first_y + owner.height()))-offset;
525 drawFromTo(text, bv, top - text->first_y,
526 bottom - text->first_y, y_offset,
528 expose(0, top - text->first_y, owner.workWidth(),
529 bottom - text->first_y - (top - text->first_y));