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.get()) 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 = bv->text->status;
118 bv->text->status = st;
119 text->GetVisibleRow(bv, y + y_offset,
120 x_offset, row, y + text->first);
121 } while (bv->text->status == LyXText::CHANGED_IN_DRAW);
122 bv->text->status = st;
128 // maybe we have to clear the screen at the bottom
129 if ((y < y2) && text->bv_owner) {
130 owner.getPainter().fillRectangle(0, y,
138 void LyXScreen::DrawOneRow(LyXText * text, BufferView * bv, Row * row,
139 int y_text, int y_offset, int x_offset)
141 int const y = y_text - text->first + y_offset;
143 if (((y + row->height()) > 0) &&
144 ((y - row->height()) <= static_cast<int>(owner.height()))) {
145 // ok there is something visible
146 LyXText::text_status st = bv->text->status;
148 bv->text->status = st;
149 text->GetVisibleRow(bv, y, x_offset, row,
151 } while (bv->text->status == LyXText::CHANGED_IN_DRAW);
152 bv->text->status = st;
158 /* draws the screen, starting with textposition y. uses as much already
159 * printed pixels as possible */
160 void LyXScreen::Draw(LyXText * text, BufferView * bv, unsigned int y)
162 if (cursor_visible) HideCursor();
164 int const old_first = text->first;
167 // is any optimiziation possible?
168 if ((y - old_first) < owner.height()
169 && (old_first - y) < owner.height()) {
170 if (text->first < old_first) {
171 DrawFromTo(text, bv, 0, old_first - text->first);
172 XCopyArea (fl_get_display(),
179 owner.height() - old_first + text->first,
181 owner.ypos() + old_first - text->first
183 // expose the area drawn
186 old_first - text->first);
189 owner.height() + old_first - text->first,
191 XCopyArea (fl_get_display(),
196 owner.ypos() + text->first - old_first,
198 owner.height() + old_first - text->first,
201 // expose the area drawn
202 expose(0, owner.height() + old_first - text->first,
203 owner.workWidth(), text->first - old_first);
206 // make a dumb new-draw
207 DrawFromTo(text, bv, 0, owner.height());
208 expose(0, 0, owner.workWidth(), owner.height());
213 void LyXScreen::ShowCursor(LyXText const * text, BufferView const * bv)
215 if (!cursor_visible) {
216 Cursor_Shape shape = BAR_SHAPE;
217 if (text->real_current_font.language() !=
218 bv->buffer()->params.language
219 || text->real_current_font.isVisibleRightToLeft()
220 != bv->buffer()->params.language->RightToLeft())
221 shape = (text->real_current_font.isVisibleRightToLeft())
222 ? REVERSED_L_SHAPE : L_SHAPE;
223 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
224 lyxfont::maxAscent(text->real_current_font),
225 lyxfont::maxDescent(text->real_current_font),
231 /* returns true if first has changed, otherwise false */
232 bool LyXScreen::FitManualCursor(LyXText * text, BufferView * bv,
233 int /*x*/, int y, int asc, int desc)
235 int newtop = text->first;
237 if (y + desc - text->first >= static_cast<int>(owner.height()))
238 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
239 else if (y - asc < static_cast<int>(text->first)
240 && text->first > 0) {
241 newtop = y - owner.height() / 4;
244 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
246 if (newtop != static_cast<int>(text->first)) {
247 Draw(text, bv, newtop);
248 text->first = newtop;
255 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
256 int asc, int desc, Cursor_Shape shape)
258 // Update the cursor color.
261 int const y1 = max(y - text->first - asc, 0);
262 int const y_tmp = min(y - text->first + desc,
263 static_cast<int>(owner.height()));
265 // Secure against very strange situations
266 int const y2 = max(y_tmp, y1);
269 XFreePixmap(fl_get_display(), cursor_pixmap);
273 if (y2 > 0 && y1 < int(owner.height())) {
274 cursor_pixmap_h = y2 - y1 + 1;
275 cursor_pixmap_y = y1;
283 cursor_pixmap_w = cursor_pixmap_h/3;
286 case REVERSED_L_SHAPE:
287 cursor_pixmap_w = cursor_pixmap_h/3;
288 cursor_pixmap_x = x - cursor_pixmap_w + 1;
293 XCreatePixmap (fl_get_display(),
297 fl_get_visual_depth());
298 XCopyArea (fl_get_display(),
302 owner.xpos() + cursor_pixmap_x,
303 owner.ypos() + cursor_pixmap_y,
307 XDrawLine(fl_get_display(),
318 case REVERSED_L_SHAPE:
319 int const rectangle_h = (cursor_pixmap_h + 10) / 20;
320 XFillRectangle(fl_get_display(),
323 cursor_pixmap_x + owner.xpos(),
324 y2 - rectangle_h + 1 + owner.ypos(),
325 cursor_pixmap_w - 1, rectangle_h);
330 cursor_visible = true;
334 void LyXScreen::HideCursor()
336 if (!cursor_visible) return;
339 XCopyArea (fl_get_display(),
344 cursor_pixmap_w, cursor_pixmap_h,
345 cursor_pixmap_x + owner.xpos(),
346 cursor_pixmap_y + owner.ypos());
348 cursor_visible = false;
352 void LyXScreen::CursorToggle(LyXText const * text, BufferView const * bv)
357 ShowCursor(text, bv);
361 /* returns a new top so that the cursor is visible */
362 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
364 int newtop = text->first;
367 - text->cursor.row()->baseline()
368 + text->cursor.row()->height()
369 - text->first >= owner.height()) {
370 if (text->cursor.row()->height() < owner.height()
371 && text->cursor.row()->height() > owner.height() / 4)
372 newtop = text->cursor.y()
373 + text->cursor.row()->height()
374 - text->cursor.row()->baseline() - owner.height();
376 newtop = text->cursor.y()
377 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
378 } else if (static_cast<int>((text->cursor.y()) - text->cursor.row()->baseline()) <
379 text->first && text->first > 0)
381 if (text->cursor.row()->height() < owner.height()
382 && text->cursor.row()->height() > owner.height() / 4)
383 newtop = text->cursor.y() - text->cursor.row()->baseline();
385 newtop = text->cursor.y() - owner.height() / 4;
386 newtop = min(newtop, int(text->first));
390 newtop = max(newtop, 0);
396 /* scrolls the screen so that the cursor is visible, if necessary.
397 * returns true if a change was made, otherwise false */
398 bool LyXScreen::FitCursor(LyXText * text, BufferView * bv)
400 // Is a change necessary?
401 int const newtop = TopCursorVisible(text);
402 bool const result = (newtop != text->first);
404 Draw(text, bv, newtop);
409 void LyXScreen::Update(LyXText * text, BufferView * bv,
410 int y_offset, int x_offset)
412 switch (text->status) {
413 case LyXText::NEED_MORE_REFRESH:
415 int const y = max(int(text->refresh_y - text->first), 0);
416 DrawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
418 text->status = LyXText::UNCHANGED;
419 expose(0, y, owner.workWidth(), owner.height() - y);
422 case LyXText::NEED_VERY_LITTLE_REFRESH:
424 // ok I will update the current cursor row
425 DrawOneRow(text, bv, text->refresh_row, text->refresh_y,
427 text->status = LyXText::UNCHANGED;
428 expose(0, text->refresh_y - text->first + y_offset,
429 owner.workWidth(), text->refresh_row->height());
432 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
433 case LyXText::UNCHANGED:
434 // Nothing needs done
440 void LyXScreen::ToggleSelection(LyXText * text, BufferView * bv,
442 int y_offset, int x_offset)
444 // only if there is a selection
445 if (!text->selection) return;
447 int const bottom = min(
448 max(static_cast<int>(text->sel_end_cursor.y()
449 - text->sel_end_cursor.row()->baseline()
450 + text->sel_end_cursor.row()->height()),
452 static_cast<int>(text->first + owner.height()));
454 max(static_cast<int>(text->sel_start_cursor.y() -
455 text->sel_start_cursor.row()->baseline()),
457 static_cast<int>(text->first + owner.height()));
461 DrawFromTo(text, bv, top - text->first, bottom - text->first,
463 expose(0, top - text->first,
465 bottom - text->first - (top - text->first));
469 void LyXScreen::ToggleToggle(LyXText * text, BufferView * bv,
470 int y_offset, int x_offset)
472 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
473 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
476 int const top_tmp = text->toggle_cursor.y()
477 - text->toggle_cursor.row()->baseline();
478 int const bottom_tmp = text->toggle_end_cursor.y()
479 - text->toggle_end_cursor.row()->baseline()
480 + text->toggle_end_cursor.row()->height();
482 int const bottom = min(max(bottom_tmp, text->first),
483 static_cast<int>(text->first + owner.height()));
484 int const top = min(max(top_tmp, text->first),
485 static_cast<int>(text->first + owner.height()));
487 DrawFromTo(text, bv, top - text->first, bottom - text->first, y_offset,
489 expose(0, top - text->first, owner.workWidth(),
490 bottom - text->first - (top - text->first));