]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.C
new LFUN_WINDOW_CLOSE
[lyx.git] / src / frontends / WorkArea.C
1 /**
2  * \file WorkArea.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  * \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
21 #include "funcrequest.h"
22 #include "lyxfunc.h"
23 #include "Painter.h"
24
25 #include "BufferView.h"
26 #include "buffer.h"
27 #include "bufferparams.h"
28 #include "coordcache.h"
29 #include "cursor.h"
30 #include "debug.h"
31 #include "language.h"
32 #include "LColor.h"
33 #include "lyxfont.h"
34 #include "lyxrc.h"
35 #include "lyxrow.h"
36 #include "lyxtext.h"
37 #include "LyXView.h"
38 #include "metricsinfo.h"
39 #include "paragraph.h"
40 #include "rowpainter.h"
41
42 #include "gettext.h"
43 #include "support/filetools.h" // LibFileSearch
44 #include "support/forkedcontr.h"
45
46 #include <boost/utility.hpp>
47 #include <boost/bind.hpp>
48 #include <boost/current_function.hpp>
49
50 using lyx::support::libFileSearch;
51 using lyx::support::ForkedcallsController;
52
53 using std::endl;
54 using std::min;
55 using std::max;
56 using std::string;
57
58
59 namespace {
60
61 // All the below connection objects are needed because of a bug in some
62 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
63 // to these connections we avoid a segfault upon startup, and also at exit.
64 // (Lgb)
65
66 boost::signals::connection timecon;
67
68 } // anon namespace
69
70 namespace lyx {
71 namespace frontend {
72
73 WorkArea::WorkArea(int id, LyXView & lyx_view)
74         : id_(id), buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
75           cursor_visible_(false), cursor_timeout_(400)
76 {
77         // Start loading the pixmap as soon as possible
78         //if (lyxrc.show_banner) {
79         //      showBanner();
80         //}
81
82         // Setup the signals
83         timecon = cursor_timeout_.timeout
84                 .connect(boost::bind(&WorkArea::toggleCursor, this));
85
86         cursor_timeout_.start();
87 }
88
89
90 void WorkArea::setBufferView(BufferView * buffer_view)
91 {
92         if (buffer_view_) {
93                 message_connection_.disconnect();
94                 lyx_view_.disconnectBufferView();
95         }
96
97         theApp->setBufferView(buffer_view);
98
99         hideCursor();
100         buffer_view_ = buffer_view;
101         toggleCursor();
102
103         message_connection_ = buffer_view_->message.connect(
104                         boost::bind(&WorkArea::displayMessage, this, _1));
105
106         lyx_view_.connectBufferView(*buffer_view);
107 }
108
109
110 BufferView & WorkArea::bufferView()
111 {
112         return *buffer_view_;
113 }
114
115
116 BufferView const & WorkArea::bufferView() const
117 {
118         return *buffer_view_;
119 }
120
121
122 void WorkArea::checkAndGreyOut()
123 {
124         if (greyed_out_)
125                 greyOut();
126 }
127
128
129 void WorkArea::redraw()
130 {
131         if (!buffer_view_)
132                 return;
133
134         if (!buffer_view_->buffer()) {
135                 checkAndGreyOut();
136                 return;
137         }
138
139         buffer_view_->updateMetrics(false);
140
141         updateScrollbar();
142
143         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
144         greyed_out_ = false;
145
146         lyxerr[Debug::WORKAREA] << "WorkArea::redraw screen" << endl;
147         int const ymin = std::max(vi.y1, 0);
148         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
149
150         expose(0, ymin, width(), ymax - ymin);
151
152         //lyxerr[Debug::WORKAREA]
153         //<< "  ymin = " << ymin << "  width() = " << width()
154 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
155
156         if (lyxerr.debugging(Debug::WORKAREA))
157                 buffer_view_->coordCache().dump();
158 }
159
160
161 void WorkArea::processKeySym(LyXKeySymPtr key,
162                                                          key_modifier::state state)
163 {
164         hideCursor();
165
166         theLyXFunc().setLyXView(&lyx_view_);
167         theLyXFunc().processKeySym(key, state);
168
169         /* This is perhaps a bit of a hack. When we move
170          * around, or type, it's nice to be able to see
171          * the cursor immediately after the keypress. So
172          * we reset the toggle timeout and force the visibility
173          * of the cursor. Note we cannot do this inside
174          * dispatch() itself, because that's called recursively.
175          */
176 //      if (buffer_view_->buffer())
177         toggleCursor();
178 }
179
180
181 void WorkArea::dispatch(FuncRequest const & cmd0)
182 {
183         // Handle drag&drop
184         if (cmd0.action == LFUN_FILE_OPEN) {
185                 lyx_view_.dispatch(cmd0);
186                 return;
187         }
188
189         theLyXFunc().setLyXView(&lyx_view_);
190
191         buffer_view_->workAreaDispatch(cmd0);
192
193         // Skip these when selecting
194         if (cmd0.action != LFUN_MOUSE_MOTION) {
195                 lyx_view_.updateLayoutChoice();
196                 lyx_view_.updateMenubar();
197                 lyx_view_.updateToolbars();
198         }
199
200         // Slight hack: this is only called currently when we
201         // clicked somewhere, so we force through the display
202         // of the new status here.
203         lyx_view_.clearMessage();
204
205         redraw();
206 }
207
208
209 void WorkArea::resizeBufferView()
210 {
211         lyx_view_.busy(true);
212         lyx_view_.message(_("Formatting document..."));
213         buffer_view_->workAreaResize(width(), height());
214         lyx_view_.updateLayoutChoice();
215         redraw();
216         lyx_view_.busy(false);
217         lyx_view_.clearMessage();
218 }
219
220
221 void WorkArea::updateScrollbar()
222 {
223         buffer_view_->updateScrollbar(); 
224         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
225         setScrollbarParams(scroll_.height, scroll_.position,
226                 scroll_.lineScrollHeight);
227 }
228
229
230 void WorkArea::scrollBufferView(int position)
231 {
232         buffer_view_->scrollDocView(position);
233         redraw();
234         hideCursor();
235         if (lyxrc.cursor_follows_scrollbar) {
236                 buffer_view_->setCursorFromScrollbar();
237                 lyx_view_.updateLayoutChoice();
238         }
239         toggleCursor();
240 }
241
242
243 void WorkArea::greyOut()
244 {
245         greyed_out_ = true;
246         expose(4, 5, 3, 3);
247 }
248
249
250 void WorkArea::showCursor()
251 {
252         if (cursor_visible_)
253                 return;
254
255         if (!buffer_view_->buffer())
256                 return;
257
258         CursorShape shape = BAR_SHAPE;
259
260         LyXText const & text = *buffer_view_->getLyXText();
261         LyXFont const & realfont = text.real_current_font;
262         BufferParams const & bp = buffer_view_->buffer()->params();
263         bool const samelang = realfont.language() == bp.language;
264         bool const isrtl = realfont.isVisibleRightToLeft();
265
266         if (!samelang || isrtl != bp.language->rightToLeft()) {
267                 shape = L_SHAPE;
268                 if (isrtl)
269                         shape = REVERSED_L_SHAPE;
270         }
271
272         // The ERT language hack needs fixing up
273         if (realfont.language() == latex_language)
274                 shape = BAR_SHAPE;
275
276         LyXFont const font = buffer_view_->cursor().getFont();
277         FontMetrics const & fm = theFontMetrics(font);
278         int const asc = fm.maxAscent();
279         int const des = fm.maxDescent();
280         int h = asc + des;
281         int x = 0;
282         int y = 0;
283         buffer_view_->cursor().getPos(x, y);
284         y -= asc;
285
286         // if it doesn't touch the screen, don't try to show it
287         if (y + h < 0 || y >= height())
288                 return;
289
290         cursor_visible_ = true;
291         showCursor(x, y, h, shape);
292 }
293
294
295 void WorkArea::hideCursor()
296 {
297         if (!cursor_visible_)
298                 return;
299
300         cursor_visible_ = false;
301         removeCursor();
302 }
303
304
305 void WorkArea::toggleCursor()
306 {
307         if (buffer_view_->buffer()) {
308
309                 if (cursor_visible_)
310                         hideCursor();
311                 else
312                         showCursor();
313
314                 // Use this opportunity to deal with any child processes that
315                 // have finished but are waiting to communicate this fact
316                 // to the rest of LyX.
317                 ForkedcallsController & fcc = ForkedcallsController::get();
318                 fcc.handleCompletedProcesses();
319         }
320
321         cursor_timeout_.restart();
322 }
323
324
325 void WorkArea::displayMessage(lyx::docstring const & message)
326 {
327         lyx_view_.message(message);
328 }
329
330 } // namespace frontend
331 } // namespace lyx