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