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"
35 val.foreground = BlackPixel(fl_display,
36 DefaultScreen(fl_display));
39 val.graphics_exposures = false;
40 val.line_style = LineSolid;
42 return XCreateGC(fl_display, RootWindow(fl_display, 0),
43 GCForeground | GCFunction | GCGraphicsExposures
44 | GCLineWidth | GCLineStyle , &val);
49 LyXScreen::LyXScreen(WorkArea & o) //, LyXText * text_ptr)
50 : owner(o), force_clear(true) //, text(text_ptr)
52 // the cursor isnt yet visible
53 cursor_visible = false;
65 void LyXScreen::Redraw(LyXText * text)
67 DrawFromTo(text, 0, owner.height());
68 expose(0, 0, owner.workWidth(), owner.height());
70 cursor_visible = false;
76 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
83 exp_width, exp_height,
89 void LyXScreen::DrawFromTo(LyXText * text,
90 int y1, int y2, int y_offset, int x_offset)
92 int y_text = text->first + y1;
94 // get the first needed row
95 Row * row = text->GetRowNearY(y_text);
96 // y_text is now the real beginning of the row
98 int y = y_text - text->first;
99 // y1 is now the real beginning of row on the screen
101 while (row != 0 && y < y2) {
102 LyXText::text_status st = text->status;
105 text->GetVisibleRow(owner.owner(), y+y_offset,
106 x_offset, row, y + text->first);
107 } while (text->status == LyXText::CHANGED_IN_DRAW);
114 // maybe we have to clear the screen at the bottom
115 if ((y < y2) && text->bv_owner) {
116 owner.getPainter().fillRectangle(0, y,
124 void LyXScreen::DrawOneRow(LyXText * text, Row * row, int y_text,
125 int y_offset, int x_offset)
127 int y = y_text - text->first + y_offset;
129 if (y + row->height() > 0
130 && y - row->height() <= owner.height()) {
131 // ok there is something visible
132 LyXText::text_status st = text->status;
135 text->GetVisibleRow(owner.owner(), y, x_offset, row,
137 } while (text->status == LyXText::CHANGED_IN_DRAW);
144 /* draws the screen, starting with textposition y. uses as much already
145 * printed pixels as possible */
146 void LyXScreen::Draw(LyXText * text, unsigned int y)
148 if (cursor_visible) HideCursor();
150 unsigned int old_first = text->first;
153 // is any optimiziation possible?
154 if ((y - old_first) < owner.height()
155 && (old_first - y) < owner.height()) {
156 if (text->first < old_first) {
157 DrawFromTo(text, 0, old_first - text->first);
158 XCopyArea (fl_display,
165 owner.height() - old_first + text->first,
167 owner.ypos() + old_first - text->first
169 // expose the area drawn
172 old_first - text->first);
175 owner.height() + old_first - text->first,
177 XCopyArea (fl_display,
182 owner.ypos() + text->first - old_first,
184 owner.height() + old_first - text->first,
187 // expose the area drawn
188 expose(0, owner.height() + old_first - text->first,
189 owner.workWidth(), text->first - old_first);
192 // make a dumb new-draw
193 DrawFromTo(text, 0, owner.height());
194 expose(0, 0, owner.workWidth(), owner.height());
199 void LyXScreen::ShowCursor(LyXText const * text)
201 if (!cursor_visible) {
202 Cursor_Shape shape = BAR_SHAPE;
203 if (text->real_current_font.language() !=
204 owner.owner()->buffer()->params.language_info
205 || text->real_current_font.isVisibleRightToLeft()
206 != owner.owner()->buffer()->params.language_info->RightToLeft())
207 shape = (text->real_current_font.isVisibleRightToLeft())
208 ? REVERSED_L_SHAPE : L_SHAPE;
209 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
210 lyxfont::maxAscent(text->real_current_font),
211 lyxfont::maxDescent(text->real_current_font),
217 /* returns true if first has changed, otherwise false */
218 bool LyXScreen::FitManualCursor(LyXText * text,
219 int /*x*/, int y, int asc, int desc)
221 int newtop = text->first;
223 if (y + desc - text->first >= owner.height())
224 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
225 else if (y - asc < text->first
226 && text->first > 0) {
227 newtop = y - owner.height() / 4;
230 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
232 if (newtop != text->first) {
234 text->first = newtop;
241 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
242 int asc, int desc, Cursor_Shape shape)
244 unsigned int y1 = max(y - text->first - asc, 0U);
245 typedef unsigned int uint;
247 unsigned int y2 = min(y - text->first + desc, owner.height());
249 // Secure against very strange situations
253 XFreePixmap(fl_display, cursor_pixmap);
257 if (y2 > 0 && y1 < owner.height()) {
258 cursor_pixmap_h = y2 - y1 + 1;
259 cursor_pixmap_y = y1;
267 cursor_pixmap_w = cursor_pixmap_h/3;
270 case REVERSED_L_SHAPE:
271 cursor_pixmap_w = cursor_pixmap_h/3;
272 cursor_pixmap_x = x - cursor_pixmap_w + 1;
277 XCreatePixmap (fl_display,
281 fl_get_visual_depth());
282 XCopyArea (fl_display,
286 owner.xpos() + cursor_pixmap_x,
287 owner.ypos() + cursor_pixmap_y,
291 XDrawLine(fl_display,
302 case REVERSED_L_SHAPE:
303 int rectangle_h = (cursor_pixmap_h+10)/20;
304 XFillRectangle(fl_display,
307 cursor_pixmap_x + owner.xpos(),
308 y2 - rectangle_h + 1 + owner.ypos(),
309 cursor_pixmap_w - 1, rectangle_h);
314 cursor_visible = true;
318 void LyXScreen::HideCursor()
320 if (!cursor_visible) return;
323 XCopyArea (fl_display,
328 cursor_pixmap_w, cursor_pixmap_h,
329 cursor_pixmap_x + owner.xpos(),
330 cursor_pixmap_y + owner.ypos());
332 cursor_visible = false;
336 void LyXScreen::CursorToggle(LyXText const * text)
345 /* returns a new top so that the cursor is visible */
346 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
348 int newtop = text->first;
351 - text->cursor.row()->baseline()
352 + text->cursor.row()->height()
353 - text->first >= owner.height()) {
354 if (text->cursor.row()->height() < owner.height()
355 && text->cursor.row()->height() > owner.height() / 4)
356 newtop = text->cursor.y()
357 + text->cursor.row()->height()
358 - text->cursor.row()->baseline() - owner.height();
360 newtop = text->cursor.y()
361 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
362 } else if (text->cursor.y() - text->cursor.row()->baseline() < text->first
363 && text->first > 0) {
364 if (text->cursor.row()->height() < owner.height()
365 && text->cursor.row()->height() > owner.height() / 4)
366 newtop = text->cursor.y() - text->cursor.row()->baseline();
368 newtop = text->cursor.y() - owner.height() / 4;
369 newtop = min(newtop, int(text->first));
373 newtop = max(newtop, 0);
379 /* scrolls the screen so that the cursor is visible, if necessary.
380 * returns true if a change was made, otherwise false */
381 bool LyXScreen::FitCursor(LyXText * text)
383 // Is a change necessary?
384 unsigned int newtop = TopCursorVisible(text);
385 bool result = (newtop != text->first);
392 void LyXScreen::Update(LyXText * text, int y_offset, int x_offset)
394 switch(text->status) {
395 case LyXText::NEED_MORE_REFRESH:
397 int y = max(int(text->refresh_y - text->first), 0);
399 if (text->inset_owner)
400 height = text->inset_owner->ascent(owner.owner(),
401 text->real_current_font)
402 + text->inset_owner->descent(owner.owner(),
403 text->real_current_font);
405 height = owner.height();
406 DrawFromTo(text, y, owner.height(), y_offset, x_offset);
408 text->status = LyXText::UNCHANGED;
410 owner.workWidth(), owner.height() - y);
413 case LyXText::NEED_VERY_LITTLE_REFRESH:
415 // ok I will update the current cursor row
416 DrawOneRow(text, text->refresh_row, text->refresh_y,
418 text->status = LyXText::UNCHANGED;
419 expose(0, text->refresh_y - text->first + y_offset,
420 owner.workWidth(), text->refresh_row->height());
423 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
424 case LyXText::UNCHANGED:
425 // Nothing needs done
431 void LyXScreen::ToggleSelection(LyXText * text, bool kill_selection,
432 int y_offset, int x_offset)
434 // only if there is a selection
435 if (!text->selection) return;
437 int bottom = min(max(text->sel_end_cursor.y()
438 - text->sel_end_cursor.row()->baseline()
439 + text->sel_end_cursor.row()->height(), text->first),
440 text->first + owner.height());
441 int top = min(max(text->sel_start_cursor.y()
442 - text->sel_start_cursor.row()->baseline(), text->first),
443 text->first + owner.height());
447 DrawFromTo(text, top - text->first, bottom - text->first,
449 expose(0, top - text->first,
451 bottom - text->first - (top - text->first));
455 void LyXScreen::ToggleToggle(LyXText * text, int y_offset, int x_offset)
457 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
458 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
461 int top = text->toggle_cursor.y()
462 - text->toggle_cursor.row()->baseline();
463 int bottom = text->toggle_end_cursor.y()
464 - text->toggle_end_cursor.row()->baseline()
465 + text->toggle_end_cursor.row()->height();
467 typedef unsigned int uint;
469 bottom = min(max(uint(bottom), text->first), text->first + owner.height());
470 top = min(max(uint(top), text->first), text->first + owner.height());
472 DrawFromTo(text, top - text->first, bottom - text->first, y_offset,
474 expose(0, top - text->first, owner.workWidth(),
475 bottom - text->first - (top - text->first));