]> git.lyx.org Git - lyx.git/blob - src/screen.C
Small fix.
[lyx.git] / src / screen.C
1 /* This file is part of
2 * ====================================================== 
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright 1995 Matthias Ettrich
7 *           Copyright 1995-1998 The LyX Team
8 *
9 * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation "lyxscreen.h"
15 #endif
16
17 #include <algorithm>
18
19 #include "lyxscreen.h"
20 #include "lyxtext.h"
21 #include "lyxrow.h"
22 #include "Painter.h"
23 #include "WorkArea.h"
24 #include "buffer.h"
25 #include "font.h"
26 #include "insets/insettext.h"
27
28 using std::max;
29 using std::min;
30
31 static
32 GC createGC()
33 {
34         XGCValues val;
35         val.foreground = BlackPixel(fl_display, 
36                                     DefaultScreen(fl_display));
37         
38         val.function=GXcopy;
39         val.graphics_exposures = false;
40         val.line_style = LineSolid;
41         val.line_width = 0;
42         return XCreateGC(fl_display, RootWindow(fl_display, 0), 
43                          GCForeground | GCFunction | GCGraphicsExposures
44                          | GCLineWidth | GCLineStyle , &val);
45 }
46
47
48 // Constructor
49 LyXScreen::LyXScreen(WorkArea & o) //, LyXText * text_ptr)
50         : owner(o) //, text(text_ptr)
51 {
52         // the cursor isnt yet visible
53         cursor_visible = false;
54         cursor_pixmap = 0;
55         cursor_pixmap_x = 0;
56         cursor_pixmap_y = 0;
57         cursor_pixmap_w = 0;
58         cursor_pixmap_h = 0;
59
60         // We need this GC
61         gc_copy = createGC();
62 }
63
64
65 void LyXScreen::Redraw(LyXText * text)
66 {
67         DrawFromTo(text, 0, owner.height());
68         expose(0, 0, owner.workWidth(), owner.height());
69         if (cursor_visible) {
70                 cursor_visible = false;
71                 ShowCursor(text);
72         }
73 }
74
75
76 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
77 {
78         XCopyArea(fl_display,
79                   owner.getPixmap(),
80                   owner.getWin(),
81                   gc_copy,
82                   x, y,
83                   exp_width, exp_height,
84                   x + owner.xpos(),
85                   y + owner.ypos());
86 }
87
88
89 void LyXScreen::DrawFromTo(LyXText * text, int y1, int y2, int y_offset, int x_offset)
90 {
91         long y_text = text->first + y1;
92    
93         // get the first needed row 
94         Row * row = text->GetRowNearY(y_text);
95         // y_text is now the real beginning of the row
96    
97         long y = y_text - text->first;
98         // y1 is now the real beginning of row on the screen
99         
100         while (row != 0 && y < y2) {
101                 LyXText::text_status st = text->status;
102                 do {
103                         text->status = st;
104                         text->GetVisibleRow(owner.owner(), y+y_offset,
105                                             x_offset, row, y + text->first);
106                 } while (text->status == LyXText::CHANGED_IN_DRAW);
107                 text->status = st;
108                 y += row->height();
109                 row = row->next();
110         }
111    
112         // maybe we have to clear the screen at the bottom
113         if ((y < y2) && text->bv_owner) {
114                 owner.getPainter().fillRectangle(0, y,
115                                                  owner.workWidth(),
116                                                  y2 - y,
117                                                LColor::bottomarea);
118         }
119 }
120
121
122 void LyXScreen::DrawOneRow(LyXText * text, Row * row, long y_text,
123                            int y_offset, int x_offset)
124 {
125         long y = y_text - text->first + y_offset;
126       
127         if (y + row->height() > 0
128             && y - row->height() <= long(owner.height())) {
129                 // ok there is something visible
130                 LyXText::text_status st = text->status;
131                 do {
132                         text->status = st;
133                         text->GetVisibleRow(owner.owner(), y, x_offset, row,
134                                             y + text->first);
135                 } while (text->status == LyXText::CHANGED_IN_DRAW);
136                 text->status = st;
137         }
138 }
139
140
141 /* draws the screen, starting with textposition y. uses as much already
142 * printed pixels as possible */
143 void LyXScreen::Draw(LyXText * text, unsigned long y)
144 {
145         if (cursor_visible) HideCursor();
146
147         unsigned long old_first = text->first;
148         text->first = y;
149
150         // is any optimiziation possible?
151         if ((y - old_first) < owner.height()
152             && (old_first - y) < owner.height()) {
153                 if (text->first < old_first) {
154                         DrawFromTo(text, 0, old_first - text->first);
155                         XCopyArea (fl_display,
156                                    owner.getWin(),
157                                    owner.getWin(),
158                                    gc_copy,
159                                    owner.xpos(),
160                                    owner.ypos(),
161                                    owner.workWidth(),
162                                    owner.height() - old_first + text->first,
163                                    owner.xpos(),
164                                    owner.ypos() + old_first - text->first
165                                 );
166                         // expose the area drawn
167                         expose(0, 0,
168                                owner.workWidth(),
169                                old_first - text->first);
170                 } else  {
171                         DrawFromTo(text,
172                                    owner.height() + old_first - text->first,
173                                    owner.height());
174                         XCopyArea (fl_display,
175                                    owner.getWin(),
176                                    owner.getWin(),
177                                    gc_copy,
178                                    owner.xpos(),
179                                    owner.ypos() + text->first - old_first,
180                                    owner.workWidth(),
181                                    owner.height() + old_first - text->first,
182                                    owner.xpos(),
183                                    owner.ypos());
184                         // expose the area drawn
185                         expose(0, owner.height() + old_first - text->first,
186                                owner.workWidth(), text->first - old_first);
187                 }
188         } else {
189                 // make a dumb new-draw 
190                 DrawFromTo(text, 0, owner.height());
191                 expose(0, 0, owner.workWidth(), owner.height());
192         }
193 }
194
195
196 void LyXScreen::ShowCursor(LyXText const * text)
197 {
198         if (!cursor_visible) {
199                 Cursor_Shape shape = BAR_SHAPE;
200                 if (text->real_current_font.language() !=
201                     owner.owner()->buffer()->params.language_info
202                     || text->real_current_font.isVisibleRightToLeft()
203                     != owner.owner()->buffer()->params.language_info->RightToLeft())
204                         shape = (text->real_current_font.isVisibleRightToLeft())
205                                 ? REVERSED_L_SHAPE : L_SHAPE;
206                 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
207                                  lyxfont::maxAscent(text->real_current_font),
208                                  lyxfont::maxDescent(text->real_current_font),
209                                  shape);
210         }
211 }
212
213
214 /* returns true if first has changed, otherwise false */ 
215 bool LyXScreen::FitManualCursor(LyXText * text,
216                                 long /*x*/, long y, int asc, int desc)
217 {
218         long newtop = text->first;
219   
220         if (y + desc - text->first >= owner.height())
221                 newtop = y - 3 * owner.height() / 4;  // the scroll region must be so big!!
222         else if (y - asc < long(text->first)
223                 && text->first > 0) {
224                 newtop = y - owner.height() / 4;
225         }
226
227         newtop = max(newtop, 0L); // can newtop ever be < 0? (Lgb)
228   
229         if (newtop != long(text->first)) {
230                 Draw(text, newtop);
231                 text->first = newtop;
232                 return true;
233         }
234         return false;
235 }
236
237
238 void LyXScreen::ShowManualCursor(LyXText const * text, long x, long y,
239                                  int asc, int desc, Cursor_Shape shape)
240 {
241         unsigned long y1 = max(y - text->first - asc, 0UL);
242         typedef unsigned long ulong;
243         
244         unsigned long y2 = min(y - text->first + desc, ulong(owner.height()));
245
246         // Secure against very strange situations
247         y2 = max(y2, y1);
248         
249         if (cursor_pixmap){
250                 XFreePixmap(fl_display, cursor_pixmap);
251                 cursor_pixmap = 0;
252         }
253
254         if (y2 > 0 && y1 < owner.height()) {
255                 cursor_pixmap_h = y2 - y1 + 1;
256                 cursor_pixmap_y = y1;
257
258                 switch(shape) {
259                 case BAR_SHAPE:
260                         cursor_pixmap_w = 1;
261                         cursor_pixmap_x = x;
262                         break;
263                 case L_SHAPE:
264                         cursor_pixmap_w = cursor_pixmap_h/3;
265                         cursor_pixmap_x = x;
266                         break;
267                 case REVERSED_L_SHAPE:
268                         cursor_pixmap_w = cursor_pixmap_h/3;
269                         cursor_pixmap_x = x - cursor_pixmap_w + 1;
270                         break;
271                 }
272
273                 cursor_pixmap = 
274                         XCreatePixmap (fl_display,
275                                        fl_root,
276                                        cursor_pixmap_w,
277                                        cursor_pixmap_h,
278                                        fl_get_visual_depth());
279                 XCopyArea (fl_display,
280                            owner.getWin(),
281                            cursor_pixmap,
282                            gc_copy,
283                            owner.xpos() + cursor_pixmap_x,
284                            owner.ypos() + cursor_pixmap_y,
285                            cursor_pixmap_w,
286                            cursor_pixmap_h,
287                            0, 0);
288                 XDrawLine(fl_display,
289                           owner.getWin(),
290                           gc_copy,
291                           x + owner.xpos(),
292                           y1 + owner.ypos(),
293                           x + owner.xpos(),
294                           y2 + owner.ypos());
295                 switch(shape) {
296                 case BAR_SHAPE:
297                         break;
298                 case L_SHAPE:
299                 case REVERSED_L_SHAPE:
300                         int rectangle_h = (cursor_pixmap_h+10)/20;
301                         XFillRectangle(fl_display,
302                                        owner.getWin(),
303                                        gc_copy,
304                                        cursor_pixmap_x + owner.xpos(),
305                                        y2 - rectangle_h + 1 + owner.ypos(),
306                                        cursor_pixmap_w - 1, rectangle_h);
307                         break;
308                 }
309
310         }
311         cursor_visible = true;
312 }
313
314
315 void LyXScreen::HideCursor()
316 {
317         if (!cursor_visible) return;
318
319         if (cursor_pixmap){
320                 XCopyArea (fl_display, 
321                            cursor_pixmap,
322                            owner.getWin(),
323                            gc_copy,
324                            0, 0, 
325                            cursor_pixmap_w, cursor_pixmap_h,
326                            cursor_pixmap_x + owner.xpos(),
327                            cursor_pixmap_y + owner.ypos());
328         }
329         cursor_visible = false;
330 }
331
332
333 void LyXScreen::CursorToggle(LyXText const * text)
334 {
335         if (cursor_visible)
336                 HideCursor();
337         else
338                 ShowCursor(text);
339 }
340
341
342 /* returns a new top so that the cursor is visible */ 
343 unsigned long LyXScreen::TopCursorVisible(LyXText const * text)
344 {
345         long newtop = text->first;
346
347         if (text->cursor.y()
348             - text->cursor.row()->baseline()
349             + text->cursor.row()->height()
350             - text->first >= owner.height()) {
351                 if (text->cursor.row()->height() < owner.height()
352                     && text->cursor.row()->height() > owner.height() / 4)
353                         newtop = text->cursor.y()
354                                 + text->cursor.row()->height()
355                                 - text->cursor.row()->baseline() - owner.height();
356                 else
357                         newtop = text->cursor.y()
358                                 - 3 * owner.height() / 4;   /* the scroll region must be so big!! */
359         } else if (text->cursor.y() - text->cursor.row()->baseline() < text->first
360                    && text->first > 0) {
361                 if (text->cursor.row()->height() < owner.height()
362                     && text->cursor.row()->height() > owner.height() / 4)
363                         newtop = text->cursor.y() - text->cursor.row()->baseline();
364                 else {
365                         newtop = text->cursor.y() - owner.height() / 4;
366                         newtop = min(newtop, long(text->first));
367                 }
368         }
369
370         newtop = max(newtop, 0L);
371
372         return newtop;
373 }
374
375
376 /* scrolls the screen so that the cursor is visible, if necessary.
377 * returns true if a change was made, otherwise false */ 
378 bool LyXScreen::FitCursor(LyXText * text)
379 {
380         // Is a change necessary?
381         unsigned long newtop = TopCursorVisible(text);
382         bool result = (newtop != text->first);
383         if (result)
384                 Draw(text, newtop);
385         return result;
386 }
387
388    
389 void LyXScreen::Update(LyXText * text, int y_offset, int x_offset)
390 {
391         switch(text->status) {
392         case LyXText::NEED_MORE_REFRESH:
393         {
394                 long y = max(text->refresh_y - long(text->first), 0L);
395                 int height;
396                 if (text->inset_owner)
397                         height = text->inset_owner->ascent(owner.owner(),
398                                                            text->real_current_font)
399                                 + text->inset_owner->descent(owner.owner(),
400                                                              text->real_current_font);
401                 else
402                         height = owner.height();
403                 DrawFromTo(text, y, owner.height(), y_offset, x_offset);
404                 text->refresh_y = 0;
405                 text->status = LyXText::UNCHANGED;
406                 expose(0, y,
407                        owner.workWidth(), owner.height() - y);
408         }
409         break;
410         case LyXText::NEED_VERY_LITTLE_REFRESH:
411         {
412                 // ok I will update the current cursor row
413                 DrawOneRow(text, text->refresh_row, text->refresh_y,
414                            y_offset, x_offset);
415                 text->status = LyXText::UNCHANGED;
416                 expose(0, text->refresh_y - text->first + y_offset,
417                        owner.workWidth(), text->refresh_row->height());
418         }
419         break;
420         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
421         case LyXText::UNCHANGED:
422                 // Nothing needs done
423                 break;
424         }
425 }
426
427
428 void LyXScreen::ToggleSelection(LyXText * text,  bool kill_selection,
429                                 int y_offset, int x_offset)
430 {
431         // only if there is a selection
432         if (!text->selection) return;
433
434         long bottom = min(max(text->sel_end_cursor.y()
435                               - text->sel_end_cursor.row()->baseline()
436                               + text->sel_end_cursor.row()->height(), text->first),
437                           text->first + owner.height());
438         long top = min(max(text->sel_start_cursor.y()
439                            - text->sel_start_cursor.row()->baseline(), text->first),
440                        text->first + owner.height());
441
442         if (kill_selection)
443                 text->selection = 0;
444         DrawFromTo(text, top - text->first, bottom - text->first,
445                    y_offset, x_offset);
446         expose(0, top - text->first,
447                owner.workWidth(),
448                bottom - text->first - (top - text->first));
449 }
450   
451    
452 void LyXScreen::ToggleToggle(LyXText * text, int y_offset, int x_offset)
453 {
454         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
455             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
456                 return;
457         
458         long top = text->toggle_cursor.y()
459                 - text->toggle_cursor.row()->baseline();
460         long bottom = text->toggle_end_cursor.y()
461                 - text->toggle_end_cursor.row()->baseline() 
462                 + text->toggle_end_cursor.row()->height();
463         
464         typedef unsigned long ulong;
465         
466         bottom = min(max(ulong(bottom), text->first), text->first + owner.height());
467         top = min(max(ulong(top), text->first), text->first + owner.height());
468
469         DrawFromTo(text, top - text->first, bottom - text->first, y_offset,
470                    x_offset);
471         expose(0, top - text->first, owner.workWidth(),
472                bottom - text->first - (top - text->first));
473 }