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"
34 val.foreground = BlackPixel(fl_display,
35 DefaultScreen(fl_display));
38 val.graphics_exposures = false;
39 val.line_style = LineSolid;
41 return XCreateGC(fl_display, RootWindow(fl_display, 0),
42 GCForeground | GCFunction | GCGraphicsExposures
43 | GCLineWidth | GCLineStyle , &val);
48 LyXScreen::LyXScreen(WorkArea & o, LyXText * text_ptr)
49 : owner(o), text(text_ptr)
53 // the cursor isnt yet visible
54 cursor_visible = false;
66 void LyXScreen::Redraw()
68 DrawFromTo(0, owner.height());
69 expose(0, 0, owner.workWidth(), owner.height());
71 cursor_visible = false;
77 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
84 exp_width, exp_height,
90 void LyXScreen::DrawFromTo(int y1, int y2)
92 long y_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 long y = y_text - first;
99 // y1 is now the real beginning of row on the screen
101 while (row != 0 && y < y2) {
102 text->GetVisibleRow(owner.owner(), y, 0, row, y + first);
107 // maybe we have to clear the screen at the bottom
109 owner.getPainter().fillRectangle(0, y,
117 void LyXScreen::DrawOneRow(Row * row, long y_text)
119 long y = y_text - first;
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, 0, row, y + first);
129 /* draws the screen, starting with textposition y. uses as much already
130 * printed pixels as possible */
131 void LyXScreen::Draw(unsigned long y)
133 if (cursor_visible) HideCursor();
135 unsigned long old_first = first;
138 // is any optimiziation possible?
139 if ((y - old_first) < owner.height()
140 && (old_first - y) < owner.height()) {
141 if (first < old_first) {
142 DrawFromTo(0, old_first - first);
143 XCopyArea (fl_display,
150 owner.height() - old_first + first,
152 owner.ypos() + old_first - first
154 // expose the area drawn
160 owner.height() + old_first - first,
162 XCopyArea (fl_display,
167 owner.ypos() + first - old_first,
169 owner.height() + old_first - first,
172 // expose the area drawn
173 expose(0, owner.height() + old_first - first,
174 owner.workWidth(), first - old_first);
177 // make a dumb new-draw
178 DrawFromTo(0, owner.height());
179 expose(0, 0, owner.workWidth(), owner.height());
184 void LyXScreen::ShowCursor()
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->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(long /*x*/, long y, int asc, int desc)
207 if (y + desc - first >= owner.height())
208 newtop = y - 3 * owner.height() / 4; // the scroll region must be so big!!
209 else if (y - asc < long(first)
211 newtop = y - owner.height() / 4;
214 newtop = max(newtop, 0L); // can newtop ever be < 0? (Lgb)
216 if (newtop != long(first)) {
225 void LyXScreen::ShowManualCursor(long x, long y, int asc, int desc,
228 unsigned long y1 = max(y - first - asc, 0UL);
229 typedef unsigned long ulong;
231 unsigned long y2 = min(y - first + desc, ulong(owner.height()));
233 // Secure against very strange situations
237 XFreePixmap(fl_display, cursor_pixmap);
241 if (y2 > 0 && y1 < owner.height()) {
242 cursor_pixmap_h = y2 - y1 + 1;
243 cursor_pixmap_y = y1;
251 cursor_pixmap_w = cursor_pixmap_h/3;
254 case REVERSED_L_SHAPE:
255 cursor_pixmap_w = cursor_pixmap_h/3;
256 cursor_pixmap_x = x - cursor_pixmap_w + 1;
261 XCreatePixmap (fl_display,
265 fl_get_visual_depth());
266 XCopyArea (fl_display,
270 owner.xpos() + cursor_pixmap_x,
271 owner.ypos() + cursor_pixmap_y,
275 XDrawLine(fl_display,
286 case REVERSED_L_SHAPE:
287 int rectangle_h = (cursor_pixmap_h+10)/20;
288 XFillRectangle(fl_display,
291 cursor_pixmap_x + owner.xpos(),
292 y2 - rectangle_h + 1 + owner.ypos(),
293 cursor_pixmap_w - 1, rectangle_h);
298 cursor_visible = true;
302 void LyXScreen::HideCursor()
304 if (!cursor_visible) return;
307 XCopyArea (fl_display,
312 cursor_pixmap_w, cursor_pixmap_h,
313 cursor_pixmap_x + owner.xpos(),
314 cursor_pixmap_y + owner.ypos());
316 cursor_visible = false;
320 void LyXScreen::CursorToggle()
329 /* returns a new top so that the cursor is visible */
330 unsigned long LyXScreen::TopCursorVisible()
335 - text->cursor.row()->baseline()
336 + text->cursor.row()->height()
337 - first >= owner.height()) {
338 if (text->cursor.row()->height() < owner.height()
339 && text->cursor.row()->height() > owner.height() / 4)
340 newtop = text->cursor.y()
341 + text->cursor.row()->height()
342 - text->cursor.row()->baseline() - owner.height();
344 newtop = text->cursor.y()
345 - 3 * owner.height() / 4; /* the scroll region must be so big!! */
346 } else if (text->cursor.y() - text->cursor.row()->baseline() < first
348 if (text->cursor.row()->height() < owner.height()
349 && text->cursor.row()->height() > owner.height() / 4)
350 newtop = text->cursor.y() - text->cursor.row()->baseline();
352 newtop = text->cursor.y() - owner.height() / 4;
353 newtop = min(newtop, long(first));
357 newtop = max(newtop, 0L);
363 /* scrolls the screen so that the cursor is visible, if necessary.
364 * returns true if a change was made, otherwise false */
365 bool LyXScreen::FitCursor()
367 // Is a change necessary?
368 unsigned long newtop = TopCursorVisible();
369 bool result = (newtop != first);
376 void LyXScreen::Update()
378 switch(text->status) {
379 case LyXText::NEED_MORE_REFRESH:
381 long y = max(text->refresh_y - long(first), 0L);
383 DrawFromTo(y, owner.height());
385 text->status = LyXText::UNCHANGED;
387 owner.workWidth(), owner.height() - y);
390 case LyXText::NEED_VERY_LITTLE_REFRESH:
392 // ok I will update the current cursor row
393 DrawOneRow(text->refresh_row, text->refresh_y);
394 text->status = LyXText::UNCHANGED;
395 expose(0, text->refresh_y - first,
396 owner.workWidth(), text->refresh_row->height());
399 case LyXText::UNCHANGED:
400 // Nothing needs done
406 void LyXScreen::ToggleSelection(bool kill_selection)
408 // only if there is a selection
409 if (!text->selection) return;
411 long bottom = min(max(text->sel_end_cursor.y()
412 - text->sel_end_cursor.row()->baseline()
413 + text->sel_end_cursor.row()->height(), first),
414 first + owner.height());
415 long top = min(max(text->sel_start_cursor.y()
416 - text->sel_start_cursor.row()->baseline(), first),
417 first + owner.height());
421 DrawFromTo(top - first, bottom - first);
422 expose(0, top - first,
424 bottom - first - (top - first));
428 void LyXScreen::ToggleToggle()
430 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
431 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
434 long top = text->toggle_cursor.y()
435 - text->toggle_cursor.row()->baseline();
436 long bottom = text->toggle_end_cursor.y()
437 - text->toggle_end_cursor.row()->baseline()
438 + text->toggle_end_cursor.row()->height();
440 typedef unsigned long ulong;
442 bottom = min(max(ulong(bottom), first), first + owner.height());
443 top = min(max(ulong(top), first), first + owner.height());
445 DrawFromTo(top - first, bottom - first);
446 expose(0, top - first, owner.workWidth(),
447 bottom - first - (top - first));