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"
17 #include "lyxscreen.h"
20 #include "BufferView.h"
27 extern int mono_video;
28 extern int fast_selection;
36 val.foreground = BlackPixel(fl_display,
37 DefaultScreen(fl_display));
40 val.graphics_exposures = false;
41 val.line_style = LineSolid;
43 return XCreateGC(fl_display, RootWindow(fl_display, 0),
44 GCForeground | GCFunction | GCGraphicsExposures
45 | GCLineWidth | GCLineStyle , &val);
51 LyXScreen::LyXScreen(BufferView * o, Window window,
60 : owner(o), text(text_ptr), _window(window),
61 _width(width), _height(height),
62 _offset_x(offset_x), _offset_y(offset_y)
66 /* the cursor isnt yet visible */
67 cursor_visible = false;
68 screen_refresh_y = -1;
70 /* create the foreground pixmap */
74 foreground = XCreatePixmap (fl_display,
77 fl_get_visual_depth());
93 LyXScreen::~LyXScreen()
96 XFreePixmap(fl_display, foreground);
101 void LyXScreen::Redraw()
103 DrawFromTo(0, _height);
104 screen_refresh_y = -1;
105 expose(0, 0, _width, _height);
106 if (cursor_visible) {
107 cursor_visible = false;
114 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
116 XCopyArea(fl_display,
121 exp_width, exp_height,
122 x+_offset_x, y+_offset_y);
125 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
127 XCopyArea(fl_display,
132 exp_width, exp_height,
133 x+_offset_x, y+_offset_y);
139 void LyXScreen::DrawFromTo(int y1, int y2)
141 long y_text = first + y1;
143 /* get the first needed row */
144 Row * row = text->GetRowNearY(y_text);
145 /* y_text is now the real beginning of the row */
147 long y = y_text - first;
148 /* y1 is now the real beginning of row on the screen */
150 while (row != 0 && y < y2) {
152 text->GetVisibleRow(y, row, y + first);
158 /* maybe we have to clear the screen at the bottom */
160 owner->painter().fillRectangle(0, y, _width, y2 - y,
165 void LyXScreen::DrawFromTo(int y1, int y2)
167 long y_text = first + y1;
169 /* get the first needed row */
170 Row * row = text->GetRowNearY(y_text);
171 /* y_text is now the real beginning of the row */
173 long y = y_text - first;
174 /* y1 is now the real beginning of row on the screen */
176 while (row != 0 && y < y2) {
178 text->GetVisibleRow(*this, y, row, y + first);
184 /* maybe we have to clear the screen at the bottom */
186 fillRectangle(gc_lighted,
196 void LyXScreen::DrawOneRow(Row * row, long & y_text)
198 long y = y_text - first;
200 if (y + row->height > 0 && y - row->height <= _height) {
201 /* ok there is something visible */
203 text->GetVisibleRow(y, row, y + first);
205 text->GetVisibleRow(*this, y, row, y + first);
208 y_text+= row->height;
212 /* draws the screen, starting with textposition y. uses as much already
213 * printed pixels as possible */
215 void LyXScreen::Draw(long y)
217 if (cursor_visible) HideCursor();
220 long old_first = first;
223 /* is any optimiziation possible? */
224 if ((y - old_first) < _height
225 && (old_first - y) < _height) {
226 if (first < old_first) {
227 DrawFromTo(0, old_first - first);
228 XCopyArea (fl_display,
232 _offset_x, _offset_y,
233 _width , _height - old_first + first,
234 _offset_x, _offset_y + old_first - first);
235 // expose the area drawn
236 expose(0, 0, _width, old_first - first);
238 DrawFromTo(_height + old_first - first, _height);
239 XCopyArea (fl_display,
243 _offset_x, _offset_y + first - old_first,
244 _width , _height + old_first - first,
245 _offset_x, _offset_y);
246 // expose the area drawn
247 expose(0, _height + old_first - first,
248 _width, first - old_first);
251 /* make a dumb new-draw */
252 DrawFromTo(0, _height);
253 expose(0, 0, _width, _height);
257 void LyXScreen::Draw(long y)
259 if (cursor_visible) HideCursor();
262 long old_first = first;
265 /* is any optimiziation possible? */
266 if ((y - old_first) < _height
267 && (old_first - y) < _height) {
268 if (first < old_first) {
269 DrawFromTo(0, old_first - first);
270 XCopyArea (fl_display,
274 _offset_x, _offset_y,
275 _width , _height - old_first + first,
276 _offset_x, _offset_y + old_first - first);
277 // expose the area drawn
278 expose(0, 0, _width, old_first - first);
280 DrawFromTo(_height + old_first - first, _height);
281 XCopyArea (fl_display,
285 _offset_x, _offset_y + first - old_first,
286 _width , _height + old_first - first,
287 _offset_x, _offset_y);
288 // expose the area drawn
289 expose(0, _height + old_first - first,
290 _width, first - old_first);
293 /* make a dumb new-draw */
294 DrawFromTo(0, _height);
295 expose(0, 0, _width, _height);
301 void LyXScreen::ShowCursor()
307 if (cursor_visible) return;
311 y1 = text->cursor.y - text->real_current_font.maxAscent() - first;
314 y2 = text->cursor.y + text->real_current_font.maxDescent() - first;
315 if (y2 > _height) y2 = _height;
317 // Secure against very strange situations
318 if (y2 < y1) y2 = y1;
321 if (fast_selection || mono_video) {
322 if (y2 > 0 && y1 < _height) {
323 XDrawLine(fl_display,
324 _window, getGC(gc_select),
329 cursor_visible = true;
334 XFreePixmap(fl_display, cursor_pixmap);
338 if (y2 > 0 && y1 < _height) {
340 cursor_pixmap_h = y2 - y1 + 1;
342 cursor_pixmap_y = y1;
344 XCreatePixmap(fl_display,
348 fl_get_visual_depth());
350 XCopyArea(fl_display,
354 _offset_x + cursor_pixmap_x,
355 _offset_y + cursor_pixmap_y,
356 cursor_pixmap_w, cursor_pixmap_h,
358 XDrawLine(fl_display,
365 XCopyArea(fl_display,
369 _offset_x + cursor_pixmap_x,
370 _offset_y + cursor_pixmap_y,
371 cursor_pixmap_w, cursor_pixmap_h,
373 XDrawLine(fl_display,
381 cursor_visible = true;
389 /* returns 1 if first has changed, otherwise 0 */
390 int LyXScreen::FitManualCursor(long /*x*/, long y, int asc, int desc)
394 if (y + desc - first >= _height)
395 newtop = y - 3*_height / 4; /* the scroll region must be so big!! */
396 else if (y - asc < first
398 newtop = y - _height / 4;
403 if (newtop != first){
412 void LyXScreen::HideManualCursor(long x, long y, int asc, int desc)
415 if (fast_selection || mono_video)
416 ShowManualCursor(x, y, asc, desc);
423 void LyXScreen::ShowManualCursor(long x, long y, int asc, int desc)
428 y1 = y - first - asc;
431 y2 = y -first + desc;
436 if (fast_selection || mono_video) {
437 if (y2 > 0 && y1 < _height) {
438 XDrawLine(fl_display,
439 _window, getGC(gc_select),
448 XFreePixmap(fl_display, cursor_pixmap);
452 if (y2 > 0 && y1 < _height) {
454 cursor_pixmap_h = y2 - y1 + 1;
456 cursor_pixmap_y = y1;
458 XCreatePixmap (fl_display,
462 fl_get_visual_depth());
464 XCopyArea (fl_display,
468 _offset_x + cursor_pixmap_x,
469 _offset_y + cursor_pixmap_y,
473 XDrawLine(fl_display,
480 XCopyArea (fl_display,
484 _offset_x + cursor_pixmap_x,
485 _offset_y + cursor_pixmap_y,
489 XDrawLine(fl_display,
498 cursor_visible = true;
505 void LyXScreen::HideCursor()
507 if (!cursor_visible) return;
510 if (fast_selection || mono_video) {
511 cursor_visible = false;
513 cursor_visible = false;
518 XCopyArea (fl_display,
523 cursor_pixmap_w, cursor_pixmap_h,
524 cursor_pixmap_x + _offset_x,
525 cursor_pixmap_y + _offset_y);
527 XCopyArea (fl_display,
532 cursor_pixmap_w, cursor_pixmap_h,
533 cursor_pixmap_x + _offset_x,
534 cursor_pixmap_y + _offset_y);
537 cursor_visible = false;
544 void LyXScreen::CursorToggle()
553 /* returns a new top so that the cursor is visible */
554 long LyXScreen::TopCursorVisible()
559 - text->cursor.row->baseline
560 + text->cursor.row->height
561 - first >= _height) {
562 if (text->cursor.row->height < _height
563 && text->cursor.row->height > _height/4)
564 newtop = text->cursor.y
565 + text->cursor.row->height
566 - text->cursor.row->baseline - _height;
568 newtop = text->cursor.y
569 - 3*_height / 4; /* the scroll region must be so big!! */
570 } else if (text->cursor.y - text->cursor.row->baseline < first
572 if (text->cursor.row->height < _height
573 && text->cursor.row->height > _height/4)
574 newtop = text->cursor.y - text->cursor.row->baseline;
576 newtop = text->cursor.y - _height / 4;
588 /* scrolls the screen so that the cursor is visible, if necessary.
589 * returns 1 if a change was made, otherwise 0 */
590 int LyXScreen::FitCursor()
592 /* is a change necessary */
593 long newtop = TopCursorVisible();
594 int result = (newtop != first);
601 void LyXScreen::Update()
605 if (text->status == LyXText::NEED_MORE_REFRESH
606 || screen_refresh_y > -1 ) {
607 if (screen_refresh_y > -1
608 && screen_refresh_y < text->refresh_y)
609 y = screen_refresh_y;
613 if (y < first) y = first;
615 DrawFromTo(y - first, _height);
617 text->status = LyXText::UNCHANGED;
618 screen_refresh_y = -1;
619 expose(0, y-first, _width, _height - (y - first));
620 } else if (text->status == LyXText::NEED_VERY_LITTLE_REFRESH) {
621 /* ok I will update the current cursor row */
623 DrawOneRow(text->refresh_row, y);
624 text->status = LyXText::UNCHANGED;
625 expose(0, text->refresh_y-first,
626 _width, text->refresh_row->height);
631 void LyXScreen::SmallUpdate()
637 if (text->status == LyXText::NEED_MORE_REFRESH){
638 /* ok I will update till the current cursor row */
639 row = text->refresh_row;
643 if (y > text->cursor.y) {
648 while (row && row != text->cursor.row && y < first + _height) {
654 screen_refresh_y = y;
655 screen_refresh_row = row->next;
656 text->status = LyXText::UNCHANGED;
657 // Is the right regin exposed?
658 expose(0, y2-first, _width, y-y2);
659 } else if (text->status == LyXText::NEED_VERY_LITTLE_REFRESH) {
660 /* ok I will update the current cursor row */
661 row = text->refresh_row;
664 text->status = LyXText::UNCHANGED;
665 expose(0, text->refresh_y - first,
666 _width, row->height);
671 void LyXScreen::ToggleSelection(bool kill_selection)
673 /* only if there is a selection */
674 if (!text->selection)
681 if (fast_selection || mono_video) {
683 /* selection only in one row ?*/
684 if (text->sel_start_cursor.y == text->sel_end_cursor.y) {
686 /* only if something is visible */
687 if (text->sel_start_cursor.y
688 - text->sel_start_cursor.row->baseline
690 && text->sel_start_cursor.y
691 - text->sel_start_cursor.row->baseline +
692 text->sel_start_cursor.row->height - first > 0) {
693 top = text->sel_start_cursor.y
694 - text->sel_start_cursor.row->baseline
697 + text->sel_start_cursor.row->height;
700 if (bottom > _height)
702 XFillRectangle(fl_display, _window,
704 text->sel_start_cursor.x
707 text->sel_end_cursor.x
708 - text->sel_start_cursor.x,
712 /* the sel_start_cursor row first */
713 /* only if anything is visible */
714 if (text->sel_start_cursor.y
715 - text->sel_start_cursor.row->baseline
717 && text->sel_start_cursor.y
718 - text->sel_start_cursor.row->baseline +
719 text->sel_start_cursor.row->height - first > 0) {
720 top = text->sel_start_cursor.y
721 - text->sel_start_cursor.row->baseline
724 + text->sel_start_cursor.row->height;
727 if (bottom > _height)
729 XFillRectangle(fl_display, _window,
731 text->sel_start_cursor.x
735 - text->sel_start_cursor.x,
741 if (text->sel_start_cursor.row->next !=
742 text->sel_end_cursor.row) {
743 top = text->sel_start_cursor.y
744 - text->sel_start_cursor.row->baseline
745 + text->sel_start_cursor.row->height;
746 bottom = text->sel_end_cursor.y
747 - text->sel_end_cursor.row->baseline;
751 if (bottom - first < 0)
754 if (bottom - first > _height)
755 bottom = first + _height;
756 if (top - first > _height)
757 top = first + _height;
760 XFillRectangle(fl_display,
764 top - first+_offset_y,
770 /* the sel_end_cursor row last */
771 if (text->sel_end_cursor.y
772 - text->sel_end_cursor.row->baseline
774 && text->sel_end_cursor.y
775 - text->sel_end_cursor.row->baseline +
776 text->sel_end_cursor.row->height - first > 0) {
777 top = text->sel_end_cursor.y
778 - text->sel_end_cursor.row->baseline
781 + text->sel_end_cursor.row->height;
784 if (bottom > _height)
786 XFillRectangle(fl_display, _window,
790 text->sel_end_cursor.x,
796 top = text->sel_start_cursor.y
797 - text->sel_start_cursor.row->baseline;
798 bottom = text->sel_end_cursor.y
799 - text->sel_end_cursor.row->baseline
800 + text->sel_end_cursor.row->height;
804 if (bottom - first < 0)
807 if (bottom - first > _height)
808 bottom = first + _height;
809 if (top - first > _height)
810 top = first + _height;
814 DrawFromTo(top - first, bottom - first);
815 expose(0, top - first, _width, bottom - first - (top - first));
822 void LyXScreen::ToggleToggle()
827 if (text->toggle_cursor.par == text->toggle_end_cursor.par
828 && text->toggle_cursor.pos == text->toggle_end_cursor.pos)
832 if (fast_selection || mono_video) {
834 /* selection only in one row ?*/
835 if (text->toggle_cursor.y == text->toggle_end_cursor.y) {
837 /* only if anything is visible */
838 if (text->toggle_cursor.y - text->toggle_cursor.row->baseline - first < _height
839 && text->toggle_cursor.y - text->toggle_cursor.row->baseline +
840 text->toggle_cursor.row->height - first > 0) {
841 top = text->toggle_cursor.y - text->toggle_cursor.row->baseline - first;
842 bottom = top + text->toggle_cursor.row->height;
843 if (top < 0) top = 0;
844 if (bottom > _height) bottom = _height;
845 XFillRectangle(fl_display, _window,
847 text->toggle_cursor.x+_offset_x,
849 text->toggle_end_cursor.x -
850 text->toggle_cursor.x,
854 /* the toggle_cursor row first */
855 /* only if anything is visible */
856 if (text->toggle_cursor.y - text->toggle_cursor.row->baseline - first < _height
857 && text->toggle_cursor.y - text->toggle_cursor.row->baseline +
858 text->toggle_cursor.row->height - first > 0) {
859 top = text->toggle_cursor.y - text->toggle_cursor.row->baseline - first;
860 bottom = top + text->toggle_cursor.row->height;
863 if (bottom > _height)
865 XFillRectangle(fl_display, _window,
867 text->toggle_cursor.x+_offset_x,
869 _width - text->toggle_cursor.x,
875 if (text->toggle_cursor.row->next !=
876 text->toggle_end_cursor.row) {
877 top = text->toggle_cursor.y
878 - text->toggle_cursor.row->baseline
879 + text->toggle_cursor.row->height;
880 bottom = text->toggle_end_cursor.y
881 - text->toggle_end_cursor.row->baseline;
885 if (bottom - first < 0)
888 if (bottom - first > _height)
889 bottom = first + _height;
890 if (top - first > _height)
891 top = first + _height;
894 XFillRectangle(fl_display, _window,
897 top - first+_offset_y,
903 /* the toggle_end_cursor row last */
904 if (text->toggle_end_cursor.y - text->toggle_end_cursor.row->baseline - first < _height
905 && text->toggle_end_cursor.y - text->toggle_end_cursor.row->baseline +
906 text->toggle_end_cursor.row->height - first > 0) {
907 top = text->toggle_end_cursor.y
908 - text->toggle_end_cursor.row->baseline
911 + text->toggle_end_cursor.row->height;
914 if (bottom > _height)
916 XFillRectangle(fl_display, _window,
920 text->toggle_end_cursor.x,
926 top = text->toggle_cursor.y
927 - text->toggle_cursor.row->baseline;
928 bottom = text->toggle_end_cursor.y
929 - text->toggle_end_cursor.row->baseline
930 + text->toggle_end_cursor.row->height;
934 if (bottom - first < 0)
937 if (bottom - first > _height)
938 bottom = first + _height;
939 if (top - first > _height)
940 top = first + _height;
942 DrawFromTo(top - first, bottom - first);
943 expose(0, top - first, _width, bottom - first - (top - first));
951 void LyXScreen::drawTableLine(int baseline, int x, int length, bool on_off)
955 gc = getGC(gc_thin_on_off_line);
968 void LyXScreen::drawVerticalTableLine(int x, int y1, int y2, bool on_off)
972 gc = getGC(gc_thin_on_off_line);
985 void LyXScreen::drawFrame(int /*ft*/, int x, int y, int w, int h,
986 FL_COLOR /*col*/, int /*b*/)
988 // Implement this using X11 calls, and the repaint problems are gone!
989 // At least, I think that should do it since we only have them after
990 // one of these buttons are displayed now! Lars, it seems you've hit the
992 // fl_winset(foreground);
993 // fl_drw_frame(ft, x, y, w, h, col, b);
994 // This should be changed to draw a button like frame, in the
995 // mean time we'll just use a regular rectangle
997 // I want the buttons back before 0.12. OK, this is very simple,
998 // like what is done in xforms sources. (Ale)
1000 // This one is too dirty. Get rid of it.
1003 // Please comment this annonymous variable.
1006 // I think these calls to fl_color might make xforms sometimes
1007 // draw the wrong color on other objects.
1008 fl_color(FL_TOP_BCOL);
1009 XFillRectangle(fl_display, foreground, fl_gc,
1010 x - d, y - d, w + 2 * d, d);
1011 fl_color(FL_BOTTOM_BCOL);
1012 XFillRectangle(fl_display, foreground, fl_gc,
1013 x - d, y + h, w + 2 * d, d);
1015 // Now a couple of trapezoids
1016 XPoint pl[4], pr[4];
1018 pl[0].x = x - d; pl[0].y = y - d;
1019 pl[1].x = x - d; pl[1].y = y + h + d;
1020 pl[2].x = x; pl[2].y = y + h;
1021 pl[3].x = x; pl[3].y = y;
1023 pr[0].x = x + w + d; pr[0].y = y - d;
1024 pr[1].x = x + w + d; pr[1].y = y + h + d;
1025 pr[2].x = x + w; pr[2].y = y + h;
1026 pr[3].x = x + w; pr[3].y = y;
1028 fl_color(FL_LEFT_BCOL);
1029 XFillPolygon(fl_display,
1032 Convex, CoordModeOrigin);
1033 fl_color(FL_RIGHT_BCOL);
1034 XFillPolygon(fl_display,
1037 Convex, CoordModeOrigin);