]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qscreen.C
185007700b60f62759738ddee439aff8b3652aef
[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::showManualCursor(LyXText const * text, int x, int y,
56                                  int asc, int desc, Cursor_Shape shape)
57 {
58         if (!qApp->focusWidget())
59                 return;
60
61         int const y1 = max(y - text->top_y() - asc, 0);
62         int const y_tmp = min(y - text->top_y() + desc, owner_.height());
63
64         // secure against very strange situations
65         // which would be when .... ?
66         int const y2 = max(y_tmp, y1);
67
68         if (y2 > 0 && y1 < owner_.height()) {
69                 cursor_h_ = y2 - y1 + 1;
70                 cursor_y_ = y1;
71
72                 switch (shape) {
73                 case BAR_SHAPE:
74                         cursor_w_ = 1;
75                         cursor_x_ = x;
76                         break;
77                 case L_SHAPE:
78                         cursor_w_ = cursor_h_ / 3;
79                         cursor_x_ = x;
80                         break;
81                 case REVERSED_L_SHAPE:
82                         cursor_w_ = cursor_h_ / 3;
83                         cursor_x_ = x - cursor_w_ + 1;
84                         break;
85                 }
86
87                 if (!nocursor_pixmap_.get()
88                         || cursor_w_ != nocursor_pixmap_->width()
89                         || cursor_h_ != nocursor_pixmap_->height()) {
90                         nocursor_pixmap_.reset(new QPixmap(cursor_w_, cursor_h_));
91                 }
92
93                 owner_.getPainter().start();
94
95                 // save old area
96                 bitBlt(nocursor_pixmap_.get(), 0, 0, owner_.getPixmap(),
97                         cursor_x_, cursor_y_, cursor_w_, cursor_h_);
98
99                 owner_.getPainter().line(x, y1, x, y2, LColor::cursor);
100                 switch (shape) {
101                 case BAR_SHAPE:
102                         break;
103                 case L_SHAPE:
104                 case REVERSED_L_SHAPE:
105                         int const rectangle_h = (cursor_h_ + 10) / 20;
106                         owner_.getPainter().fillRectangle(
107                                 cursor_x_, y2 - rectangle_h + 1,
108                                 cursor_w_ - 1, rectangle_h, LColor::cursor);
109                         break;
110                 }
111
112                 owner_.getPainter().end();
113
114                 owner_.getContent()->repaint(
115                         cursor_x_, cursor_y_,
116                         cursor_w_, cursor_h_);
117
118         }
119         cursor_visible_ = true;
120 }
121
122
123 void QScreen::hideCursor()
124 {
125         if (!cursor_visible_)
126                 return;
127
128         bitBlt(owner_.getPixmap(), cursor_x_, cursor_y_,
129                 nocursor_pixmap_.get(), 0, 0, cursor_w_, cursor_h_);
130
131         owner_.getContent()->repaint(
132                 cursor_x_, cursor_y_,
133                 cursor_w_, cursor_h_);
134
135         cursor_visible_ = false;
136 }
137
138
139 void QScreen::repaint()
140 {
141         QWidget * content(owner_.getContent());
142         content->repaint(0, 0, content->width(), content->height());
143 }
144
145
146 void QScreen::expose(int x, int y, int w, int h)
147 {
148         lyxerr[Debug::GUI] << "expose " << w << 'x' << h
149                 << '+' << x << '+' << y << endl;
150
151         owner_.getContent()->update(x, y, w, h);
152 }
153
154
155 void QScreen::draw(LyXText * text, BufferView * bv, unsigned int y)
156 {
157         QPixmap * p(owner_.getPixmap());
158
159         owner_.getPainter().start();
160
161         if (cursor_visible_) hideCursor();
162
163         int const old_first = text->top_y();
164         text->top_y(y);
165
166         // If you want to fix the warning below, fix it so it
167         // actually scrolls properly. Hint: a cast won't do.
168
169         // is any optimization possible?
170         if (y - old_first < owner_.workHeight()
171             && old_first - y < owner_.workHeight()) {
172                 if (text->top_y() < old_first) {
173                         int const dest_y = old_first - text->top_y();
174                         drawFromTo(text, bv, 0, dest_y, 0, 0);
175                         copyInPixmap(p, dest_y, 0, owner_.workWidth(), owner_.height() - dest_y);
176                         expose(0, 0, owner_.workWidth(), dest_y);
177                 } else  {
178                         int const src_y = text->top_y() - old_first;
179                         drawFromTo(text, bv, owner_.height() - src_y, owner_.height(), 0, 0);
180                         copyInPixmap(p, 0, 0, owner_.workWidth(), owner_.height() - src_y);
181                         expose(0, owner_.height() - src_y, owner_.workWidth(), src_y);
182                 }
183         } else {
184                 lyxerr[Debug::GUI] << "dumb full redraw" << endl;
185                 drawFromTo(text, bv, 0, owner_.height(), 0, 0);
186                 repaint();
187         }
188
189         owner_.getPainter().end();
190 }