]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.cpp
Fulfill promise to Andre: TextClass_ptr --> TextClassPtr.
[lyx.git] / src / frontends / WorkArea.cpp
1 /**
2  * \file WorkArea.cpp
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  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Splash screen code added by Angus Leeming
12  */
13
14 #include <config.h>
15
16 #include "frontends/WorkArea.h"
17
18 #include "frontends/Application.h"
19 #include "frontends/FontMetrics.h"
20 #include "frontends/LyXView.h"
21
22 #include "BufferView.h"
23 #include "Buffer.h"
24 #include "BufferParams.h"
25 #include "Color.h"
26 #include "CoordCache.h"
27 #include "Cursor.h"
28 #include "debug.h"
29 #include "Font.h"
30 #include "FuncRequest.h"
31 #include "KeySymbol.h"
32 #include "Language.h"
33 #include "LyXFunc.h"
34 #include "LyXRC.h"
35 #include "MetricsInfo.h"
36
37 #include "gettext.h"
38 #include "support/ForkedcallsController.h"
39 #include "support/FileName.h"
40
41 #include <boost/utility.hpp>
42 #include <boost/bind.hpp>
43 #include <boost/current_function.hpp>
44
45 using lyx::support::ForkedcallsController;
46
47 using std::endl;
48 using std::min;
49 using std::max;
50 using std::string;
51
52
53 namespace {
54
55 // All the below connection objects are needed because of a bug in some
56 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
57 // to these connections we avoid a segfault upon startup, and also at exit.
58 // (Lgb)
59
60 boost::signals::connection timecon;
61
62 } // anon namespace
63
64 namespace lyx {
65 namespace frontend {
66
67 WorkArea::WorkArea(Buffer & buffer, LyXView & lv)
68         : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
69         cursor_visible_(false), cursor_timeout_(400)
70 {
71         // Setup the signals
72         timecon = cursor_timeout_.timeout
73                 .connect(boost::bind(&WorkArea::toggleCursor, this));
74
75         bufferChangedConnection_ =
76                 buffer.changed.connect(
77                         boost::bind(&WorkArea::redraw, this));
78
79         bufferClosingConnection_ =
80                 buffer.closing.connect(
81                 boost::bind(&WorkArea::close, this));
82
83         cursor_timeout_.start();
84 }
85
86
87 WorkArea::~WorkArea()
88 {
89         bufferChangedConnection_.disconnect();
90         bufferClosingConnection_.disconnect();
91
92         delete buffer_view_;
93 }
94
95
96 void WorkArea::close()
97 {
98         lyx_view_->removeWorkArea(this);
99 }
100
101 //void WorkArea::setLyXView(LyXView * lyx_view)
102 //{
103 //      lyx_view_ = lyx_view;
104 //}
105
106
107 BufferView & WorkArea::bufferView()
108 {
109         return *buffer_view_;
110 }
111
112
113 BufferView const & WorkArea::bufferView() const
114 {
115         return *buffer_view_;
116 }
117
118
119 void WorkArea::stopBlinkingCursor()
120 {
121         cursor_timeout_.stop();
122         hideCursor();
123 }
124
125
126 void WorkArea::startBlinkingCursor()
127 {
128         showCursor();
129         cursor_timeout_.restart();
130 }
131
132
133 void WorkArea::redraw()
134 {
135         if (!isVisible())
136                 // No need to redraw in this case.
137                 return;
138
139         // No need to do anything if this is the current view. The BufferView
140         // metrics are already up to date.
141         if (lyx_view_ != theApp()->currentView()) {
142                 // FIXME: it would be nice to optimize for the off-screen case.
143                 buffer_view_->updateMetrics(false);
144                 buffer_view_->cursor().fixIfBroken();
145         }
146
147         updateScrollbar();
148
149         // update cursor position, because otherwise it has to wait until
150         // the blinking interval is over
151         if (cursor_visible_) {
152                 hideCursor();
153                 showCursor();
154         }
155         
156         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
157
158         LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
159
160         int const ymin = std::max(vi.y1, 0);
161         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
162
163         expose(0, ymin, width(), ymax - ymin);
164
165         //LYXERR(Debug::WORKAREA)
166         //<< "  ymin = " << ymin << "  width() = " << width()
167 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
168
169         if (lyxerr.debugging(Debug::WORKAREA))
170                 buffer_view_->coordCache().dump();
171 }
172
173
174 void WorkArea::processKeySym(KeySymbol const & key, key_modifier::state state)
175 {
176         // In order to avoid bad surprise in the middle of an operation, we better stop
177         // the blinking cursor.
178         stopBlinkingCursor();
179
180         theLyXFunc().setLyXView(lyx_view_);
181         theLyXFunc().processKeySym(key, state);
182 }
183
184
185 void WorkArea::dispatch(FuncRequest const & cmd0, key_modifier::state k)
186 {
187         // Handle drag&drop
188         if (cmd0.action == LFUN_FILE_OPEN) {
189                 lyx_view_->dispatch(cmd0);
190                 return;
191         }
192
193         theLyXFunc().setLyXView(lyx_view_);
194
195         FuncRequest cmd;
196
197         if (cmd0.action == LFUN_MOUSE_PRESS) {
198                 if (k == key_modifier::shift)
199                         cmd = FuncRequest(cmd0, "region-select");
200                 else if (k == key_modifier::ctrl)
201                         cmd = FuncRequest(cmd0, "paragraph-select");
202                 else
203                         cmd = cmd0;
204         }
205         else
206                 cmd = cmd0;
207
208         // In order to avoid bad surprise in the middle of an operation, we better stop
209         // the blinking cursor.
210         if (!(cmd.action == LFUN_MOUSE_MOTION
211                 && cmd.button() == mouse_button::none))
212                 stopBlinkingCursor();
213
214         bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
215
216         if (needRedraw)
217                 buffer_view_->buffer().changed();
218
219         // Skip these when selecting
220         if (cmd.action != LFUN_MOUSE_MOTION) {
221                 lyx_view_->updateLayoutChoice();
222                 lyx_view_->updateToolbars();
223         }
224
225         // GUI tweaks except with mouse motion with no button pressed.
226         if (!(cmd.action == LFUN_MOUSE_MOTION
227                 && cmd.button() == mouse_button::none)) {
228                 // Slight hack: this is only called currently when we
229                 // clicked somewhere, so we force through the display
230                 // of the new status here.
231                 lyx_view_->clearMessage();
232
233                 // Show the cursor immediately after any operation.
234                 startBlinkingCursor();
235         }
236 }
237
238
239 void WorkArea::resizeBufferView()
240 {
241         // WARNING: Please don't put any code that will trigger a repaint here!
242         // We are already inside a paint event.
243         lyx_view_->busy(true);
244         buffer_view_->resize(width(), height());
245         lyx_view_->updateLayoutChoice();
246         lyx_view_->busy(false);
247 }
248
249
250 void WorkArea::updateScrollbar()
251 {
252         buffer_view_->updateScrollbar();
253         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
254         setScrollbarParams(scroll_.height, scroll_.position,
255                 scroll_.lineScrollHeight);
256 }
257
258
259 void WorkArea::showCursor()
260 {
261         if (cursor_visible_)
262                 return;
263
264         CursorShape shape = BAR_SHAPE;
265
266         Font const & realfont = buffer_view_->cursor().real_current_font;
267         BufferParams const & bp = buffer_view_->buffer().params();
268         bool const samelang = realfont.language() == bp.language;
269         bool const isrtl = realfont.isVisibleRightToLeft();
270
271         if (!samelang || isrtl != bp.language->rightToLeft()) {
272                 shape = L_SHAPE;
273                 if (isrtl)
274                         shape = REVERSED_L_SHAPE;
275         }
276
277         // The ERT language hack needs fixing up
278         if (realfont.language() == latex_language)
279                 shape = BAR_SHAPE;
280
281         Font const font = buffer_view_->cursor().getFont();
282         FontMetrics const & fm = theFontMetrics(font);
283         int const asc = fm.maxAscent();
284         int const des = fm.maxDescent();
285         int h = asc + des;
286         int x = 0;
287         int y = 0;
288         buffer_view_->cursor().getPos(x, y);
289         y -= asc;
290
291         // if it doesn't touch the screen, don't try to show it
292         if (y + h < 0 || y >= height())
293                 return;
294
295         cursor_visible_ = true;
296         showCursor(x, y, h, shape);
297 }
298
299
300 void WorkArea::hideCursor()
301 {
302         if (!cursor_visible_)
303                 return;
304
305         cursor_visible_ = false;
306         removeCursor();
307 }
308
309
310 void WorkArea::toggleCursor()
311 {
312         if (cursor_visible_)
313                 hideCursor();
314         else
315                 showCursor();
316
317         // Use this opportunity to deal with any child processes that
318         // have finished but are waiting to communicate this fact
319         // to the rest of LyX.
320         ForkedcallsController & fcc = ForkedcallsController::get();
321         fcc.handleCompletedProcesses();
322
323         cursor_timeout_.restart();
324 }
325
326 } // namespace frontend
327 } // namespace lyx