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_get_display(),
36 DefaultScreen(fl_get_display()));
39 val.graphics_exposures = false;
40 val.line_style = LineSolid;
42 return XCreateGC(fl_get_display(), RootWindow(fl_get_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)
78 XCopyArea(fl_get_display(),
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()) <= (int)owner.height()))
132 // ok there is something visible
133 LyXText::text_status st = text->status;
136 text->GetVisibleRow(owner.owner(), y, x_offset, row,
138 } while (text->status == LyXText::CHANGED_IN_DRAW);
145 /* draws the screen, starting with textposition y. uses as much already
146 * printed pixels as possible */
147 void LyXScreen::Draw(LyXText * text, unsigned int y)
149 if (cursor_visible) HideCursor();
151 int old_first = text->first;
154 // is any optimiziation possible?
155 if ((y - old_first) < owner.height()
156 && (old_first - y) < owner.height()) {
157 if (text->first < old_first) {
158 DrawFromTo(text, 0, old_first - text->first);
159 XCopyArea (fl_get_display(),
166 owner.height() - old_first + text->first,
168 owner.ypos() + old_first - text->first
170 // expose the area drawn
173 old_first - text->first);
176 owner.height() + old_first - text->first,
178 XCopyArea (fl_get_display(),
183 owner.ypos() + text->first - old_first,
185 owner.height() + old_first - text->first,
188 // expose the area drawn
189 expose(0, owner.height() + old_first - text->first,
190 owner.workWidth(), text->first - old_first);
193 // make a dumb new-draw
194 DrawFromTo(text, 0, owner.height());
195 expose(0, 0, owner.workWidth(), owner.height());
200 void LyXScreen::ShowCursor(LyXText const * text)
202 if (!cursor_visible) {
203 Cursor_Shape shape = BAR_SHAPE;
204 if (text->real_current_font.language() !=
205 owner.owner()->buffer()->params.language
206 || text->real_current_font.isVisibleRightToLeft()
207 != owner.owner()->buffer()->params.language->RightToLeft())
208 shape = (text->real_current_font.isVisibleRightToLeft())
209 ? REVERSED_L_SHAPE : L_SHAPE;
210 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
211 lyxfont::maxAscent(text->real_current_font),
212 lyxfont::maxDescent(text->real_current_font),
218 /* returns true if first has changed, otherwise false */
219 bool LyXScreen::FitManualCursor(LyXText * text,
220 int /*x*/, int y, int asc, int desc)
222 int newtop = text->first;
224 if (y + desc - text->first >= (int)owner.height())
225 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
226 else if (y - asc < (int)text->first
227 && text->first > 0) {
228 newtop = y - owner.height() / 4;
231 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
233 if (newtop != (int)text->first) {
235 text->first = newtop;
242 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
243 int asc, int desc, Cursor_Shape shape)
245 unsigned int y1 = max(y - text->first - asc, 0);
246 typedef unsigned int uint;
248 unsigned int y2 = min(y - text->first + desc, (int)owner.height());
250 // Secure against very strange situations
254 XFreePixmap(fl_get_display(), cursor_pixmap);
258 if (y2 > 0 && y1 < owner.height()) {
259 cursor_pixmap_h = y2 - y1 + 1;
260 cursor_pixmap_y = y1;
268 cursor_pixmap_w = cursor_pixmap_h/3;
271 case REVERSED_L_SHAPE:
272 cursor_pixmap_w = cursor_pixmap_h/3;
273 cursor_pixmap_x = x - cursor_pixmap_w + 1;
278 XCreatePixmap (fl_get_display(),
282 fl_get_visual_depth());
283 XCopyArea (fl_get_display(),
287 owner.xpos() + cursor_pixmap_x,
288 owner.ypos() + cursor_pixmap_y,
292 XDrawLine(fl_get_display(),
303 case REVERSED_L_SHAPE:
304 int rectangle_h = (cursor_pixmap_h+10)/20;
305 XFillRectangle(fl_get_display(),
308 cursor_pixmap_x + owner.xpos(),
309 y2 - rectangle_h + 1 + owner.ypos(),
310 cursor_pixmap_w - 1, rectangle_h);
315 cursor_visible = true;
319 void LyXScreen::HideCursor()
321 if (!cursor_visible) return;
324 XCopyArea (fl_get_display(),
329 cursor_pixmap_w, cursor_pixmap_h,
330 cursor_pixmap_x + owner.xpos(),
331 cursor_pixmap_y + owner.ypos());
333 cursor_visible = false;
337 void LyXScreen::CursorToggle(LyXText const * text)
346 /* returns a new top so that the cursor is visible */
347 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
349 int newtop = text->first;
352 - text->cursor.row()->baseline()
353 + text->cursor.row()->height()
354 - text->first >= owner.height()) {
355 if (text->cursor.row()->height() < owner.height()
356 && text->cursor.row()->height() > owner.height() / 4)
357 newtop = text->cursor.y()
358 + text->cursor.row()->height()
359 - text->cursor.row()->baseline() - owner.height();
361 newtop = text->cursor.y()
362 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
363 } else if ((int)(text->cursor.y() - text->cursor.row()->baseline()) <
364 text->first && text->first > 0)
366 if (text->cursor.row()->height() < owner.height()
367 && text->cursor.row()->height() > owner.height() / 4)
368 newtop = text->cursor.y() - text->cursor.row()->baseline();
370 newtop = text->cursor.y() - owner.height() / 4;
371 newtop = min(newtop, int(text->first));
375 newtop = max(newtop, 0);
381 /* scrolls the screen so that the cursor is visible, if necessary.
382 * returns true if a change was made, otherwise false */
383 bool LyXScreen::FitCursor(LyXText * text)
385 // Is a change necessary?
386 int newtop = TopCursorVisible(text);
387 bool result = (newtop != text->first);
394 void LyXScreen::Update(LyXText * text, int y_offset, int x_offset)
396 switch(text->status) {
397 case LyXText::NEED_MORE_REFRESH:
399 int y = max(int(text->refresh_y - text->first), 0);
400 DrawFromTo(text, y, owner.height(), y_offset, x_offset);
402 text->status = LyXText::UNCHANGED;
403 expose(0, y, owner.workWidth(), owner.height() - y);
406 case LyXText::NEED_VERY_LITTLE_REFRESH:
408 // ok I will update the current cursor row
409 DrawOneRow(text, text->refresh_row, text->refresh_y,
411 text->status = LyXText::UNCHANGED;
412 expose(0, text->refresh_y - text->first + y_offset,
413 owner.workWidth(), text->refresh_row->height());
416 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
417 case LyXText::UNCHANGED:
418 // Nothing needs done
424 void LyXScreen::ToggleSelection(LyXText * text, bool kill_selection,
425 int y_offset, int x_offset)
427 // only if there is a selection
428 if (!text->selection) return;
430 int bottom = min(max((int)(text->sel_end_cursor.y()
431 - text->sel_end_cursor.row()->baseline()
432 + text->sel_end_cursor.row()->height()), text->first),
433 (int)(text->first + owner.height()));
434 int top = min(max((int)(text->sel_start_cursor.y() -
435 text->sel_start_cursor.row()->baseline()),
437 (int)(text->first + owner.height()));
441 DrawFromTo(text, top - text->first, bottom - text->first,
443 expose(0, top - text->first,
445 bottom - text->first - (top - text->first));
449 void LyXScreen::ToggleToggle(LyXText * text, int y_offset, int x_offset)
451 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
452 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
455 int top = text->toggle_cursor.y()
456 - text->toggle_cursor.row()->baseline();
457 int bottom = text->toggle_end_cursor.y()
458 - text->toggle_end_cursor.row()->baseline()
459 + text->toggle_end_cursor.row()->height();
461 typedef unsigned int uint;
463 bottom = min(max(bottom, text->first), (int)(text->first + owner.height()));
464 top = min(max(top, text->first), (int)(text->first + owner.height()));
466 DrawFromTo(text, top - text->first, bottom - text->first, y_offset,
468 expose(0, top - text->first, owner.workWidth(),
469 bottom - text->first - (top - text->first));