]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
3322d112f5f1f75990f6e410e4624d6235d5d5a9
[lyx.git] / src / frontends / screen.C
1 /**
2  * \file screen.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #ifdef __GNUG__
10 #pragma implementation
11 #endif
12
13 #include <config.h>
14
15 #include "screen.h"
16 #include "lyxtext.h"
17 #include "lyxrow.h"
18 #include "BufferView.h"
19 #include "buffer.h"
20 #include "WorkArea.h"
21 #include "Painter.h"
22 #include "font_metrics.h"
23 #include "language.h"
24 #include "debug.h"
25
26 using std::min;
27 using std::max;
28 using std::endl;
29  
30 LyXScreen::LyXScreen()
31         : force_clear_(true), cursor_visible_(false)
32 {
33 }
34
35
36 LyXScreen::~LyXScreen()
37 {
38 }
39
40 // FIXME: GUII these cursor methods need to decide
41 // whether the workarea is focused or not
42
43 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
44 {
45         if (cursor_visible_)
46                 return;
47  
48         workarea().getPainter().start();
49  
50         Cursor_Shape shape = BAR_SHAPE;
51         BufferParams const & bp(bv->buffer()->params);
52         LyXFont const & realfont(text->real_current_font);
53
54         if (realfont.language() != bp.language
55                 || realfont.isVisibleRightToLeft() 
56                 != bp.language->RightToLeft()) {
57                 shape = (realfont.isVisibleRightToLeft())
58                         ? REVERSED_L_SHAPE : L_SHAPE;
59         }
60  
61         showManualCursor(text, text->cursor.x(), text->cursor.y(),
62                 font_metrics::maxAscent(realfont),
63                 font_metrics::maxDescent(realfont),
64                 shape);
65  
66         workarea().getPainter().end();
67 }
68
69
70 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
71         int /*x*/, int y, int asc, int desc)
72 {
73         int const vheight = workarea().workHeight();
74         int newtop = text->first_y;
75  
76         if (y + desc - text->first_y >= vheight)
77                 newtop = y - 3 * vheight / 4;  // the scroll region must be so big!!
78         else if (y - asc < text->first_y
79                 && text->first_y > 0) {
80                 newtop = y - vheight / 4;
81         }
82
83         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
84  
85         if (newtop != text->first_y) {
86                 draw(text, bv, newtop);
87                 text->first_y = newtop;
88                 return true;
89         }
90         return false;
91 }
92
93
94 void LyXScreen::cursorToggle(BufferView * bv) const
95 {
96         if (cursor_visible_)
97                 bv->hideCursor();
98         else
99                 bv->showCursor();
100 }
101
102
103 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
104 {
105         int const vheight = workarea().workHeight();
106         int newtop = top_y;
107
108         Row * row = cursor.row();
109
110         // Is this a hack? Yes, probably... (Lgb)
111         if (!row)
112                 return max(newtop, 0);
113         
114         if (cursor.y() - row->baseline() + row->height()
115             - top_y >= vheight) {
116                 if (row->height() < vheight
117                     && row->height() > vheight / 4) {
118                         newtop = cursor.y()
119                                 + row->height()
120                                 - row->baseline() - vheight;
121                 } else {
122                         // scroll down
123                         newtop = cursor.y()
124                                 - vheight / 2;   /* the scroll region must be so big!! */
125                 }
126                 
127         } else if (static_cast<int>((cursor.y()) - row->baseline()) <
128                    top_y && top_y > 0) {
129                 if (row->height() < vheight
130                     && row->height() > vheight / 4) {
131                         newtop = cursor.y() - row->baseline();
132                 } else {
133                         // scroll up
134                         newtop = cursor.y() - vheight / 2;
135                         newtop = min(newtop, top_y);
136                 }
137         }
138
139         newtop = max(newtop, 0);
140
141         return newtop;
142 }
143
144
145 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
146 {
147         // Is a change necessary?
148         int const newtop = topCursorVisible(text->cursor, text->first_y);
149         bool const result = (newtop != text->first_y);
150         if (result)
151                 draw(text, bv, newtop);
152         return result;
153 }
154
155   
156 void LyXScreen::update(LyXText * text, BufferView * bv,
157         int yo, int xo)
158 {
159         int const vwidth = workarea().workWidth();
160         int const vheight = workarea().workHeight();
161
162         workarea().getPainter().start();
163  
164         switch (text->status()) {
165         case LyXText::NEED_MORE_REFRESH:
166         {
167                 int const y = max(int(text->refresh_y - text->first_y), 0);
168                 drawFromTo(text, bv, y, vheight, yo, xo);
169                 text->refresh_y = 0;
170                 // otherwise this is called ONLY from BufferView_pimpl(update)
171                 // or we should see to set this flag accordingly
172                 if (text != bv->text)
173                         text->status(bv, LyXText::UNCHANGED);
174                 expose(0, y, vwidth, vheight - y);
175         }
176         break;
177         case LyXText::NEED_VERY_LITTLE_REFRESH:
178         {
179                 // ok I will update the current cursor row
180                 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
181                            yo, xo);
182                 // this because if we had a major update the refresh_row could
183                 // have been set to 0!
184                 if (text->refresh_row) {
185                         // otherwise this is called ONLY from BufferView_pimpl(update)
186                         // or we should see to set this flag accordingly
187                         if (text != bv->text)
188                                 text->status(bv, LyXText::UNCHANGED);
189                         expose(0, text->refresh_y - text->first_y + yo,
190                                    vwidth, text->refresh_row->height());
191                 }
192         }
193         break;
194         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
195         case LyXText::UNCHANGED:
196                 // Nothing needs done
197                 break;
198         }
199  
200         workarea().getPainter().end();
201 }
202
203
204 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
205                                 bool kill_selection,
206                                 int yo, int xo)
207 {
208         // only if there is a selection
209         if (!text->selection.set()) return;
210
211         int const bottom = min(
212                 max(static_cast<int>(text->selection.end.y()
213                                      - text->selection.end.row()->baseline()
214                                      + text->selection.end.row()->height()),
215                     text->first_y),
216                 static_cast<int>(text->first_y + workarea().workHeight()));
217         int const top = min(
218                 max(static_cast<int>(text->selection.start.y() -
219                                      text->selection.start.row()->baseline()),
220                     text->first_y),
221                 static_cast<int>(text->first_y + workarea().workHeight()));
222
223         if (kill_selection)
224                 text->selection.set(false);
225  
226         workarea().getPainter().start();
227  
228         drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
229                    yo, xo);
230         expose(0, top - text->first_y,
231                workarea().workWidth(),
232                bottom - text->first_y - (top - text->first_y));
233  
234         workarea().getPainter().end();
235 }
236  
237   
238 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
239                              int yo, int xo)
240 {
241         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
242             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
243                 return;
244         
245         int const top_tmp = text->toggle_cursor.y()
246                 - text->toggle_cursor.row()->baseline();
247         int const bottom_tmp = text->toggle_end_cursor.y()
248                 - text->toggle_end_cursor.row()->baseline()
249                 + text->toggle_end_cursor.row()->height();
250         
251         int const offset = yo < 0 ? yo : 0;
252         int const bottom = min(max(bottom_tmp, text->first_y),
253                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
254         int const top = min(max(top_tmp, text->first_y),
255                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
256
257         workarea().getPainter().start();
258
259         drawFromTo(text, bv, top - text->first_y,
260                    bottom - text->first_y, yo,
261                    xo);
262         expose(0, top - text->first_y, workarea().workWidth(),
263                bottom - text->first_y - (top - text->first_y));
264  
265         workarea().getPainter().end();
266 }
267
268
269 void LyXScreen::redraw(LyXText * text, BufferView * bv)
270 {
271         workarea().getPainter().start();
272
273         if (!text) {
274                 greyOut();
275                 expose(0, 0, workarea().workWidth(), workarea().workHeight());
276                 workarea().getPainter().end();
277                 return;
278         }
279
280         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
281         expose(0, 0, workarea().workWidth(), workarea().workHeight());
282  
283         workarea().getPainter().end();
284  
285         if (cursor_visible_) {
286                 cursor_visible_ = false;
287                 bv->showCursor();
288         }
289  
290 }
291
292
293 void LyXScreen::greyOut()
294 {
295         workarea().getPainter().fillRectangle(0, 0,
296                 workarea().workWidth(),
297                 workarea().workHeight(),
298                 LColor::bottomarea);
299 }
300
301
302 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
303         int y1, int y2, int yo, int xo,
304         bool internal)
305 {
306         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
307  
308         int y_text = text->first_y + y1;
309   
310         // get the first needed row
311         Row * row = text->getRowNearY(y_text);
312         // y_text is now the real beginning of the row
313   
314         int y = y_text - text->first_y;
315         // y1 is now the real beginning of row on the screen
316         
317         while (row != 0 && y < y2) {
318                 LyXText::text_status st = text->status();
319                 text->getVisibleRow(bv, y + yo,
320                                     xo, row, y + text->first_y);
321                 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
322                 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
323                         text->fullRebreak(bv);
324                         st = LyXText::NEED_MORE_REFRESH;
325                         text->setCursor(bv, text->cursor.par(), text->cursor.pos());
326                         text->status(bv, st);
327                         text->getVisibleRow(bv, y + yo,
328                                             xo, row, y + text->first_y);
329                 }
330                 y += row->height();
331                 row = row->next();
332         }
333         force_clear_ = false;
334
335         // maybe we have to clear the screen at the bottom
336         if ((y < y2) && text->bv_owner) {
337                 workarea().getPainter().fillRectangle(0, y,
338                         workarea().workWidth(), y2 - y,
339                         LColor::bottomarea);
340         }
341 }
342
343
344 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
345         int y_text, int yo, int xo)
346 {
347         int const y = y_text - text->first_y + yo;
348
349         if (((y + row->height()) > 0) &&
350             ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
351                 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
352         }
353         force_clear_ = false;
354 }