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"
21 #include "BufferView.h"
24 extern int mono_video;
25 extern int fast_selection;
32 val.foreground = BlackPixel(fl_display,
33 DefaultScreen(fl_display));
36 val.graphics_exposures = false;
37 val.line_style = LineSolid;
39 return XCreateGC(fl_display, RootWindow(fl_display, 0),
40 GCForeground | GCFunction | GCGraphicsExposures
41 | GCLineWidth | GCLineStyle , &val);
47 LyXScreen::LyXScreen(BufferView * o, Window window,
56 : owner(o), text(text_ptr), _window(window),
57 _width(width), _height(height),
58 _offset_x(offset_x), _offset_y(offset_y)
62 /* the cursor isnt yet visible */
63 cursor_visible = false;
64 screen_refresh_y = -1;
66 /* create the foreground pixmap */
70 foreground = XCreatePixmap (fl_display,
73 fl_get_visual_depth());
89 LyXScreen::~LyXScreen()
92 XFreePixmap(fl_display, foreground);
97 void LyXScreen::Redraw()
99 DrawFromTo(0, _height);
100 screen_refresh_y = -1;
101 expose(0, 0, _width, _height);
102 if (cursor_visible) {
103 cursor_visible = false;
110 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
112 XCopyArea(fl_display,
117 exp_width, exp_height,
118 x+_offset_x, y+_offset_y);
121 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
123 XCopyArea(fl_display,
128 exp_width, exp_height,
129 x+_offset_x, y+_offset_y);
135 void LyXScreen::DrawFromTo(int y1, int y2)
137 long y_text = first + y1;
139 /* get the first needed row */
140 Row * row = text->GetRowNearY(y_text);
141 /* y_text is now the real beginning of the row */
143 long y = y_text - first;
144 /* y1 is now the real beginning of row on the screen */
146 while (row != 0 && y < y2) {
148 text->GetVisibleRow(y, row, y + first);
154 /* maybe we have to clear the screen at the bottom */
156 owner->painter().fillRectangle(0, y, _width, y2 - y,
161 void LyXScreen::DrawFromTo(int y1, int y2)
163 long y_text = first + y1;
165 /* get the first needed row */
166 Row * row = text->GetRowNearY(y_text);
167 /* y_text is now the real beginning of the row */
169 long y = y_text - first;
170 /* y1 is now the real beginning of row on the screen */
172 while (row != 0 && y < y2) {
174 text->GetVisibleRow(*this, y, row, y + first);
180 /* maybe we have to clear the screen at the bottom */
182 fillRectangle(gc_lighted,
192 void LyXScreen::DrawOneRow(Row * row, long & y_text)
194 long y = y_text - first;
196 if (y + row->height > 0 && y - row->height <= _height) {
197 /* ok there is something visible */
199 text->GetVisibleRow(y, row, y + first);
201 text->GetVisibleRow(*this, y, row, y + first);
204 y_text+= row->height;
208 /* draws the screen, starting with textposition y. uses as much already
209 * printed pixels as possible */
211 void LyXScreen::Draw(long y)
213 if (cursor_visible) HideCursor();
216 long old_first = first;
219 /* is any optimiziation possible? */
220 if ((y - old_first) < _height
221 && (old_first - y) < _height) {
222 if (first < old_first) {
223 DrawFromTo(0, old_first - first);
224 XCopyArea (fl_display,
228 _offset_x, _offset_y,
229 _width , _height - old_first + first,
230 _offset_x, _offset_y + old_first - first);
231 // expose the area drawn
232 expose(0, 0, _width, old_first - first);
234 DrawFromTo(_height + old_first - first, _height);
235 XCopyArea (fl_display,
239 _offset_x, _offset_y + first - old_first,
240 _width , _height + old_first - first,
241 _offset_x, _offset_y);
242 // expose the area drawn
243 expose(0, _height + old_first - first,
244 _width, first - old_first);
247 /* make a dumb new-draw */
248 DrawFromTo(0, _height);
249 expose(0, 0, _width, _height);
253 void LyXScreen::Draw(long y)
255 if (cursor_visible) HideCursor();
258 long old_first = first;
261 /* is any optimiziation possible? */
262 if ((y - old_first) < _height
263 && (old_first - y) < _height) {
264 if (first < old_first) {
265 DrawFromTo(0, old_first - first);
266 XCopyArea (fl_display,
270 _offset_x, _offset_y,
271 _width , _height - old_first + first,
272 _offset_x, _offset_y + old_first - first);
273 // expose the area drawn
274 expose(0, 0, _width, old_first - first);
276 DrawFromTo(_height + old_first - first, _height);
277 XCopyArea (fl_display,
281 _offset_x, _offset_y + first - old_first,
282 _width , _height + old_first - first,
283 _offset_x, _offset_y);
284 // expose the area drawn
285 expose(0, _height + old_first - first,
286 _width, first - old_first);
289 /* make a dumb new-draw */
290 DrawFromTo(0, _height);
291 expose(0, 0, _width, _height);
297 void LyXScreen::ShowCursor()
303 if (cursor_visible) return;
307 y1 = text->cursor.y - text->real_current_font.maxAscent() - first;
310 y2 = text->cursor.y + text->real_current_font.maxDescent() - first;
311 if (y2 > _height) y2 = _height;
313 // Secure against very strange situations
314 if (y2 < y1) y2 = y1;
317 if (fast_selection || mono_video) {
318 if (y2 > 0 && y1 < _height) {
319 XDrawLine(fl_display,
320 _window, getGC(gc_select),
325 cursor_visible = true;
330 XFreePixmap(fl_display, cursor_pixmap);
334 if (y2 > 0 && y1 < _height) {
336 cursor_pixmap_h = y2 - y1 + 1;
338 cursor_pixmap_y = y1;
340 XCreatePixmap(fl_display,
344 fl_get_visual_depth());
346 XCopyArea(fl_display,
350 _offset_x + cursor_pixmap_x,
351 _offset_y + cursor_pixmap_y,
352 cursor_pixmap_w, cursor_pixmap_h,
354 XDrawLine(fl_display,
361 XCopyArea(fl_display,
365 _offset_x + cursor_pixmap_x,
366 _offset_y + cursor_pixmap_y,
367 cursor_pixmap_w, cursor_pixmap_h,
369 XDrawLine(fl_display,
377 cursor_visible = true;
385 /* returns 1 if first has changed, otherwise 0 */
386 int LyXScreen::FitManualCursor(long /*x*/, long y, int asc, int desc)
390 if (y + desc - first >= _height)
391 newtop = y - 3*_height / 4; /* the scroll region must be so big!! */
392 else if (y - asc < first
394 newtop = y - _height / 4;
399 if (newtop != first){
408 void LyXScreen::HideManualCursor(long x, long y, int asc, int desc)
410 if (fast_selection || mono_video)
411 ShowManualCursor(x, y, asc, desc);
417 void LyXScreen::ShowManualCursor(long x, long y, int asc, int desc)
422 y1 = y - first - asc;
425 y2 = y -first + desc;
430 if (fast_selection || mono_video) {
431 if (y2 > 0 && y1 < _height) {
432 XDrawLine(fl_display,
433 _window, getGC(gc_select),
442 XFreePixmap(fl_display, cursor_pixmap);
446 if (y2 > 0 && y1 < _height) {
448 cursor_pixmap_h = y2 - y1 + 1;
450 cursor_pixmap_y = y1;
452 XCreatePixmap (fl_display,
456 fl_get_visual_depth());
458 XCopyArea (fl_display,
462 _offset_x + cursor_pixmap_x,
463 _offset_y + cursor_pixmap_y,
467 XDrawLine(fl_display,
474 XCopyArea (fl_display,
478 _offset_x + cursor_pixmap_x,
479 _offset_y + cursor_pixmap_y,
483 XDrawLine(fl_display,
492 cursor_visible = true;
499 void LyXScreen::HideCursor()
501 if (!cursor_visible) return;
504 if (fast_selection || mono_video) {
505 cursor_visible = false;
507 cursor_visible = false;
512 XCopyArea (fl_display,
517 cursor_pixmap_w, cursor_pixmap_h,
518 cursor_pixmap_x + _offset_x,
519 cursor_pixmap_y + _offset_y);
521 XCopyArea (fl_display,
526 cursor_pixmap_w, cursor_pixmap_h,
527 cursor_pixmap_x + _offset_x,
528 cursor_pixmap_y + _offset_y);
531 cursor_visible = false;
538 void LyXScreen::CursorToggle()
547 /* returns a new top so that the cursor is visible */
548 long LyXScreen::TopCursorVisible()
553 - text->cursor.row->baseline
554 + text->cursor.row->height
555 - first >= _height) {
556 if (text->cursor.row->height < _height
557 && text->cursor.row->height > _height/4)
558 newtop = text->cursor.y
559 + text->cursor.row->height
560 - text->cursor.row->baseline - _height;
562 newtop = text->cursor.y
563 - 3*_height / 4; /* the scroll region must be so big!! */
564 } else if (text->cursor.y - text->cursor.row->baseline < first
566 if (text->cursor.row->height < _height
567 && text->cursor.row->height > _height/4)
568 newtop = text->cursor.y - text->cursor.row->baseline;
570 newtop = text->cursor.y - _height / 4;
582 /* scrolls the screen so that the cursor is visible, if necessary.
583 * returns 1 if a change was made, otherwise 0 */
584 int LyXScreen::FitCursor()
586 /* is a change necessary */
587 long newtop = TopCursorVisible();
588 int result = (newtop != first);
595 void LyXScreen::Update()
599 if (text->status == LyXText::NEED_MORE_REFRESH
600 || screen_refresh_y > -1 ) {
601 if (screen_refresh_y > -1
602 && screen_refresh_y < text->refresh_y)
603 y = screen_refresh_y;
607 if (y < first) y = first;
609 DrawFromTo(y - first, _height);
611 text->status = LyXText::UNCHANGED;
612 screen_refresh_y = -1;
613 expose(0, y-first, _width, _height - (y - first));
614 } else if (text->status == LyXText::NEED_VERY_LITTLE_REFRESH) {
615 /* ok I will update the current cursor row */
617 DrawOneRow(text->refresh_row, y);
618 text->status = LyXText::UNCHANGED;
619 expose(0, text->refresh_y-first,
620 _width, text->refresh_row->height);
625 void LyXScreen::SmallUpdate()
631 if (text->status == LyXText::NEED_MORE_REFRESH){
632 /* ok I will update till the current cursor row */
633 row = text->refresh_row;
637 if (y > text->cursor.y) {
642 while (row && row != text->cursor.row && y < first + _height) {
648 screen_refresh_y = y;
649 screen_refresh_row = row->next;
650 text->status = LyXText::UNCHANGED;
651 // Is the right regin exposed?
652 expose(0, y2-first, _width, y-y2);
653 } else if (text->status == LyXText::NEED_VERY_LITTLE_REFRESH) {
654 /* ok I will update the current cursor row */
655 row = text->refresh_row;
658 text->status = LyXText::UNCHANGED;
659 expose(0, text->refresh_y - first,
660 _width, row->height);
665 void LyXScreen::ToggleSelection(bool kill_selection)
667 /* only if there is a selection */
668 if (!text->selection)
675 if (fast_selection || mono_video) {
677 /* selection only in one row ?*/
678 if (text->sel_start_cursor.y == text->sel_end_cursor.y) {
680 /* only if something is visible */
681 if (text->sel_start_cursor.y
682 - text->sel_start_cursor.row->baseline
684 && text->sel_start_cursor.y
685 - text->sel_start_cursor.row->baseline +
686 text->sel_start_cursor.row->height - first > 0) {
687 top = text->sel_start_cursor.y
688 - text->sel_start_cursor.row->baseline
691 + text->sel_start_cursor.row->height;
694 if (bottom > _height)
696 XFillRectangle(fl_display, _window,
698 text->sel_start_cursor.x
701 text->sel_end_cursor.x
702 - text->sel_start_cursor.x,
706 /* the sel_start_cursor row first */
707 /* only if anything is visible */
708 if (text->sel_start_cursor.y
709 - text->sel_start_cursor.row->baseline
711 && text->sel_start_cursor.y
712 - text->sel_start_cursor.row->baseline +
713 text->sel_start_cursor.row->height - first > 0) {
714 top = text->sel_start_cursor.y
715 - text->sel_start_cursor.row->baseline
718 + text->sel_start_cursor.row->height;
721 if (bottom > _height)
723 XFillRectangle(fl_display, _window,
725 text->sel_start_cursor.x
729 - text->sel_start_cursor.x,
735 if (text->sel_start_cursor.row->next !=
736 text->sel_end_cursor.row) {
737 top = text->sel_start_cursor.y
738 - text->sel_start_cursor.row->baseline
739 + text->sel_start_cursor.row->height;
740 bottom = text->sel_end_cursor.y
741 - text->sel_end_cursor.row->baseline;
745 if (bottom - first < 0)
748 if (bottom - first > _height)
749 bottom = first + _height;
750 if (top - first > _height)
751 top = first + _height;
754 XFillRectangle(fl_display,
758 top - first+_offset_y,
764 /* the sel_end_cursor row last */
765 if (text->sel_end_cursor.y
766 - text->sel_end_cursor.row->baseline
768 && text->sel_end_cursor.y
769 - text->sel_end_cursor.row->baseline +
770 text->sel_end_cursor.row->height - first > 0) {
771 top = text->sel_end_cursor.y
772 - text->sel_end_cursor.row->baseline
775 + text->sel_end_cursor.row->height;
778 if (bottom > _height)
780 XFillRectangle(fl_display, _window,
784 text->sel_end_cursor.x,
790 top = text->sel_start_cursor.y
791 - text->sel_start_cursor.row->baseline;
792 bottom = text->sel_end_cursor.y
793 - text->sel_end_cursor.row->baseline
794 + text->sel_end_cursor.row->height;
798 if (bottom - first < 0)
801 if (bottom - first > _height)
802 bottom = first + _height;
803 if (top - first > _height)
804 top = first + _height;
808 DrawFromTo(top - first, bottom - first);
809 expose(0, top - first, _width, bottom - first - (top - first));
816 void LyXScreen::ToggleToggle()
821 if (text->toggle_cursor.par == text->toggle_end_cursor.par
822 && text->toggle_cursor.pos == text->toggle_end_cursor.pos)
826 if (fast_selection || mono_video) {
828 /* selection only in one row ?*/
829 if (text->toggle_cursor.y == text->toggle_end_cursor.y) {
831 /* only if anything is visible */
832 if (text->toggle_cursor.y - text->toggle_cursor.row->baseline - first < _height
833 && text->toggle_cursor.y - text->toggle_cursor.row->baseline +
834 text->toggle_cursor.row->height - first > 0) {
835 top = text->toggle_cursor.y - text->toggle_cursor.row->baseline - first;
836 bottom = top + text->toggle_cursor.row->height;
837 if (top < 0) top = 0;
838 if (bottom > _height) bottom = _height;
839 XFillRectangle(fl_display, _window,
841 text->toggle_cursor.x+_offset_x,
843 text->toggle_end_cursor.x -
844 text->toggle_cursor.x,
848 /* the toggle_cursor row first */
849 /* only if anything is visible */
850 if (text->toggle_cursor.y - text->toggle_cursor.row->baseline - first < _height
851 && text->toggle_cursor.y - text->toggle_cursor.row->baseline +
852 text->toggle_cursor.row->height - first > 0) {
853 top = text->toggle_cursor.y - text->toggle_cursor.row->baseline - first;
854 bottom = top + text->toggle_cursor.row->height;
857 if (bottom > _height)
859 XFillRectangle(fl_display, _window,
861 text->toggle_cursor.x+_offset_x,
863 _width - text->toggle_cursor.x,
869 if (text->toggle_cursor.row->next !=
870 text->toggle_end_cursor.row) {
871 top = text->toggle_cursor.y
872 - text->toggle_cursor.row->baseline
873 + text->toggle_cursor.row->height;
874 bottom = text->toggle_end_cursor.y
875 - text->toggle_end_cursor.row->baseline;
879 if (bottom - first < 0)
882 if (bottom - first > _height)
883 bottom = first + _height;
884 if (top - first > _height)
885 top = first + _height;
888 XFillRectangle(fl_display, _window,
891 top - first+_offset_y,
897 /* the toggle_end_cursor row last */
898 if (text->toggle_end_cursor.y - text->toggle_end_cursor.row->baseline - first < _height
899 && text->toggle_end_cursor.y - text->toggle_end_cursor.row->baseline +
900 text->toggle_end_cursor.row->height - first > 0) {
901 top = text->toggle_end_cursor.y
902 - text->toggle_end_cursor.row->baseline
905 + text->toggle_end_cursor.row->height;
908 if (bottom > _height)
910 XFillRectangle(fl_display, _window,
914 text->toggle_end_cursor.x,
920 top = text->toggle_cursor.y
921 - text->toggle_cursor.row->baseline;
922 bottom = text->toggle_end_cursor.y
923 - text->toggle_end_cursor.row->baseline
924 + text->toggle_end_cursor.row->height;
928 if (bottom - first < 0)
931 if (bottom - first > _height)
932 bottom = first + _height;
933 if (top - first > _height)
934 top = first + _height;
936 DrawFromTo(top - first, bottom - first);
937 expose(0, top - first, _width, bottom - first - (top - first));
945 void LyXScreen::drawTableLine(int baseline, int x, int length, bool on_off)
949 gc = getGC(gc_thin_on_off_line);
962 void LyXScreen::drawVerticalTableLine(int x, int y1, int y2, bool on_off)
966 gc = getGC(gc_thin_on_off_line);
979 void LyXScreen::drawFrame(int /*ft*/, int x, int y, int w, int h,
980 FL_COLOR /*col*/, int /*b*/)
982 // Implement this using X11 calls, and the repaint problems are gone!
983 // At least, I think that should do it since we only have them after
984 // one of these buttons are displayed now! Lars, it seems you've hit the
986 // fl_winset(foreground);
987 // fl_drw_frame(ft, x, y, w, h, col, b);
988 // This should be changed to draw a button like frame, in the
989 // mean time we'll just use a regular rectangle
991 // I want the buttons back before 0.12. OK, this is very simple,
992 // like what is done in xforms sources. (Ale)
994 // This one is too dirty. Get rid of it.
997 // Please comment this annonymous variable.
1000 // I think these calls to fl_color might make xforms sometimes
1001 // draw the wrong color on other objects.
1002 fl_color(FL_TOP_BCOL);
1003 XFillRectangle(fl_display, foreground, fl_gc,
1004 x - d, y - d, w + 2 * d, d);
1005 fl_color(FL_BOTTOM_BCOL);
1006 XFillRectangle(fl_display, foreground, fl_gc,
1007 x - d, y + h, w + 2 * d, d);
1009 // Now a couple of trapezoids
1010 XPoint pl[4], pr[4];
1012 pl[0].x = x - d; pl[0].y = y - d;
1013 pl[1].x = x - d; pl[1].y = y + h + d;
1014 pl[2].x = x; pl[2].y = y + h;
1015 pl[3].x = x; pl[3].y = y;
1017 pr[0].x = x + w + d; pr[0].y = y - d;
1018 pr[1].x = x + w + d; pr[1].y = y + h + d;
1019 pr[2].x = x + w; pr[2].y = y + h;
1020 pr[3].x = x + w; pr[3].y = y;
1022 fl_color(FL_LEFT_BCOL);
1023 XFillPolygon(fl_display,
1026 Convex, CoordModeOrigin);
1027 fl_color(FL_RIGHT_BCOL);
1028 XFillPolygon(fl_display,
1031 Convex, CoordModeOrigin);