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"
38 val.foreground = BlackPixel(fl_get_display(),
39 DefaultScreen(fl_get_display()));
41 val.function = GXcopy;
42 val.graphics_exposures = false;
43 val.line_style = LineSolid;
45 return XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
46 GCForeground | GCFunction | GCGraphicsExposures
47 | GCLineWidth | GCLineStyle , &val);
54 LyXScreen::LyXScreen(WorkArea & o)
55 : owner(o), force_clear(true)
57 // the cursor isnt yet visible
58 cursor_visible = false;
70 void LyXScreen::setCursorColor()
72 if (!lyxColorHandler.get()) return;
74 GC gc = lyxColorHandler->getGCForeground(LColor::cursor);
77 XGetGCValues(fl_get_display(),
78 gc, GCForeground, &val);
79 XChangeGC(fl_get_display(), gc_copy, GCForeground, &val);
83 void LyXScreen::Redraw(LyXText * text, BufferView * bv)
85 DrawFromTo(text, bv, 0, owner.height());
86 expose(0, 0, owner.workWidth(), owner.height());
88 cursor_visible = false;
94 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
96 XCopyArea(fl_get_display(),
101 exp_width, exp_height,
107 void LyXScreen::DrawFromTo(LyXText * text, BufferView * bv,
108 int y1, int y2, int y_offset, int x_offset)
110 int y_text = text->first + y1;
112 // get the first needed row
113 Row * row = text->GetRowNearY(y_text);
114 // y_text is now the real beginning of the row
116 int y = y_text - text->first;
117 // y1 is now the real beginning of row on the screen
119 while (row != 0 && y < y2) {
120 LyXText::text_status st = bv->text->status;
122 bv->text->status = st;
123 text->GetVisibleRow(bv, y + y_offset,
124 x_offset, row, y + text->first);
125 } while (bv->text->status == LyXText::CHANGED_IN_DRAW);
126 bv->text->status = st;
132 // maybe we have to clear the screen at the bottom
133 if ((y < y2) && text->bv_owner) {
134 owner.getPainter().fillRectangle(0, y,
142 void LyXScreen::DrawOneRow(LyXText * text, BufferView * bv, Row * row,
143 int y_text, int y_offset, int x_offset)
145 int const y = y_text - text->first + y_offset;
147 if (((y + row->height()) > 0) &&
148 ((y - row->height()) <= static_cast<int>(owner.height()))) {
149 // ok there is something visible
150 LyXText::text_status st = bv->text->status;
152 bv->text->status = st;
153 text->GetVisibleRow(bv, y, x_offset, row,
155 } while (bv->text->status == LyXText::CHANGED_IN_DRAW);
156 bv->text->status = st;
162 /* draws the screen, starting with textposition y. uses as much already
163 * printed pixels as possible */
164 void LyXScreen::Draw(LyXText * text, BufferView * bv, unsigned int y)
166 if (cursor_visible) HideCursor();
168 int const old_first = text->first;
171 // is any optimiziation possible?
172 if ((y - old_first) < owner.height()
173 && (old_first - y) < owner.height()) {
174 if (text->first < old_first) {
175 DrawFromTo(text, bv, 0, old_first - text->first);
176 XCopyArea (fl_get_display(),
183 owner.height() - old_first + text->first,
185 owner.ypos() + old_first - text->first
187 // expose the area drawn
190 old_first - text->first);
193 owner.height() + old_first - text->first,
195 XCopyArea (fl_get_display(),
200 owner.ypos() + text->first - old_first,
202 owner.height() + old_first - text->first,
205 // expose the area drawn
206 expose(0, owner.height() + old_first - text->first,
207 owner.workWidth(), text->first - old_first);
210 // make a dumb new-draw
211 DrawFromTo(text, bv, 0, owner.height());
212 expose(0, 0, owner.workWidth(), owner.height());
217 void LyXScreen::ShowCursor(LyXText const * text, BufferView const * bv)
219 if (!cursor_visible) {
220 Cursor_Shape shape = BAR_SHAPE;
221 if (text->real_current_font.language() !=
222 bv->buffer()->params.language
223 || text->real_current_font.isVisibleRightToLeft()
224 != bv->buffer()->params.language->RightToLeft())
225 shape = (text->real_current_font.isVisibleRightToLeft())
226 ? REVERSED_L_SHAPE : L_SHAPE;
227 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
228 lyxfont::maxAscent(text->real_current_font),
229 lyxfont::maxDescent(text->real_current_font),
235 /* returns true if first has changed, otherwise false */
236 bool LyXScreen::FitManualCursor(LyXText * text, BufferView * bv,
237 int /*x*/, int y, int asc, int desc)
239 int newtop = text->first;
241 if (y + desc - text->first >= static_cast<int>(owner.height()))
242 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
243 else if (y - asc < static_cast<int>(text->first)
244 && text->first > 0) {
245 newtop = y - owner.height() / 4;
248 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
250 if (newtop != static_cast<int>(text->first)) {
251 Draw(text, bv, newtop);
252 text->first = newtop;
259 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
260 int asc, int desc, Cursor_Shape shape)
262 // Update the cursor color.
265 int const y1 = max(y - text->first - asc, 0);
266 int const y_tmp = min(y - text->first + desc,
267 static_cast<int>(owner.height()));
269 // Secure against very strange situations
270 int const y2 = max(y_tmp, y1);
273 XFreePixmap(fl_get_display(), cursor_pixmap);
277 if (y2 > 0 && y1 < int(owner.height())) {
278 cursor_pixmap_h = y2 - y1 + 1;
279 cursor_pixmap_y = y1;
287 cursor_pixmap_w = cursor_pixmap_h/3;
290 case REVERSED_L_SHAPE:
291 cursor_pixmap_w = cursor_pixmap_h/3;
292 cursor_pixmap_x = x - cursor_pixmap_w + 1;
297 XCreatePixmap (fl_get_display(),
301 fl_get_visual_depth());
302 XCopyArea (fl_get_display(),
306 owner.xpos() + cursor_pixmap_x,
307 owner.ypos() + cursor_pixmap_y,
311 XDrawLine(fl_get_display(),
322 case REVERSED_L_SHAPE:
323 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
324 XFillRectangle(fl_get_display(),
327 cursor_pixmap_x + owner.xpos(),
328 y2 - rectangle_h + 1 + owner.ypos(),
329 cursor_pixmap_w - 1, rectangle_h);
334 cursor_visible = true;
338 void LyXScreen::HideCursor()
340 if (!cursor_visible) return;
343 XCopyArea (fl_get_display(),
348 cursor_pixmap_w, cursor_pixmap_h,
349 cursor_pixmap_x + owner.xpos(),
350 cursor_pixmap_y + owner.ypos());
352 cursor_visible = false;
356 void LyXScreen::CursorToggle(LyXText const * text, BufferView const * bv)
361 ShowCursor(text, bv);
365 /* returns a new top so that the cursor is visible */
366 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
368 int newtop = text->first;
371 - text->cursor.row()->baseline()
372 + text->cursor.row()->height()
373 - text->first >= owner.height()) {
374 if (text->cursor.row()->height() < owner.height()
375 && text->cursor.row()->height() > owner.height() / 4)
376 newtop = text->cursor.y()
377 + text->cursor.row()->height()
378 - text->cursor.row()->baseline() - owner.height();
380 newtop = text->cursor.y()
381 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
382 } else if (static_cast<int>((text->cursor.y()) - text->cursor.row()->baseline()) <
383 text->first && text->first > 0)
385 if (text->cursor.row()->height() < owner.height()
386 && text->cursor.row()->height() > owner.height() / 4)
387 newtop = text->cursor.y() - text->cursor.row()->baseline();
389 newtop = text->cursor.y() - owner.height() / 4;
390 newtop = min(newtop, int(text->first));
394 newtop = max(newtop, 0);
400 /* scrolls the screen so that the cursor is visible, if necessary.
401 * returns true if a change was made, otherwise false */
402 bool LyXScreen::FitCursor(LyXText * text, BufferView * bv)
404 // Is a change necessary?
405 int const newtop = TopCursorVisible(text);
406 bool const result = (newtop != text->first);
408 Draw(text, bv, newtop);
413 void LyXScreen::Update(LyXText * text, BufferView * bv,
414 int y_offset, int x_offset)
416 switch (text->status) {
417 case LyXText::NEED_MORE_REFRESH:
419 int const y = max(int(text->refresh_y - text->first), 0);
420 DrawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
422 text->status = LyXText::UNCHANGED;
423 expose(0, y, owner.workWidth(), owner.height() - y);
426 case LyXText::NEED_VERY_LITTLE_REFRESH:
428 // ok I will update the current cursor row
429 DrawOneRow(text, bv, text->refresh_row, text->refresh_y,
431 text->status = LyXText::UNCHANGED;
432 expose(0, text->refresh_y - text->first + y_offset,
433 owner.workWidth(), text->refresh_row->height());
436 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
437 case LyXText::UNCHANGED:
438 // Nothing needs done
444 void LyXScreen::ToggleSelection(LyXText * text, BufferView * bv,
446 int y_offset, int x_offset)
448 // only if there is a selection
449 if (!text->selection.set()) return;
451 int const bottom = min(
452 max(static_cast<int>(text->selection.end.y()
453 - text->selection.end.row()->baseline()
454 + text->selection.end.row()->height()),
456 static_cast<int>(text->first + owner.height()));
458 max(static_cast<int>(text->selection.start.y() -
459 text->selection.start.row()->baseline()),
461 static_cast<int>(text->first + owner.height()));
464 text->selection.set(false);
465 DrawFromTo(text, bv, top - text->first, bottom - text->first,
467 expose(0, top - text->first,
469 bottom - text->first - (top - text->first));
473 void LyXScreen::ToggleToggle(LyXText * text, BufferView * bv,
474 int y_offset, int x_offset)
476 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
477 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
480 int const top_tmp = text->toggle_cursor.y()
481 - text->toggle_cursor.row()->baseline();
482 int const bottom_tmp = text->toggle_end_cursor.y()
483 - text->toggle_end_cursor.row()->baseline()
484 + text->toggle_end_cursor.row()->height();
486 int const bottom = min(max(bottom_tmp, text->first),
487 static_cast<int>(text->first + owner.height()));
488 int const top = min(max(top_tmp, text->first),
489 static_cast<int>(text->first + owner.height()));
491 DrawFromTo(text, bv, top - text->first, bottom - text->first, y_offset,
493 expose(0, top - text->first, owner.workWidth(),
494 bottom - text->first - (top - text->first));