]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qscreen.C
add some extra smarts about when to paint immediately.
[lyx.git] / src / frontends / qt2 / qscreen.C
1 /**
2  * \file qscreen.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 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <algorithm>
16 #include <iostream> 
17
18 #include "LColor.h" 
19 #include "QWorkArea.h"
20 #include "qscreen.h"
21 #include "lyxtext.h"
22 #include "Painter.h"
23 #include "BufferView.h"
24 #include "insets/insettext.h"
25 #include "debug.h"
26
27 #include <qapplication.h>
28  
29 using std::endl;
30 using std::max;
31 using std::min;
32
33 namespace {
34
35 /// copy some horizontal regions about inside a pixmap
36 void copyInPixmap(QPixmap * p, int dest_y, int src_y, int src_w, int src_h)
37 {
38         bitBlt(p,
39                 0, dest_y,   // dest x,y
40                 p, 0, src_y, // source x, y
41                 src_w, src_h // source w, h
42                 );
43 }
44  
45 } // namespace anon
46  
47
48 QScreen::QScreen(QWorkArea & o)
49         : LyXScreen(), owner_(o)
50 {
51 }
52
53
54 QScreen::~QScreen()
55 {
56 }
57
58  
59 void QScreen::showManualCursor(LyXText const * text, int x, int y,
60                                  int asc, int desc, Cursor_Shape shape)
61 {
62         if (!qApp->focusWidget())
63                 return;
64  
65         string const focusname(qApp->focusWidget()->name());
66  
67         // Probably a hack
68         if (focusname != "content_pane")
69                 return;
70
71         int const y1 = max(y - text->first_y - asc, 0);
72         int const y_tmp = min(y - text->first_y + desc, owner_.height());
73
74         // secure against very strange situations
75         // which would be when .... ? 
76         int const y2 = max(y_tmp, y1);
77         
78         if (y2 > 0 && y1 < owner_.height()) {
79                 cursor_h_ = y2 - y1 + 1;
80                 cursor_y_ = y1;
81
82                 switch (shape) {
83                 case BAR_SHAPE:
84                         cursor_w_ = 1;
85                         cursor_x_ = x;
86                         break;
87                 case L_SHAPE:
88                         cursor_w_ = cursor_h_ / 3;
89                         cursor_x_ = x;
90                         break;
91                 case REVERSED_L_SHAPE:
92                         cursor_w_ = cursor_h_ / 3;
93                         cursor_x_ = x - cursor_w_ + 1;
94                         break;
95                 }
96
97                 if (!nocursor_pixmap_.get() 
98                         || cursor_w_ != nocursor_pixmap_->width()
99                         || cursor_h_ != nocursor_pixmap_->height()) {
100                         nocursor_pixmap_.reset(new QPixmap(cursor_w_, cursor_h_));
101                 }
102  
103                 owner_.getPainter().start();
104
105                 // save old area 
106                 bitBlt(nocursor_pixmap_.get(), 0, 0, owner_.getPixmap(),
107                         cursor_x_, cursor_y_, cursor_w_, cursor_h_);
108  
109                 owner_.getPainter().line(x, y1, x, y2);
110                 switch (shape) {
111                 case BAR_SHAPE:
112                         break;
113                 case L_SHAPE:
114                 case REVERSED_L_SHAPE:
115                         int const rectangle_h = (cursor_h_ + 10) / 20;
116                         owner_.getPainter().fillRectangle(
117                                 cursor_x_, y2 - rectangle_h + 1,
118                                 cursor_w_ - 1, rectangle_h, LColor::cursor);
119                         break;
120                 }
121  
122                 owner_.getPainter().end();
123  
124                 owner_.getContent()->repaint(
125                         cursor_x_, cursor_y_,
126                         cursor_w_, cursor_h_); 
127
128         }
129         cursor_visible_ = true;
130 }
131
132
133 void QScreen::hideCursor()
134 {
135         if (!cursor_visible_) 
136                 return;
137
138         bitBlt(owner_.getPixmap(), cursor_x_, cursor_y_, 
139                 nocursor_pixmap_.get(), 0, 0, cursor_w_, cursor_h_);
140  
141         owner_.getContent()->repaint(
142                 cursor_x_, cursor_y_,
143                 cursor_w_, cursor_h_); 
144  
145         cursor_visible_ = false;
146 }
147
148  
149 void QScreen::repaint()
150 {
151         QWidget * content(owner_.getContent());
152         content->repaint(0, 0, content->width(), content->height());
153 }
154
155  
156 void QScreen::expose(int x, int y, int w, int h)
157 {
158         lyxerr[Debug::GUI] << "expose " << w << "x" << h
159                 << "+" << x << "+" << y << endl;
160  
161         owner_.getContent()->update(x, y, w, h);
162 }
163
164
165 void QScreen::draw(LyXText * text, BufferView * bv, unsigned int y)
166 {
167         QPixmap * p(owner_.getPixmap());
168  
169         owner_.getPainter().start();
170  
171         if (cursor_visible_) hideCursor();
172
173         int const old_first = text->first_y;
174         bool const internal = (text == bv->text);
175         text->first_y = y;
176
177         // If you want to fix the warning below, fix it so it
178         // actually scrolls properly. Hint: a cast won't do.
179  
180         // is any optimization possible?
181         if (y - old_first < owner_.workHeight()
182             && old_first - y < owner_.workHeight()) {
183                 if (text->first_y < old_first) {
184                         int const dest_y = old_first - text->first_y;
185                         drawFromTo(text, bv, 0, dest_y, 0, 0, internal);
186                         copyInPixmap(p, dest_y, 0, owner_.workWidth(), owner_.height() - dest_y);
187                         expose(0, 0, owner_.workWidth(), dest_y);
188                 } else  {
189                         int const src_y = text->first_y - old_first;
190                         drawFromTo(text, bv, owner_.height() - src_y, owner_.height(), 0, 0, internal);
191                         copyInPixmap(p, 0, 0, owner_.workWidth(), owner_.height() - src_y);
192                         expose(0, owner_.height() - src_y, owner_.workWidth(), src_y);
193                 }
194         } else {
195                 lyxerr[Debug::GUI] << "dumb full redraw" << endl; 
196                 drawFromTo(text, bv, 0, owner_.height(), 0, 0, internal);
197                 repaint();
198         }
199  
200         owner_.getPainter().end();
201 }