]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qscreen.C
55c873413dbdc2d64453049c4b619225305e0a54
[lyx.git] / src / frontends / qt2 / qscreen.C
1 /**
2  * \file qscreen.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include <algorithm>
15 #include <iostream>
16
17 #include "LColor.h"
18 #include "QWorkArea.h"
19 #include "qscreen.h"
20 #include "lyxtext.h"
21 #include "Painter.h"
22 #include "BufferView.h"
23 #include "insets/insettext.h"
24 #include "debug.h"
25
26 #include <qapplication.h>
27
28 using std::endl;
29 using std::max;
30 using std::min;
31
32 namespace {
33
34 /// copy some horizontal regions about inside a pixmap
35 void copyInPixmap(QPixmap * p, int dest_y, int src_y, int src_w, int src_h)
36 {
37         // bitBlt(dest, dest_x, dest_y, source, src_x, src_y, src_w, src_h)
38         bitBlt(p, 0, dest_y, p, 0, src_y, src_w, src_h);
39 }
40
41 } // namespace anon
42
43
44 QScreen::QScreen(QWorkArea & o)
45         : LyXScreen(), owner_(o)
46 {
47 }
48
49
50 QScreen::~QScreen()
51 {
52 }
53
54
55 void QScreen::repaint()
56 {
57         QWidget * content(owner_.getContent());
58         content->repaint(0, 0, content->width(), content->height());
59 }
60
61
62 void QScreen::expose(int x, int y, int w, int h)
63 {
64         lyxerr[Debug::GUI] << "expose " << w << 'x' << h
65                 << '+' << x << '+' << y << endl;
66
67         owner_.getContent()->update(x, y, w, h);
68 }
69
70
71 void QScreen::draw(LyXText * text, BufferView * bv, unsigned int y)
72 {
73         QPixmap * p(owner_.getPixmap());
74
75         owner_.getPainter().start();
76
77         int const old_first = text->top_y();
78         text->top_y(y);
79
80         // If you want to fix the warning below, fix it so it
81         // actually scrolls properly. Hint: a cast won't do.
82
83         // is any optimization possible?
84         if (y - old_first < owner_.workHeight()
85             && old_first - y < owner_.workHeight()) {
86                 if (text->top_y() < old_first) {
87                         int const dest_y = old_first - text->top_y();
88                         drawFromTo(text, bv, 0, dest_y, 0, 0);
89                         copyInPixmap(p, dest_y, 0, owner_.workWidth(), owner_.height() - dest_y);
90                         expose(0, 0, owner_.workWidth(), dest_y);
91                 } else  {
92                         int const src_y = text->top_y() - old_first;
93                         drawFromTo(text, bv, owner_.height() - src_y, owner_.height(), 0, 0);
94                         copyInPixmap(p, 0, 0, owner_.workWidth(), owner_.height() - src_y);
95                         expose(0, owner_.height() - src_y, owner_.workWidth(), src_y);
96                 }
97         } else {
98                 lyxerr[Debug::GUI] << "dumb full redraw" << endl;
99                 drawFromTo(text, bv, 0, owner_.height(), 0, 0);
100                 repaint();
101         }
102
103         owner_.getPainter().end();
104 }
105
106
107 void QScreen::showCursor(int x, int y, int h, Cursor_Shape shape)
108 {
109         cursor_x_ = x;
110         cursor_y_ = y;
111         cursor_h_ = h;
112
113         switch (shape) {
114                 case BAR_SHAPE:
115                         cursor_w_ = 1;
116                         break;
117                 case L_SHAPE:
118                         cursor_w_ = cursor_h_ / 3;
119                         break;
120                 case REVERSED_L_SHAPE:
121                         cursor_w_ = cursor_h_ / 3;
122                         cursor_x_ = x - cursor_w_ + 1;
123                         break;
124         }
125
126         if (!nocursor_pixmap_.get()
127                 || cursor_w_ != nocursor_pixmap_->width()
128                 || cursor_h_ != nocursor_pixmap_->height()) {
129                 nocursor_pixmap_.reset(new QPixmap(cursor_w_, cursor_h_));
130         }
131
132         // save old area
133         bitBlt(nocursor_pixmap_.get(), 0, 0, owner_.getPixmap(),
134                 cursor_x_, cursor_y_, cursor_w_, cursor_h_);
135
136         if (!qApp->focusWidget())
137                 return;
138
139         Painter & pain(owner_.getPainter());
140         pain.start();
141         pain.line(x, y, x, y + h - 1, LColor::cursor);
142
143         switch (shape) {
144                 case BAR_SHAPE:
145                         break;
146                 case REVERSED_L_SHAPE:
147                 case L_SHAPE:
148                         pain.line(cursor_x_, y + h - 1, cursor_x_ + cursor_w_ - 1,
149                                 y + h - 1, LColor::cursor);
150                         break;
151         }
152
153         pain.end();
154
155         owner_.getContent()->repaint(
156                 cursor_x_, cursor_y_,
157                 cursor_w_, cursor_h_);
158 }
159
160
161 void QScreen::removeCursor()
162 {
163         // before first showCursor
164         if (!nocursor_pixmap_.get())
165                 return;
166
167         bitBlt(owner_.getPixmap(), cursor_x_, cursor_y_,
168                 nocursor_pixmap_.get(), 0, 0, cursor_w_, cursor_h_);
169
170         owner_.getContent()->repaint(
171                 cursor_x_, cursor_y_,
172                 cursor_w_, cursor_h_);
173 }