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 text->GetVisibleRow(owner.owner(), y+y_offset, x_offset, row, y + text->first);
106 // maybe we have to clear the screen at the bottom
107 if ((y < y2) && text->bv_owner) {
108 owner.getPainter().fillRectangle(0, y,
116 void LyXScreen::DrawOneRow(LyXText * text, Row * row, long y_text,
117 int y_offset, int x_offset)
119 long y = y_text - text->first + y_offset;
121 if (y + row->height() > 0
122 && y - row->height() <= long(owner.height())) {
123 // ok there is something visible
124 text->GetVisibleRow(owner.owner(), y, x_offset, row, y + text->first);
129 /* draws the screen, starting with textposition y. uses as much already
130 * printed pixels as possible */
131 void LyXScreen::Draw(LyXText * text, unsigned long y)
133 if (cursor_visible) HideCursor();
135 unsigned long old_first = text->first;
138 // is any optimiziation possible?
139 if ((y - old_first) < owner.height()
140 && (old_first - y) < owner.height()) {
141 if (text->first < old_first) {
142 DrawFromTo(text, 0, old_first - text->first);
143 XCopyArea (fl_display,
150 owner.height() - old_first + text->first,
152 owner.ypos() + old_first - text->first
154 // expose the area drawn
157 old_first - text->first);
160 owner.height() + old_first - text->first,
162 XCopyArea (fl_display,
167 owner.ypos() + text->first - old_first,
169 owner.height() + old_first - text->first,
172 // expose the area drawn
173 expose(0, owner.height() + old_first - text->first,
174 owner.workWidth(), text->first - old_first);
177 // make a dumb new-draw
178 DrawFromTo(text, 0, owner.height());
179 expose(0, 0, owner.workWidth(), owner.height());
184 void LyXScreen::ShowCursor(LyXText const * text)
186 if (!cursor_visible) {
187 Cursor_Shape shape = BAR_SHAPE;
188 if (text->real_current_font.language() !=
189 owner.owner()->buffer()->params.language_info
190 || text->real_current_font.isVisibleRightToLeft()
191 != owner.owner()->buffer()->params.language_info->RightToLeft())
192 shape = (text->real_current_font.isVisibleRightToLeft())
193 ? REVERSED_L_SHAPE : L_SHAPE;
194 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
195 lyxfont::maxAscent(text->real_current_font),
196 lyxfont::maxDescent(text->real_current_font),
202 /* returns true if first has changed, otherwise false */
203 bool LyXScreen::FitManualCursor(LyXText * text,
204 long /*x*/, long y, int asc, int desc)
206 long newtop = text->first;
208 if (y + desc - text->first >= owner.height())
209 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
210 else if (y - asc < long(text->first)
211 && text->first > 0) {
212 newtop = y - owner.height() / 4;
215 newtop = max(newtop, 0L); // can newtop ever be < 0? (Lgb)
217 if (newtop != long(text->first)) {
219 text->first = newtop;
226 void LyXScreen::ShowManualCursor(LyXText const * text, long x, long y,
227 int asc, int desc, Cursor_Shape shape)
229 unsigned long y1 = max(y - text->first - asc, 0UL);
230 typedef unsigned long ulong;
232 unsigned long y2 = min(y - text->first + desc, ulong(owner.height()));
234 // Secure against very strange situations
238 XFreePixmap(fl_display, cursor_pixmap);
242 if (y2 > 0 && y1 < owner.height()) {
243 cursor_pixmap_h = y2 - y1 + 1;
244 cursor_pixmap_y = y1;
252 cursor_pixmap_w = cursor_pixmap_h/3;
255 case REVERSED_L_SHAPE:
256 cursor_pixmap_w = cursor_pixmap_h/3;
257 cursor_pixmap_x = x - cursor_pixmap_w + 1;
262 XCreatePixmap (fl_display,
266 fl_get_visual_depth());
267 XCopyArea (fl_display,
271 owner.xpos() + cursor_pixmap_x,
272 owner.ypos() + cursor_pixmap_y,
276 XDrawLine(fl_display,
287 case REVERSED_L_SHAPE:
288 int rectangle_h = (cursor_pixmap_h+10)/20;
289 XFillRectangle(fl_display,
292 cursor_pixmap_x + owner.xpos(),
293 y2 - rectangle_h + 1 + owner.ypos(),
294 cursor_pixmap_w - 1, rectangle_h);
299 cursor_visible = true;
303 void LyXScreen::HideCursor()
305 if (!cursor_visible) return;
308 XCopyArea (fl_display,
313 cursor_pixmap_w, cursor_pixmap_h,
314 cursor_pixmap_x + owner.xpos(),
315 cursor_pixmap_y + owner.ypos());
317 cursor_visible = false;
321 void LyXScreen::CursorToggle(LyXText const * text)
330 /* returns a new top so that the cursor is visible */
331 unsigned long LyXScreen::TopCursorVisible(LyXText const * text)
333 long newtop = text->first;
336 - text->cursor.row()->baseline()
337 + text->cursor.row()->height()
338 - text->first >= owner.height()) {
339 if (text->cursor.row()->height() < owner.height()
340 && text->cursor.row()->height() > owner.height() / 4)
341 newtop = text->cursor.y()
342 + text->cursor.row()->height()
343 - text->cursor.row()->baseline() - owner.height();
345 newtop = text->cursor.y()
346 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
347 } else if (text->cursor.y() - text->cursor.row()->baseline() < text->first
348 && text->first > 0) {
349 if (text->cursor.row()->height() < owner.height()
350 && text->cursor.row()->height() > owner.height() / 4)
351 newtop = text->cursor.y() - text->cursor.row()->baseline();
353 newtop = text->cursor.y() - owner.height() / 4;
354 newtop = min(newtop, long(text->first));
358 newtop = max(newtop, 0L);
364 /* scrolls the screen so that the cursor is visible, if necessary.
365 * returns true if a change was made, otherwise false */
366 bool LyXScreen::FitCursor(LyXText * text)
368 // Is a change necessary?
369 unsigned long newtop = TopCursorVisible(text);
370 bool result = (newtop != text->first);
377 void LyXScreen::Update(LyXText * text, int y_offset, int x_offset)
379 switch(text->status) {
380 case LyXText::NEED_MORE_REFRESH:
382 long y = max(text->refresh_y - long(text->first), 0L);
384 if (text->inset_owner)
385 height = text->inset_owner->ascent(owner.owner()->painter(),
386 text->real_current_font)
387 + text->inset_owner->descent(owner.owner()->painter(),
388 text->real_current_font);
390 height = owner.height();
391 DrawFromTo(text, y, owner.height(), y_offset, x_offset);
393 text->status = LyXText::UNCHANGED;
395 owner.workWidth(), owner.height() - y);
398 case LyXText::NEED_VERY_LITTLE_REFRESH:
400 // ok I will update the current cursor row
401 DrawOneRow(text, text->refresh_row, text->refresh_y,
403 text->status = LyXText::UNCHANGED;
404 expose(0, text->refresh_y - text->first + y_offset,
405 owner.workWidth(), text->refresh_row->height());
408 case LyXText::UNCHANGED:
409 // Nothing needs done
415 void LyXScreen::ToggleSelection(LyXText * text, bool kill_selection)
417 // only if there is a selection
418 if (!text->selection) return;
420 long bottom = min(max(text->sel_end_cursor.y()
421 - text->sel_end_cursor.row()->baseline()
422 + text->sel_end_cursor.row()->height(), text->first),
423 text->first + owner.height());
424 long top = min(max(text->sel_start_cursor.y()
425 - text->sel_start_cursor.row()->baseline(), text->first),
426 text->first + owner.height());
430 DrawFromTo(text, top - text->first, bottom - text->first);
431 expose(0, top - text->first,
433 bottom - text->first - (top - text->first));
437 void LyXScreen::ToggleToggle(LyXText * text, int y_offset, int x_offset)
439 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
440 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
443 long top = text->toggle_cursor.y()
444 - text->toggle_cursor.row()->baseline();
445 long bottom = text->toggle_end_cursor.y()
446 - text->toggle_end_cursor.row()->baseline()
447 + text->toggle_end_cursor.row()->height();
449 typedef unsigned long ulong;
451 bottom = min(max(ulong(bottom), text->first), text->first + owner.height());
452 top = min(max(ulong(top), text->first), text->first + owner.height());
454 DrawFromTo(text, top - text->first, bottom - text->first, y_offset,
456 expose(0, top - text->first, owner.workWidth(),
457 bottom - text->first - (top - text->first));