]> git.lyx.org Git - features.git/blob - src/screen.C
fix parse error in FormPreferences, make LyX be able to compile with gcc 2.97, use...
[features.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_get_display(), 
36                                     DefaultScreen(fl_get_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_get_display(), RootWindow(fl_get_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), force_clear(true) //, 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_get_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,
90                            int y1, int y2, int y_offset, int x_offset)
91 {
92         int y_text = text->first + y1;
93    
94         // get the first needed row 
95         Row * row = text->GetRowNearY(y_text);
96         // y_text is now the real beginning of the row
97    
98         int y = y_text - text->first;
99         // y1 is now the real beginning of row on the screen
100         
101         while (row != 0 && y < y2) {
102                 LyXText::text_status st = text->status;
103                 do {
104                         text->status = st;
105                         text->GetVisibleRow(owner.owner(), y+y_offset,
106                                             x_offset, row, y + text->first);
107                 } while (text->status == LyXText::CHANGED_IN_DRAW);
108                 text->status = st;
109                 y += row->height();
110                 row = row->next();
111         }
112         force_clear = false;
113
114         // maybe we have to clear the screen at the bottom
115         if ((y < y2) && text->bv_owner) {
116                 owner.getPainter().fillRectangle(0, y,
117                                                  owner.workWidth(),
118                                                  y2 - y,
119                                                LColor::bottomarea);
120         }
121 }
122
123
124 void LyXScreen::DrawOneRow(LyXText * text, Row * row, int y_text,
125                            int y_offset, int x_offset)
126 {
127         int y = y_text - text->first + y_offset;
128
129         if (((y + row->height()) > 0) &&
130             ((y - row->height()) <= static_cast<int>(owner.height()))) {
131                 // ok there is something visible
132                 LyXText::text_status st = text->status;
133                 do {
134                         text->status = st;
135                         text->GetVisibleRow(owner.owner(), y, x_offset, row,
136                                             y + text->first);
137                 } while (text->status == LyXText::CHANGED_IN_DRAW);
138                 text->status = st;
139         }
140         force_clear = false;
141 }
142
143
144 /* draws the screen, starting with textposition y. uses as much already
145 * printed pixels as possible */
146 void LyXScreen::Draw(LyXText * text, unsigned int y)
147 {
148         if (cursor_visible) HideCursor();
149
150         int old_first = text->first;
151         text->first = y;
152
153         // is any optimiziation possible?
154         if ((y - old_first) < owner.height()
155             && (old_first - y) < owner.height()) {
156                 if (text->first < old_first) {
157                         DrawFromTo(text, 0, old_first - text->first);
158                         XCopyArea (fl_get_display(),
159                                    owner.getWin(),
160                                    owner.getWin(),
161                                    gc_copy,
162                                    owner.xpos(),
163                                    owner.ypos(),
164                                    owner.workWidth(),
165                                    owner.height() - old_first + text->first,
166                                    owner.xpos(),
167                                    owner.ypos() + old_first - text->first
168                                 );
169                         // expose the area drawn
170                         expose(0, 0,
171                                owner.workWidth(),
172                                old_first - text->first);
173                 } else  {
174                         DrawFromTo(text,
175                                    owner.height() + old_first - text->first,
176                                    owner.height());
177                         XCopyArea (fl_get_display(),
178                                    owner.getWin(),
179                                    owner.getWin(),
180                                    gc_copy,
181                                    owner.xpos(),
182                                    owner.ypos() + text->first - old_first,
183                                    owner.workWidth(),
184                                    owner.height() + old_first - text->first,
185                                    owner.xpos(),
186                                    owner.ypos());
187                         // expose the area drawn
188                         expose(0, owner.height() + old_first - text->first,
189                                owner.workWidth(), text->first - old_first);
190                 }
191         } else {
192                 // make a dumb new-draw 
193                 DrawFromTo(text, 0, owner.height());
194                 expose(0, 0, owner.workWidth(), owner.height());
195         }
196 }
197
198
199 void LyXScreen::ShowCursor(LyXText const * text)
200 {
201         if (!cursor_visible) {
202                 Cursor_Shape shape = BAR_SHAPE;
203                 if (text->real_current_font.language() !=
204                     owner.owner()->buffer()->params.language
205                     || text->real_current_font.isVisibleRightToLeft()
206                     != owner.owner()->buffer()->params.language->RightToLeft())
207                         shape = (text->real_current_font.isVisibleRightToLeft())
208                                 ? REVERSED_L_SHAPE : L_SHAPE;
209                 ShowManualCursor(text, text->cursor.x(), text->cursor.y(),
210                                  lyxfont::maxAscent(text->real_current_font),
211                                  lyxfont::maxDescent(text->real_current_font),
212                                  shape);
213         }
214 }
215
216
217 /* returns true if first has changed, otherwise false */ 
218 bool LyXScreen::FitManualCursor(LyXText * text,
219                                 int /*x*/, int y, int asc, int desc)
220 {
221         int newtop = text->first;
222   
223         if (y + desc - text->first >= static_cast<int>(owner.height()))
224                 newtop = y - 3 * owner.height() / 4;  // the scroll region must be so big!!
225         else if (y - asc < static_cast<int>(text->first)
226                 && text->first > 0) {
227                 newtop = y - owner.height() / 4;
228         }
229
230         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
231   
232         if (newtop != static_cast<int>(text->first)) {
233                 Draw(text, newtop);
234                 text->first = newtop;
235                 return true;
236         }
237         return false;
238 }
239
240
241 void LyXScreen::ShowManualCursor(LyXText const * text, int x, int y,
242                                  int asc, int desc, Cursor_Shape shape)
243 {
244         int y1 = max(y - text->first - asc, 0);
245         int y2 = min(y - text->first + desc, static_cast<int>(owner.height()));
246
247         // Secure against very strange situations
248         y2 = max(y2, y1);
249         
250         if (cursor_pixmap){
251                 XFreePixmap(fl_get_display(), cursor_pixmap);
252                 cursor_pixmap = 0;
253         }
254
255         if (y2 > 0 && y1 < int(owner.height())) {
256                 cursor_pixmap_h = y2 - y1 + 1;
257                 cursor_pixmap_y = y1;
258
259                 switch (shape) {
260                 case BAR_SHAPE:
261                         cursor_pixmap_w = 1;
262                         cursor_pixmap_x = x;
263                         break;
264                 case L_SHAPE:
265                         cursor_pixmap_w = cursor_pixmap_h/3;
266                         cursor_pixmap_x = x;
267                         break;
268                 case REVERSED_L_SHAPE:
269                         cursor_pixmap_w = cursor_pixmap_h/3;
270                         cursor_pixmap_x = x - cursor_pixmap_w + 1;
271                         break;
272                 }
273
274                 cursor_pixmap = 
275                         XCreatePixmap (fl_get_display(),
276                                        fl_root,
277                                        cursor_pixmap_w,
278                                        cursor_pixmap_h,
279                                        fl_get_visual_depth());
280                 XCopyArea (fl_get_display(),
281                            owner.getWin(),
282                            cursor_pixmap,
283                            gc_copy,
284                            owner.xpos() + cursor_pixmap_x,
285                            owner.ypos() + cursor_pixmap_y,
286                            cursor_pixmap_w,
287                            cursor_pixmap_h,
288                            0, 0);
289                 XDrawLine(fl_get_display(),
290                           owner.getWin(),
291                           gc_copy,
292                           x + owner.xpos(),
293                           y1 + owner.ypos(),
294                           x + owner.xpos(),
295                           y2 + owner.ypos());
296                 switch (shape) {
297                 case BAR_SHAPE:
298                         break;
299                 case L_SHAPE:
300                 case REVERSED_L_SHAPE:
301                         int rectangle_h = (cursor_pixmap_h+10)/20;
302                         XFillRectangle(fl_get_display(),
303                                        owner.getWin(),
304                                        gc_copy,
305                                        cursor_pixmap_x + owner.xpos(),
306                                        y2 - rectangle_h + 1 + owner.ypos(),
307                                        cursor_pixmap_w - 1, rectangle_h);
308                         break;
309                 }
310
311         }
312         cursor_visible = true;
313 }
314
315
316 void LyXScreen::HideCursor()
317 {
318         if (!cursor_visible) return;
319
320         if (cursor_pixmap){
321                 XCopyArea (fl_get_display(), 
322                            cursor_pixmap,
323                            owner.getWin(),
324                            gc_copy,
325                            0, 0, 
326                            cursor_pixmap_w, cursor_pixmap_h,
327                            cursor_pixmap_x + owner.xpos(),
328                            cursor_pixmap_y + owner.ypos());
329         }
330         cursor_visible = false;
331 }
332
333
334 void LyXScreen::CursorToggle(LyXText const * text)
335 {
336         if (cursor_visible)
337                 HideCursor();
338         else
339                 ShowCursor(text);
340 }
341
342
343 /* returns a new top so that the cursor is visible */ 
344 unsigned int LyXScreen::TopCursorVisible(LyXText const * text)
345 {
346         int newtop = text->first;
347
348         if (text->cursor.y()
349             - text->cursor.row()->baseline()
350             + text->cursor.row()->height()
351             - text->first >= owner.height()) {
352                 if (text->cursor.row()->height() < owner.height()
353                     && text->cursor.row()->height() > owner.height() / 4)
354                         newtop = text->cursor.y()
355                                 + text->cursor.row()->height()
356                                 - text->cursor.row()->baseline() - owner.height();
357                 else
358                         newtop = text->cursor.y()
359                                 - 3 * owner.height() / 4;   /* the scroll region must be so big!! */
360         } else if (static_cast<int>((text->cursor.y()) - text->cursor.row()->baseline()) <
361                    text->first && text->first > 0)
362         {
363                 if (text->cursor.row()->height() < owner.height()
364                     && text->cursor.row()->height() > owner.height() / 4)
365                         newtop = text->cursor.y() - text->cursor.row()->baseline();
366                 else {
367                         newtop = text->cursor.y() - owner.height() / 4;
368                         newtop = min(newtop, int(text->first));
369                 }
370         }
371
372         newtop = max(newtop, 0);
373
374         return newtop;
375 }
376
377
378 /* scrolls the screen so that the cursor is visible, if necessary.
379 * returns true if a change was made, otherwise false */ 
380 bool LyXScreen::FitCursor(LyXText * text)
381 {
382         // Is a change necessary?
383         int newtop = TopCursorVisible(text);
384         bool result = (newtop != text->first);
385         if (result)
386                 Draw(text, newtop);
387         return result;
388 }
389
390    
391 void LyXScreen::Update(LyXText * text, int y_offset, int x_offset)
392 {
393         switch (text->status) {
394         case LyXText::NEED_MORE_REFRESH:
395         {
396                 int y = max(int(text->refresh_y - text->first), 0);
397                 DrawFromTo(text, y, owner.height(), y_offset, x_offset);
398                 text->refresh_y = 0;
399                 text->status = LyXText::UNCHANGED;
400                 expose(0, y, owner.workWidth(), owner.height() - y);
401         }
402         break;
403         case LyXText::NEED_VERY_LITTLE_REFRESH:
404         {
405                 // ok I will update the current cursor row
406                 DrawOneRow(text, text->refresh_row, text->refresh_y,
407                            y_offset, x_offset);
408                 text->status = LyXText::UNCHANGED;
409                 expose(0, text->refresh_y - text->first + y_offset,
410                        owner.workWidth(), text->refresh_row->height());
411         }
412         break;
413         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
414         case LyXText::UNCHANGED:
415                 // Nothing needs done
416                 break;
417         }
418 }
419
420
421 void LyXScreen::ToggleSelection(LyXText * text,  bool kill_selection,
422                                 int y_offset, int x_offset)
423 {
424         // only if there is a selection
425         if (!text->selection) return;
426
427         int bottom = min(max(static_cast<int>(text->sel_end_cursor.y()
428                               - text->sel_end_cursor.row()->baseline()
429                               + text->sel_end_cursor.row()->height()),
430                                               text->first),
431                           static_cast<int>(text->first + owner.height()));
432         int top = min(max(static_cast<int>(text->sel_start_cursor.y() -
433                           text->sel_start_cursor.row()->baseline()),
434                           text->first),
435                       static_cast<int>(text->first + owner.height()));
436
437         if (kill_selection)
438                 text->selection = 0;
439         DrawFromTo(text, top - text->first, bottom - text->first,
440                    y_offset, x_offset);
441         expose(0, top - text->first,
442                owner.workWidth(),
443                bottom - text->first - (top - text->first));
444 }
445   
446    
447 void LyXScreen::ToggleToggle(LyXText * text, int y_offset, int x_offset)
448 {
449         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
450             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
451                 return;
452         
453         int top = text->toggle_cursor.y()
454                 - text->toggle_cursor.row()->baseline();
455         int bottom = text->toggle_end_cursor.y()
456                 - text->toggle_end_cursor.row()->baseline() 
457                 + text->toggle_end_cursor.row()->height();
458         
459         bottom = min(max(bottom, text->first),
460                      static_cast<int>(text->first + owner.height()));
461         top = min(max(top, text->first),
462                   static_cast<int>(text->first + owner.height()));
463
464         DrawFromTo(text, top - text->first, bottom - text->first, y_offset,
465                    x_offset);
466         expose(0, top - text->first, owner.workWidth(),
467                bottom - text->first - (top - text->first));
468 }