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"
26 #include "insets/insettext.h"
27 #include "ColorHandler.h"
37 val.foreground = BlackPixel(fl_get_display(),
38 DefaultScreen(fl_get_display()));
40 val.function = GXcopy;
41 val.graphics_exposures = false;
42 val.line_style = LineSolid;
44 return XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
45 GCForeground | GCFunction | GCGraphicsExposures
46 | GCLineWidth | GCLineStyle , &val);
53 LyXScreen::LyXScreen(WorkArea & o)
54 : owner(o), force_clear(true)
56 // the cursor isnt yet visible
57 cursor_visible = false;
69 void LyXScreen::setCursorColor()
71 if (!lyxColorHandler.get()) return;
73 GC gc = lyxColorHandler->getGCForeground(LColor::cursor);
76 XGetGCValues(fl_get_display(),
77 gc, GCForeground, &val);
78 XChangeGC(fl_get_display(), gc_copy, GCForeground, &val);
82 void LyXScreen::Redraw(LyXText * text, BufferView * bv)
84 DrawFromTo(text, bv, 0, owner.height());
85 expose(0, 0, owner.workWidth(), owner.height());
87 cursor_visible = false;
93 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
95 XCopyArea(fl_get_display(),
100 exp_width, exp_height,
106 void LyXScreen::DrawFromTo(LyXText * text, BufferView * bv,
107 int y1, int y2, int y_offset, int x_offset)
109 int y_text = text->first + y1;
111 // get the first needed row
112 Row * row = text->GetRowNearY(y_text);
113 // y_text is now the real beginning of the row
115 int y = y_text - text->first;
116 // y1 is now the real beginning of row on the screen
118 while (row != 0 && y < y2) {
119 LyXText::text_status st = bv->text->status;
121 bv->text->status = st;
122 text->GetVisibleRow(bv, y + y_offset,
123 x_offset, row, y + text->first);
124 } while (bv->text->status == LyXText::CHANGED_IN_DRAW);
125 bv->text->status = st;
131 // maybe we have to clear the screen at the bottom
132 if ((y < y2) && text->bv_owner) {
133 owner.getPainter().fillRectangle(0, y,
141 void LyXScreen::DrawOneRow(LyXText * text, BufferView * bv, Row * row,
142 int y_text, int y_offset, int x_offset)
144 int const y = y_text - text->first + y_offset;
146 if (((y + row->height()) > 0) &&
147 ((y - row->height()) <= static_cast<int>(owner.height()))) {
148 // ok there is something visible
149 LyXText::text_status st = bv->text->status;
151 bv->text->status = st;
152 text->GetVisibleRow(bv, y, x_offset, row,
154 } while (bv->text->status == LyXText::CHANGED_IN_DRAW);
155 bv->text->status = st;
161 /* draws the screen, starting with textposition y. uses as much already
162 * printed pixels as possible */
163 void LyXScreen::Draw(LyXText * text, BufferView * bv, unsigned int y)
165 if (cursor_visible) HideCursor();
167 int const old_first = text->first;
170 // is any optimiziation possible?
171 if ((y - old_first) < owner.height()
172 && (old_first - y) < owner.height()) {
173 if (text->first < old_first) {
174 DrawFromTo(text, bv, 0, old_first - text->first);
175 XCopyArea (fl_get_display(),
182 owner.height() - old_first + text->first,
184 owner.ypos() + old_first - text->first
186 // expose the area drawn
189 old_first - text->first);
192 owner.height() + old_first - text->first,
194 XCopyArea (fl_get_display(),
199 owner.ypos() + text->first - old_first,
201 owner.height() + old_first - text->first,
204 // expose the area drawn
205 expose(0, owner.height() + old_first - text->first,
206 owner.workWidth(), text->first - old_first);
209 // make a dumb new-draw
210 DrawFromTo(text, bv, 0, owner.height());
211 expose(0, 0, owner.workWidth(), owner.height());
216 void LyXScreen::ShowCursor(LyXText const * text, BufferView const * bv)
218 if (!cursor_visible) {
219 Cursor_Shape shape = BAR_SHAPE;
220 if (text->real_current_font.language() !=
221 bv->buffer()->params.language
222 || text->real_current_font.isVisibleRightToLeft()
223 != bv->buffer()->params.language->RightToLeft())
224 shape = (text->real_current_font.isVisibleRightToLeft())
225 ? REVERSED_L_SHAPE : L_SHAPE;
226 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
227 lyxfont::maxAscent(text->real_current_font),
228 lyxfont::maxDescent(text->real_current_font),
234 /* returns true if first has changed, otherwise false */
235 bool LyXScreen::FitManualCursor(LyXText * text, BufferView * bv,
236 int /*x*/, int y, int asc, int desc)
238 int newtop = text->first;
240 if (y + desc - text->first >= static_cast<int>(owner.height()))
241 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
242 else if (y - asc < static_cast<int>(text->first)
243 && text->first > 0) {
244 newtop = y - owner.height() / 4;
247 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
249 if (newtop != static_cast<int>(text->first)) {
250 Draw(text, bv, newtop);
251 text->first = newtop;
258 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
259 int asc, int desc, Cursor_Shape shape)
261 // Update the cursor color.
264 int const y1 = max(y - text->first - asc, 0);
265 int const y_tmp = min(y - text->first + desc,
266 static_cast<int>(owner.height()));
268 // Secure against very strange situations
269 int const y2 = max(y_tmp, y1);
272 XFreePixmap(fl_get_display(), cursor_pixmap);
276 if (y2 > 0 && y1 < int(owner.height())) {
277 cursor_pixmap_h = y2 - y1 + 1;
278 cursor_pixmap_y = y1;
286 cursor_pixmap_w = cursor_pixmap_h/3;
289 case REVERSED_L_SHAPE:
290 cursor_pixmap_w = cursor_pixmap_h/3;
291 cursor_pixmap_x = x - cursor_pixmap_w + 1;
296 XCreatePixmap (fl_get_display(),
300 fl_get_visual_depth());
301 XCopyArea (fl_get_display(),
305 owner.xpos() + cursor_pixmap_x,
306 owner.ypos() + cursor_pixmap_y,
310 XDrawLine(fl_get_display(),
321 case REVERSED_L_SHAPE:
322 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
323 XFillRectangle(fl_get_display(),
326 cursor_pixmap_x + owner.xpos(),
327 y2 - rectangle_h + 1 + owner.ypos(),
328 cursor_pixmap_w - 1, rectangle_h);
333 cursor_visible = true;
337 void LyXScreen::HideCursor()
339 if (!cursor_visible) return;
342 XCopyArea (fl_get_display(),
347 cursor_pixmap_w, cursor_pixmap_h,
348 cursor_pixmap_x + owner.xpos(),
349 cursor_pixmap_y + owner.ypos());
351 cursor_visible = false;
355 void LyXScreen::CursorToggle(LyXText const * text, BufferView const * bv)
360 ShowCursor(text, bv);
364 /* returns a new top so that the cursor is visible */
365 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
367 int newtop = text->first;
370 - text->cursor.row()->baseline()
371 + text->cursor.row()->height()
372 - text->first >= owner.height()) {
373 if (text->cursor.row()->height() < owner.height()
374 && text->cursor.row()->height() > owner.height() / 4)
375 newtop = text->cursor.y()
376 + text->cursor.row()->height()
377 - text->cursor.row()->baseline() - owner.height();
379 newtop = text->cursor.y()
380 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
381 } else if (static_cast<int>((text->cursor.y()) - text->cursor.row()->baseline()) <
382 text->first && text->first > 0)
384 if (text->cursor.row()->height() < owner.height()
385 && text->cursor.row()->height() > owner.height() / 4)
386 newtop = text->cursor.y() - text->cursor.row()->baseline();
388 newtop = text->cursor.y() - owner.height() / 4;
389 newtop = min(newtop, int(text->first));
393 newtop = max(newtop, 0);
399 /* scrolls the screen so that the cursor is visible, if necessary.
400 * returns true if a change was made, otherwise false */
401 bool LyXScreen::FitCursor(LyXText * text, BufferView * bv)
403 // Is a change necessary?
404 int const newtop = TopCursorVisible(text);
405 bool const result = (newtop != text->first);
407 Draw(text, bv, newtop);
412 void LyXScreen::Update(LyXText * text, BufferView * bv,
413 int y_offset, int x_offset)
415 switch (text->status) {
416 case LyXText::NEED_MORE_REFRESH:
418 int const y = max(int(text->refresh_y - text->first), 0);
419 DrawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
421 text->status = LyXText::UNCHANGED;
422 expose(0, y, owner.workWidth(), owner.height() - y);
425 case LyXText::NEED_VERY_LITTLE_REFRESH:
427 // ok I will update the current cursor row
428 DrawOneRow(text, bv, text->refresh_row, text->refresh_y,
430 text->status = LyXText::UNCHANGED;
431 expose(0, text->refresh_y - text->first + y_offset,
432 owner.workWidth(), text->refresh_row->height());
435 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
436 case LyXText::UNCHANGED:
437 // Nothing needs done
443 void LyXScreen::ToggleSelection(LyXText * text, BufferView * bv,
445 int y_offset, int x_offset)
447 // only if there is a selection
448 if (!text->selection) return;
450 int const bottom = min(
451 max(static_cast<int>(text->sel_end_cursor.y()
452 - text->sel_end_cursor.row()->baseline()
453 + text->sel_end_cursor.row()->height()),
455 static_cast<int>(text->first + owner.height()));
457 max(static_cast<int>(text->sel_start_cursor.y() -
458 text->sel_start_cursor.row()->baseline()),
460 static_cast<int>(text->first + owner.height()));
464 DrawFromTo(text, bv, top - text->first, bottom - text->first,
466 expose(0, top - text->first,
468 bottom - text->first - (top - text->first));
472 void LyXScreen::ToggleToggle(LyXText * text, BufferView * bv,
473 int y_offset, int x_offset)
475 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
476 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
479 int const top_tmp = text->toggle_cursor.y()
480 - text->toggle_cursor.row()->baseline();
481 int const bottom_tmp = text->toggle_end_cursor.y()
482 - text->toggle_end_cursor.row()->baseline()
483 + text->toggle_end_cursor.row()->height();
485 int const bottom = min(max(bottom_tmp, text->first),
486 static_cast<int>(text->first + owner.height()));
487 int const top = min(max(top_tmp, text->first),
488 static_cast<int>(text->first + owner.height()));
490 DrawFromTo(text, bv, top - text->first, bottom - text->first, y_offset,
492 expose(0, top - text->first, owner.workWidth(),
493 bottom - text->first - (top - text->first));