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"
36 val.foreground = BlackPixel(fl_get_display(),
37 DefaultScreen(fl_get_display()));
39 val.function = GXcopy;
40 val.graphics_exposures = false;
41 val.line_style = LineSolid;
43 return XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0),
44 GCForeground | GCFunction | GCGraphicsExposures
45 | GCLineWidth | GCLineStyle , &val);
50 LyXScreen::LyXScreen(WorkArea & o)
51 : owner(o), force_clear(true)
53 // the cursor isnt yet visible
54 cursor_visible = false;
66 void LyXScreen::setCursorColor()
68 if (!lyxColorHandler) return;
70 GC gc = lyxColorHandler->getGCForeground(LColor::cursor);
73 XGetGCValues(fl_get_display(),
74 gc, GCForeground, &val);
75 XChangeGC(fl_get_display(), gc_copy, GCForeground, &val);
79 void LyXScreen::Redraw(LyXText * text, BufferView * bv)
81 DrawFromTo(text, bv, 0, owner.height());
82 expose(0, 0, owner.workWidth(), owner.height());
84 cursor_visible = false;
90 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
92 XCopyArea(fl_get_display(),
97 exp_width, exp_height,
103 void LyXScreen::DrawFromTo(LyXText * text, BufferView * bv,
104 int y1, int y2, int y_offset, int x_offset)
106 int y_text = text->first + y1;
108 // get the first needed row
109 Row * row = text->GetRowNearY(y_text);
110 // y_text is now the real beginning of the row
112 int y = y_text - text->first;
113 // y1 is now the real beginning of row on the screen
115 while (row != 0 && y < y2) {
116 LyXText::text_status st = text->status;
120 text->GetVisibleRow(owner.owner(), y + y_offset,
121 x_offset, row, y + text->first);
123 text->GetVisibleRow(bv, y + y_offset,
124 x_offset, row, y + text->first);
126 } while (text->status == LyXText::CHANGED_IN_DRAW);
133 // maybe we have to clear the screen at the bottom
134 if ((y < y2) && text->bv_owner) {
135 owner.getPainter().fillRectangle(0, y,
143 void LyXScreen::DrawOneRow(LyXText * text, BufferView * bv, Row * row,
144 int y_text, int y_offset, int x_offset)
146 int const y = y_text - text->first + y_offset;
148 if (((y + row->height()) > 0) &&
149 ((y - row->height()) <= static_cast<int>(owner.height()))) {
150 // ok there is something visible
151 LyXText::text_status st = text->status;
155 text->GetVisibleRow(owner.owner(), y, x_offset, row,
158 text->GetVisibleRow(bv, y, x_offset, row,
161 } while (text->status == LyXText::CHANGED_IN_DRAW);
168 /* draws the screen, starting with textposition y. uses as much already
169 * printed pixels as possible */
170 void LyXScreen::Draw(LyXText * text, BufferView * bv, unsigned int y)
172 if (cursor_visible) HideCursor();
174 int const old_first = text->first;
177 // is any optimiziation possible?
178 if ((y - old_first) < owner.height()
179 && (old_first - y) < owner.height()) {
180 if (text->first < old_first) {
181 DrawFromTo(text, bv, 0, old_first - text->first);
182 XCopyArea (fl_get_display(),
189 owner.height() - old_first + text->first,
191 owner.ypos() + old_first - text->first
193 // expose the area drawn
196 old_first - text->first);
199 owner.height() + old_first - text->first,
201 XCopyArea (fl_get_display(),
206 owner.ypos() + text->first - old_first,
208 owner.height() + old_first - text->first,
211 // expose the area drawn
212 expose(0, owner.height() + old_first - text->first,
213 owner.workWidth(), text->first - old_first);
216 // make a dumb new-draw
217 DrawFromTo(text, bv, 0, owner.height());
218 expose(0, 0, owner.workWidth(), owner.height());
223 void LyXScreen::ShowCursor(LyXText const * text, BufferView const * bv)
225 if (!cursor_visible) {
226 Cursor_Shape shape = BAR_SHAPE;
228 if (text->real_current_font.language() !=
229 owner.owner()->buffer()->params.language
230 || text->real_current_font.isVisibleRightToLeft()
231 != owner.owner()->buffer()->params.language->RightToLeft())
233 if (text->real_current_font.language() !=
234 bv->buffer()->params.language
235 || text->real_current_font.isVisibleRightToLeft()
236 != bv->buffer()->params.language->RightToLeft())
238 shape = (text->real_current_font.isVisibleRightToLeft())
239 ? REVERSED_L_SHAPE : L_SHAPE;
240 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
241 lyxfont::maxAscent(text->real_current_font),
242 lyxfont::maxDescent(text->real_current_font),
248 /* returns true if first has changed, otherwise false */
249 bool LyXScreen::FitManualCursor(LyXText * text, BufferView * bv,
250 int /*x*/, int y, int asc, int desc)
252 int newtop = text->first;
254 if (y + desc - text->first >= static_cast<int>(owner.height()))
255 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
256 else if (y - asc < static_cast<int>(text->first)
257 && text->first > 0) {
258 newtop = y - owner.height() / 4;
261 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
263 if (newtop != static_cast<int>(text->first)) {
264 Draw(text, bv, newtop);
265 text->first = newtop;
272 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
273 int asc, int desc, Cursor_Shape shape)
275 // Update the cursor color.
278 int const y1 = max(y - text->first - asc, 0);
279 int const y_tmp = min(y - text->first + desc,
280 static_cast<int>(owner.height()));
282 // Secure against very strange situations
283 int const y2 = max(y_tmp, y1);
286 XFreePixmap(fl_get_display(), cursor_pixmap);
290 if (y2 > 0 && y1 < int(owner.height())) {
291 cursor_pixmap_h = y2 - y1 + 1;
292 cursor_pixmap_y = y1;
300 cursor_pixmap_w = cursor_pixmap_h/3;
303 case REVERSED_L_SHAPE:
304 cursor_pixmap_w = cursor_pixmap_h/3;
305 cursor_pixmap_x = x - cursor_pixmap_w + 1;
310 XCreatePixmap (fl_get_display(),
314 fl_get_visual_depth());
315 XCopyArea (fl_get_display(),
319 owner.xpos() + cursor_pixmap_x,
320 owner.ypos() + cursor_pixmap_y,
324 XDrawLine(fl_get_display(),
335 case REVERSED_L_SHAPE:
336 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
337 XFillRectangle(fl_get_display(),
340 cursor_pixmap_x + owner.xpos(),
341 y2 - rectangle_h + 1 + owner.ypos(),
342 cursor_pixmap_w - 1, rectangle_h);
347 cursor_visible = true;
351 void LyXScreen::HideCursor()
353 if (!cursor_visible) return;
356 XCopyArea (fl_get_display(),
361 cursor_pixmap_w, cursor_pixmap_h,
362 cursor_pixmap_x + owner.xpos(),
363 cursor_pixmap_y + owner.ypos());
365 cursor_visible = false;
369 void LyXScreen::CursorToggle(LyXText const * text, BufferView const * bv)
374 ShowCursor(text, bv);
378 /* returns a new top so that the cursor is visible */
379 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
381 int newtop = text->first;
384 - text->cursor.row()->baseline()
385 + text->cursor.row()->height()
386 - text->first >= owner.height()) {
387 if (text->cursor.row()->height() < owner.height()
388 && text->cursor.row()->height() > owner.height() / 4)
389 newtop = text->cursor.y()
390 + text->cursor.row()->height()
391 - text->cursor.row()->baseline() - owner.height();
393 newtop = text->cursor.y()
394 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
395 } else if (static_cast<int>((text->cursor.y()) - text->cursor.row()->baseline()) <
396 text->first && text->first > 0)
398 if (text->cursor.row()->height() < owner.height()
399 && text->cursor.row()->height() > owner.height() / 4)
400 newtop = text->cursor.y() - text->cursor.row()->baseline();
402 newtop = text->cursor.y() - owner.height() / 4;
403 newtop = min(newtop, int(text->first));
407 newtop = max(newtop, 0);
413 /* scrolls the screen so that the cursor is visible, if necessary.
414 * returns true if a change was made, otherwise false */
415 bool LyXScreen::FitCursor(LyXText * text, BufferView * bv)
417 // Is a change necessary?
418 int const newtop = TopCursorVisible(text);
419 bool const result = (newtop != text->first);
421 Draw(text, bv, newtop);
426 void LyXScreen::Update(LyXText * text, BufferView * bv,
427 int y_offset, int x_offset)
429 switch (text->status) {
430 case LyXText::NEED_MORE_REFRESH:
432 int const y = max(int(text->refresh_y - text->first), 0);
433 DrawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
435 text->status = LyXText::UNCHANGED;
436 expose(0, y, owner.workWidth(), owner.height() - y);
439 case LyXText::NEED_VERY_LITTLE_REFRESH:
441 // ok I will update the current cursor row
442 DrawOneRow(text, bv, text->refresh_row, text->refresh_y,
444 text->status = LyXText::UNCHANGED;
445 expose(0, text->refresh_y - text->first + y_offset,
446 owner.workWidth(), text->refresh_row->height());
449 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
450 case LyXText::UNCHANGED:
451 // Nothing needs done
457 void LyXScreen::ToggleSelection(LyXText * text, BufferView * bv,
459 int y_offset, int x_offset)
461 // only if there is a selection
462 if (!text->selection) return;
464 int const bottom = min(
465 max(static_cast<int>(text->sel_end_cursor.y()
466 - text->sel_end_cursor.row()->baseline()
467 + text->sel_end_cursor.row()->height()),
469 static_cast<int>(text->first + owner.height()));
471 max(static_cast<int>(text->sel_start_cursor.y() -
472 text->sel_start_cursor.row()->baseline()),
474 static_cast<int>(text->first + owner.height()));
478 DrawFromTo(text, bv, top - text->first, bottom - text->first,
480 expose(0, top - text->first,
482 bottom - text->first - (top - text->first));
486 void LyXScreen::ToggleToggle(LyXText * text, BufferView * bv,
487 int y_offset, int x_offset)
489 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
490 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
493 int const top_tmp = text->toggle_cursor.y()
494 - text->toggle_cursor.row()->baseline();
495 int const bottom_tmp = text->toggle_end_cursor.y()
496 - text->toggle_end_cursor.row()->baseline()
497 + text->toggle_end_cursor.row()->height();
499 int const bottom = min(max(bottom_tmp, text->first),
500 static_cast<int>(text->first + owner.height()));
501 int const top = min(max(top_tmp, text->first),
502 static_cast<int>(text->first + owner.height()));
504 DrawFromTo(text, bv, top - text->first, bottom - text->first, y_offset,
506 expose(0, top - text->first, owner.workWidth(),
507 bottom - text->first - (top - text->first));