]> git.lyx.org Git - lyx.git/blob - src/screen.C
remove Last when NEW_INSETS is defined
[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 #include "ColorHandler.h"
28
29 using std::max;
30 using std::min;
31
32 static
33 GC createGC()
34 {
35         XGCValues val;
36         val.foreground = BlackPixel(fl_get_display(), 
37                                     DefaultScreen(fl_get_display()));
38         
39         val.function = GXcopy;
40         val.graphics_exposures = false;
41         val.line_style = LineSolid;
42         val.line_width = 0;
43         return XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0), 
44                          GCForeground | GCFunction | GCGraphicsExposures
45                          | GCLineWidth | GCLineStyle , &val);
46 }
47
48
49 // Constructor
50 LyXScreen::LyXScreen(WorkArea & o)
51         : owner(o), force_clear(true)
52 {
53         // the cursor isnt yet visible
54         cursor_visible = false;
55         cursor_pixmap = 0;
56         cursor_pixmap_x = 0;
57         cursor_pixmap_y = 0;
58         cursor_pixmap_w = 0;
59         cursor_pixmap_h = 0;
60
61         // We need this GC
62         gc_copy = createGC();
63 }
64
65
66 void LyXScreen::setCursorColor() 
67 {
68         if (!lyxColorHandler.get()) return;
69
70         GC gc = lyxColorHandler->getGCForeground(LColor::cursor);
71         
72         XGCValues val;
73         XGetGCValues(fl_get_display(),
74                      gc, GCForeground, &val);
75         XChangeGC(fl_get_display(), gc_copy, GCForeground, &val);
76 }
77
78
79 void LyXScreen::Redraw(LyXText * text, BufferView * bv)
80 {
81         DrawFromTo(text, bv, 0, owner.height());
82         expose(0, 0, owner.workWidth(), owner.height());
83         if (cursor_visible) {
84                 cursor_visible = false;
85                 ShowCursor(text, bv);
86         }
87 }
88
89
90 void LyXScreen::expose(int x, int y, int exp_width, int exp_height)
91 {
92         XCopyArea(fl_get_display(),
93                   owner.getPixmap(),
94                   owner.getWin(),
95                   gc_copy,
96                   x, y,
97                   exp_width, exp_height,
98                   x + owner.xpos(),
99                   y + owner.ypos());
100 }
101
102
103 void LyXScreen::DrawFromTo(LyXText * text, BufferView * bv,
104                            int y1, int y2, int y_offset, int x_offset)
105 {
106         int y_text = text->first + y1;
107    
108         // get the first needed row 
109         Row * row = text->GetRowNearY(y_text);
110         // y_text is now the real beginning of the row
111    
112         int y = y_text - text->first;
113         // y1 is now the real beginning of row on the screen
114         
115         while (row != 0 && y < y2) {
116                 LyXText::text_status st = bv->text->status;
117                 do {
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;
123                 y += row->height();
124                 row = row->next();
125         }
126         force_clear = false;
127
128         // maybe we have to clear the screen at the bottom
129         if ((y < y2) && text->bv_owner) {
130                 owner.getPainter().fillRectangle(0, y,
131                                                  owner.workWidth(),
132                                                  y2 - y,
133                                                LColor::bottomarea);
134         }
135 }
136
137
138 void LyXScreen::DrawOneRow(LyXText * text, BufferView * bv, Row * row,
139                            int y_text, int y_offset, int x_offset)
140 {
141         int const y = y_text - text->first + y_offset;
142
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;
147                 do {
148                         bv->text->status = st;
149                         text->GetVisibleRow(bv, y, x_offset, row,
150                                             y + text->first);
151                 } while (bv->text->status == LyXText::CHANGED_IN_DRAW);
152                 bv->text->status = st;
153         }
154         force_clear = false;
155 }
156
157
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)
161 {
162         if (cursor_visible) HideCursor();
163
164         int const old_first = text->first;
165         text->first = y;
166
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(),
173                                    owner.getWin(),
174                                    owner.getWin(),
175                                    gc_copy,
176                                    owner.xpos(),
177                                    owner.ypos(),
178                                    owner.workWidth(),
179                                    owner.height() - old_first + text->first,
180                                    owner.xpos(),
181                                    owner.ypos() + old_first - text->first
182                                 );
183                         // expose the area drawn
184                         expose(0, 0,
185                                owner.workWidth(),
186                                old_first - text->first);
187                 } else  {
188                         DrawFromTo(text, bv,
189                                    owner.height() + old_first - text->first,
190                                    owner.height());
191                         XCopyArea (fl_get_display(),
192                                    owner.getWin(),
193                                    owner.getWin(),
194                                    gc_copy,
195                                    owner.xpos(),
196                                    owner.ypos() + text->first - old_first,
197                                    owner.workWidth(),
198                                    owner.height() + old_first - text->first,
199                                    owner.xpos(),
200                                    owner.ypos());
201                         // expose the area drawn
202                         expose(0, owner.height() + old_first - text->first,
203                                owner.workWidth(), text->first - old_first);
204                 }
205         } else {
206                 // make a dumb new-draw 
207                 DrawFromTo(text, bv, 0, owner.height());
208                 expose(0, 0, owner.workWidth(), owner.height());
209         }
210 }
211
212
213 void LyXScreen::ShowCursor(LyXText const * text, BufferView const * bv)
214 {
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),
226                                  shape);
227         }
228 }
229
230
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)
234 {
235         int newtop = text->first;
236   
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;
242         }
243
244         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
245   
246         if (newtop != static_cast<int>(text->first)) {
247                 Draw(text, bv, newtop);
248                 text->first = newtop;
249                 return true;
250         }
251         return false;
252 }
253
254
255 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
256                                  int asc, int desc, Cursor_Shape shape)
257 {
258         // Update the cursor color.
259         setCursorColor();
260         
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()));
264
265         // Secure against very strange situations
266         int const y2 = max(y_tmp, y1);
267         
268         if (cursor_pixmap){
269                 XFreePixmap(fl_get_display(), cursor_pixmap);
270                 cursor_pixmap = 0;
271         }
272
273         if (y2 > 0 && y1 < int(owner.height())) {
274                 cursor_pixmap_h = y2 - y1 + 1;
275                 cursor_pixmap_y = y1;
276
277                 switch (shape) {
278                 case BAR_SHAPE:
279                         cursor_pixmap_w = 1;
280                         cursor_pixmap_x = x;
281                         break;
282                 case L_SHAPE:
283                         cursor_pixmap_w = cursor_pixmap_h/3;
284                         cursor_pixmap_x = x;
285                         break;
286                 case REVERSED_L_SHAPE:
287                         cursor_pixmap_w = cursor_pixmap_h/3;
288                         cursor_pixmap_x = x - cursor_pixmap_w + 1;
289                         break;
290                 }
291
292                 cursor_pixmap = 
293                         XCreatePixmap (fl_get_display(),
294                                        fl_root,
295                                        cursor_pixmap_w,
296                                        cursor_pixmap_h,
297                                        fl_get_visual_depth());
298                 XCopyArea (fl_get_display(),
299                            owner.getWin(),
300                            cursor_pixmap,
301                            gc_copy,
302                            owner.xpos() + cursor_pixmap_x,
303                            owner.ypos() + cursor_pixmap_y,
304                            cursor_pixmap_w,
305                            cursor_pixmap_h,
306                            0, 0);
307                 XDrawLine(fl_get_display(),
308                           owner.getWin(),
309                           gc_copy,
310                           x + owner.xpos(),
311                           y1 + owner.ypos(),
312                           x + owner.xpos(),
313                           y2 + owner.ypos());
314                 switch (shape) {
315                 case BAR_SHAPE:
316                         break;
317                 case L_SHAPE:
318                 case REVERSED_L_SHAPE:
319                         int const rectangle_h = (cursor_pixmap_h + 10) / 20;
320                         XFillRectangle(fl_get_display(),
321                                        owner.getWin(),
322                                        gc_copy,
323                                        cursor_pixmap_x + owner.xpos(),
324                                        y2 - rectangle_h + 1 + owner.ypos(),
325                                        cursor_pixmap_w - 1, rectangle_h);
326                         break;
327                 }
328
329         }
330         cursor_visible = true;
331 }
332
333
334 void LyXScreen::HideCursor()
335 {
336         if (!cursor_visible) return;
337
338         if (cursor_pixmap){
339                 XCopyArea (fl_get_display(), 
340                            cursor_pixmap,
341                            owner.getWin(),
342                            gc_copy,
343                            0, 0, 
344                            cursor_pixmap_w, cursor_pixmap_h,
345                            cursor_pixmap_x + owner.xpos(),
346                            cursor_pixmap_y + owner.ypos());
347         }
348         cursor_visible = false;
349 }
350
351
352 void LyXScreen::CursorToggle(LyXText const * text, BufferView const * bv)
353 {
354         if (cursor_visible)
355                 HideCursor();
356         else
357                 ShowCursor(text, bv);
358 }
359
360
361 /* returns a new top so that the cursor is visible */ 
362 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
363 {
364         int newtop = text->first;
365
366         if (text->cursor.y()
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();
375                 else
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)
380         {
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();
384                 else {
385                         newtop = text->cursor.y() - owner.height() / 4;
386                         newtop = min(newtop, int(text->first));
387                 }
388         }
389
390         newtop = max(newtop, 0);
391
392         return newtop;
393 }
394
395
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)
399 {
400         // Is a change necessary?
401         int const newtop = TopCursorVisible(text);
402         bool const result = (newtop != text->first);
403         if (result)
404                 Draw(text, bv, newtop);
405         return result;
406 }
407
408    
409 void LyXScreen::Update(LyXText * text, BufferView * bv,
410                        int y_offset, int x_offset)
411 {
412         switch (text->status) {
413         case LyXText::NEED_MORE_REFRESH:
414         {
415                 int const y = max(int(text->refresh_y - text->first), 0);
416                 DrawFromTo(text, bv, y, owner.height(), y_offset, x_offset);
417                 text->refresh_y = 0;
418                 text->status = LyXText::UNCHANGED;
419                 expose(0, y, owner.workWidth(), owner.height() - y);
420         }
421         break;
422         case LyXText::NEED_VERY_LITTLE_REFRESH:
423         {
424                 // ok I will update the current cursor row
425                 DrawOneRow(text, bv, text->refresh_row, text->refresh_y,
426                            y_offset, x_offset);
427                 text->status = LyXText::UNCHANGED;
428                 expose(0, text->refresh_y - text->first + y_offset,
429                        owner.workWidth(), text->refresh_row->height());
430         }
431         break;
432         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
433         case LyXText::UNCHANGED:
434                 // Nothing needs done
435                 break;
436         }
437 }
438
439
440 void LyXScreen::ToggleSelection(LyXText * text, BufferView * bv,
441                                 bool kill_selection,
442                                 int y_offset, int x_offset)
443 {
444         // only if there is a selection
445         if (!text->selection) return;
446
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()),
451                     text->first),
452                 static_cast<int>(text->first + owner.height()));
453         int const top = min(
454                 max(static_cast<int>(text->sel_start_cursor.y() -
455                                      text->sel_start_cursor.row()->baseline()),
456                     text->first),
457                 static_cast<int>(text->first + owner.height()));
458
459         if (kill_selection)
460                 text->selection = 0;
461         DrawFromTo(text, bv, top - text->first, bottom - text->first,
462                    y_offset, x_offset);
463         expose(0, top - text->first,
464                owner.workWidth(),
465                bottom - text->first - (top - text->first));
466 }
467   
468    
469 void LyXScreen::ToggleToggle(LyXText * text, BufferView * bv,
470                              int y_offset, int x_offset)
471 {
472         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
473             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
474                 return;
475         
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();
481         
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()));
486
487         DrawFromTo(text, bv, top - text->first, bottom - text->first, y_offset,
488                    x_offset);
489         expose(0, top - text->first, owner.workWidth(),
490                bottom - text->first - (top - text->first));
491 }