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) //, 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, int y1, int y2, int y_offset, int x_offset)
91 long y_text = text->first + y1;
93 // get the first needed row
94 Row * row = text->GetRowNearY(y_text);
95 // y_text is now the real beginning of the row
97 long y = y_text - text->first;
98 // y1 is now the real beginning of row on the screen
100 while (row != 0 && y < y2) {
101 LyXText::text_status st = text->status;
104 text->GetVisibleRow(owner.owner(), y+y_offset,
105 x_offset, row, y + text->first);
106 } while (text->status == LyXText::CHANGED_IN_DRAW);
112 // maybe we have to clear the screen at the bottom
113 if ((y < y2) && text->bv_owner) {
114 owner.getPainter().fillRectangle(0, y,
122 void LyXScreen::DrawOneRow(LyXText * text, Row * row, long y_text,
123 int y_offset, int x_offset)
125 long y = y_text - text->first + y_offset;
127 if (y + row->height() > 0
128 && y - row->height() <= long(owner.height())) {
129 // ok there is something visible
130 LyXText::text_status st = text->status;
133 text->GetVisibleRow(owner.owner(), y, x_offset, row,
135 } while (text->status == LyXText::CHANGED_IN_DRAW);
141 /* draws the screen, starting with textposition y. uses as much already
142 * printed pixels as possible */
143 void LyXScreen::Draw(LyXText * text, unsigned long y)
145 if (cursor_visible) HideCursor();
147 unsigned long old_first = text->first;
150 // is any optimiziation possible?
151 if ((y - old_first) < owner.height()
152 && (old_first - y) < owner.height()) {
153 if (text->first < old_first) {
154 DrawFromTo(text, 0, old_first - text->first);
155 XCopyArea (fl_display,
162 owner.height() - old_first + text->first,
164 owner.ypos() + old_first - text->first
166 // expose the area drawn
169 old_first - text->first);
172 owner.height() + old_first - text->first,
174 XCopyArea (fl_display,
179 owner.ypos() + text->first - old_first,
181 owner.height() + old_first - text->first,
184 // expose the area drawn
185 expose(0, owner.height() + old_first - text->first,
186 owner.workWidth(), text->first - old_first);
189 // make a dumb new-draw
190 DrawFromTo(text, 0, owner.height());
191 expose(0, 0, owner.workWidth(), owner.height());
196 void LyXScreen::ShowCursor(LyXText const * text)
198 if (!cursor_visible) {
199 Cursor_Shape shape = BAR_SHAPE;
200 if (text->real_current_font.language() !=
201 owner.owner()->buffer()->params.language_info
202 || text->real_current_font.isVisibleRightToLeft()
203 != owner.owner()->buffer()->params.language_info->RightToLeft())
204 shape = (text->real_current_font.isVisibleRightToLeft())
205 ? REVERSED_L_SHAPE : L_SHAPE;
206 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
207 lyxfont::maxAscent(text->real_current_font),
208 lyxfont::maxDescent(text->real_current_font),
214 /* returns true if first has changed, otherwise false */
215 bool LyXScreen::FitManualCursor(LyXText * text,
216 long /*x*/, long y, int asc, int desc)
218 long newtop = text->first;
220 if (y + desc - text->first >= owner.height())
221 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
222 else if (y - asc < long(text->first)
223 && text->first > 0) {
224 newtop = y - owner.height() / 4;
227 newtop = max(newtop, 0L); // can newtop ever be < 0? (Lgb)
229 if (newtop != long(text->first)) {
231 text->first = newtop;
238 void LyXScreen::ShowManualCursor(LyXText const * text, long x, long y,
239 int asc, int desc, Cursor_Shape shape)
241 unsigned long y1 = max(y - text->first - asc, 0UL);
242 typedef unsigned long ulong;
244 unsigned long y2 = min(y - text->first + desc, ulong(owner.height()));
246 // Secure against very strange situations
250 XFreePixmap(fl_display, cursor_pixmap);
254 if (y2 > 0 && y1 < owner.height()) {
255 cursor_pixmap_h = y2 - y1 + 1;
256 cursor_pixmap_y = y1;
264 cursor_pixmap_w = cursor_pixmap_h/3;
267 case REVERSED_L_SHAPE:
268 cursor_pixmap_w = cursor_pixmap_h/3;
269 cursor_pixmap_x = x - cursor_pixmap_w + 1;
274 XCreatePixmap (fl_display,
278 fl_get_visual_depth());
279 XCopyArea (fl_display,
283 owner.xpos() + cursor_pixmap_x,
284 owner.ypos() + cursor_pixmap_y,
288 XDrawLine(fl_display,
299 case REVERSED_L_SHAPE:
300 int rectangle_h = (cursor_pixmap_h+10)/20;
301 XFillRectangle(fl_display,
304 cursor_pixmap_x + owner.xpos(),
305 y2 - rectangle_h + 1 + owner.ypos(),
306 cursor_pixmap_w - 1, rectangle_h);
311 cursor_visible = true;
315 void LyXScreen::HideCursor()
317 if (!cursor_visible) return;
320 XCopyArea (fl_display,
325 cursor_pixmap_w, cursor_pixmap_h,
326 cursor_pixmap_x + owner.xpos(),
327 cursor_pixmap_y + owner.ypos());
329 cursor_visible = false;
333 void LyXScreen::CursorToggle(LyXText const * text)
342 /* returns a new top so that the cursor is visible */
343 unsigned long LyXScreen::TopCursorVisible(LyXText const * text)
345 long newtop = text->first;
348 - text->cursor.row()->baseline()
349 + text->cursor.row()->height()
350 - text->first >= owner.height()) {
351 if (text->cursor.row()->height() < owner.height()
352 && text->cursor.row()->height() > owner.height() / 4)
353 newtop = text->cursor.y()
354 + text->cursor.row()->height()
355 - text->cursor.row()->baseline() - owner.height();
357 newtop = text->cursor.y()
358 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
359 } else if (text->cursor.y() - text->cursor.row()->baseline() < text->first
360 && text->first > 0) {
361 if (text->cursor.row()->height() < owner.height()
362 && text->cursor.row()->height() > owner.height() / 4)
363 newtop = text->cursor.y() - text->cursor.row()->baseline();
365 newtop = text->cursor.y() - owner.height() / 4;
366 newtop = min(newtop, long(text->first));
370 newtop = max(newtop, 0L);
376 /* scrolls the screen so that the cursor is visible, if necessary.
377 * returns true if a change was made, otherwise false */
378 bool LyXScreen::FitCursor(LyXText * text)
380 // Is a change necessary?
381 unsigned long newtop = TopCursorVisible(text);
382 bool result = (newtop != text->first);
389 void LyXScreen::Update(LyXText * text, int y_offset, int x_offset)
391 switch(text->status) {
392 case LyXText::NEED_MORE_REFRESH:
394 long y = max(text->refresh_y - long(text->first), 0L);
396 if (text->inset_owner)
397 height = text->inset_owner->ascent(owner.owner(),
398 text->real_current_font)
399 + text->inset_owner->descent(owner.owner(),
400 text->real_current_font);
402 height = owner.height();
403 DrawFromTo(text, y, owner.height(), y_offset, x_offset);
405 text->status = LyXText::UNCHANGED;
407 owner.workWidth(), owner.height() - y);
410 case LyXText::NEED_VERY_LITTLE_REFRESH:
412 // ok I will update the current cursor row
413 DrawOneRow(text, text->refresh_row, text->refresh_y,
415 text->status = LyXText::UNCHANGED;
416 expose(0, text->refresh_y - text->first + y_offset,
417 owner.workWidth(), text->refresh_row->height());
420 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
421 case LyXText::UNCHANGED:
422 // Nothing needs done
428 void LyXScreen::ToggleSelection(LyXText * text, int y_offset, int x_offset,
431 // only if there is a selection
432 if (!text->selection) return;
434 long bottom = min(max(text->sel_end_cursor.y()
435 - text->sel_end_cursor.row()->baseline()
436 + text->sel_end_cursor.row()->height(), text->first),
437 text->first + owner.height());
438 long top = min(max(text->sel_start_cursor.y()
439 - text->sel_start_cursor.row()->baseline(), text->first),
440 text->first + owner.height());
444 DrawFromTo(text, top - text->first, bottom - text->first,
446 expose(0, top - text->first,
448 bottom - text->first - (top - text->first));
452 void LyXScreen::ToggleToggle(LyXText * text, int y_offset, int x_offset)
454 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
455 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
458 long top = text->toggle_cursor.y()
459 - text->toggle_cursor.row()->baseline();
460 long bottom = text->toggle_end_cursor.y()
461 - text->toggle_end_cursor.row()->baseline()
462 + text->toggle_end_cursor.row()->height();
464 typedef unsigned long ulong;
466 bottom = min(max(ulong(bottom), text->first), text->first + owner.height());
467 top = min(max(ulong(top), text->first), text->first + owner.height());
469 DrawFromTo(text, top - text->first, bottom - text->first, y_offset,
471 expose(0, top - text->first, owner.workWidth(),
472 bottom - text->first - (top - text->first));