]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.cpp
Move Color::color enum to ColorCode.h
[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 #include "frontends/WorkAreaManager.h"
22
23 #include "BufferView.h"
24 #include "Buffer.h"
25 #include "BufferParams.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/noncopyable.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         buffer.workAreaManager().add(this);
72         // Setup the signals
73         timecon = cursor_timeout_.timeout
74                 .connect(boost::bind(&WorkArea::toggleCursor, this));
75
76         cursor_timeout_.start();
77 }
78
79
80 WorkArea::~WorkArea()
81 {
82         buffer_view_->buffer().workAreaManager().remove(this);
83         delete buffer_view_;
84 }
85
86
87 void WorkArea::close()
88 {
89         lyx_view_->removeWorkArea(this);
90 }
91
92 //void WorkArea::setLyXView(LyXView * lyx_view)
93 //{
94 //      lyx_view_ = lyx_view;
95 //}
96
97
98 BufferView & WorkArea::bufferView()
99 {
100         return *buffer_view_;
101 }
102
103
104 BufferView const & WorkArea::bufferView() const
105 {
106         return *buffer_view_;
107 }
108
109
110 void WorkArea::stopBlinkingCursor()
111 {
112         cursor_timeout_.stop();
113         hideCursor();
114 }
115
116
117 void WorkArea::startBlinkingCursor()
118 {
119         showCursor();
120         cursor_timeout_.restart();
121 }
122
123
124 void WorkArea::redraw()
125 {
126         if (!isVisible())
127                 // No need to redraw in this case.
128                 return;
129
130         // No need to do anything if this is the current view. The BufferView
131         // metrics are already up to date.
132         if (lyx_view_ != theApp()->currentView()) {
133                 // FIXME: it would be nice to optimize for the off-screen case.
134                 buffer_view_->updateMetrics(false);
135                 buffer_view_->cursor().fixIfBroken();
136         }
137
138         updateScrollbar();
139
140         // update cursor position, because otherwise it has to wait until
141         // the blinking interval is over
142         if (cursor_visible_) {
143                 hideCursor();
144                 showCursor();
145         }
146         
147         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
148
149         LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
150
151         int const ymin = std::max(vi.y1, 0);
152         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
153
154         expose(0, ymin, width(), ymax - ymin);
155
156         //LYXERR(Debug::WORKAREA)
157         //<< "  ymin = " << ymin << "  width() = " << width()
158 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
159
160         if (lyxerr.debugging(Debug::WORKAREA))
161                 buffer_view_->coordCache().dump();
162 }
163
164
165 void WorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
166 {
167         // In order to avoid bad surprise in the middle of an operation, we better stop
168         // the blinking cursor.
169         stopBlinkingCursor();
170
171         theLyXFunc().setLyXView(lyx_view_);
172         theLyXFunc().processKeySym(key, mod);
173 }
174
175
176 void WorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
177 {
178         // Handle drag&drop
179         if (cmd0.action == LFUN_FILE_OPEN) {
180                 lyx_view_->dispatch(cmd0);
181                 return;
182         }
183
184         theLyXFunc().setLyXView(lyx_view_);
185
186         FuncRequest cmd;
187
188         if (cmd0.action == LFUN_MOUSE_PRESS) {
189                 if (mod == ShiftModifier)
190                         cmd = FuncRequest(cmd0, "region-select");
191                 else if (mod == ControlModifier)
192                         cmd = FuncRequest(cmd0, "paragraph-select");
193                 else
194                         cmd = cmd0;
195         }
196         else
197                 cmd = cmd0;
198
199         // In order to avoid bad surprise in the middle of an operation, we better stop
200         // the blinking cursor.
201         if (!(cmd.action == LFUN_MOUSE_MOTION
202                 && cmd.button() == mouse_button::none))
203                 stopBlinkingCursor();
204
205         buffer_view_->mouseEventDispatch(cmd);
206
207         // Skip these when selecting
208         if (cmd.action != LFUN_MOUSE_MOTION) {
209                 lyx_view_->updateLayoutChoice();
210                 lyx_view_->updateToolbars();
211         }
212
213         // GUI tweaks except with mouse motion with no button pressed.
214         if (!(cmd.action == LFUN_MOUSE_MOTION
215                 && cmd.button() == mouse_button::none)) {
216                 // Slight hack: this is only called currently when we
217                 // clicked somewhere, so we force through the display
218                 // of the new status here.
219                 lyx_view_->clearMessage();
220
221                 // Show the cursor immediately after any operation.
222                 startBlinkingCursor();
223         }
224 }
225
226
227 void WorkArea::resizeBufferView()
228 {
229         // WARNING: Please don't put any code that will trigger a repaint here!
230         // We are already inside a paint event.
231         lyx_view_->setBusy(true);
232         buffer_view_->resize(width(), height());
233         lyx_view_->updateLayoutChoice();
234         lyx_view_->setBusy(false);
235 }
236
237
238 void WorkArea::updateScrollbar()
239 {
240         buffer_view_->updateScrollbar();
241         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
242         setScrollbarParams(scroll_.height, scroll_.position,
243                 scroll_.lineScrollHeight);
244 }
245
246
247 void WorkArea::showCursor()
248 {
249         if (cursor_visible_)
250                 return;
251
252         CursorShape shape = BAR_SHAPE;
253
254         Font const & realfont = buffer_view_->cursor().real_current_font;
255         BufferParams const & bp = buffer_view_->buffer().params();
256         bool const samelang = realfont.language() == bp.language;
257         bool const isrtl = realfont.isVisibleRightToLeft();
258
259         if (!samelang || isrtl != bp.language->rightToLeft()) {
260                 shape = L_SHAPE;
261                 if (isrtl)
262                         shape = REVERSED_L_SHAPE;
263         }
264
265         // The ERT language hack needs fixing up
266         if (realfont.language() == latex_language)
267                 shape = BAR_SHAPE;
268
269         Font const font = buffer_view_->cursor().getFont();
270         FontMetrics const & fm = theFontMetrics(font);
271         int const asc = fm.maxAscent();
272         int const des = fm.maxDescent();
273         int h = asc + des;
274         int x = 0;
275         int y = 0;
276         buffer_view_->cursor().getPos(x, y);
277         y -= asc;
278
279         // if it doesn't touch the screen, don't try to show it
280         if (y + h < 0 || y >= height())
281                 return;
282
283         cursor_visible_ = true;
284         showCursor(x, y, h, shape);
285 }
286
287
288 void WorkArea::hideCursor()
289 {
290         if (!cursor_visible_)
291                 return;
292
293         cursor_visible_ = false;
294         removeCursor();
295 }
296
297
298 void WorkArea::toggleCursor()
299 {
300         if (cursor_visible_)
301                 hideCursor();
302         else
303                 showCursor();
304
305         // Use this opportunity to deal with any child processes that
306         // have finished but are waiting to communicate this fact
307         // to the rest of LyX.
308         ForkedcallsController & fcc = ForkedcallsController::get();
309         fcc.handleCompletedProcesses();
310
311         cursor_timeout_.restart();
312 }
313
314 } // namespace frontend
315 } // namespace lyx