]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWorkArea.h
9a0e2a2d22e72f44a8a51eb9fd67a60695df3c04
[lyx.git] / src / frontends / qt4 / GuiWorkArea.h
1 // -*- C++ -*-
2 /**
3  * \file GuiWorkArea.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef WORKAREA_H
14 #define WORKAREA_H
15
16 #include "frontends/WorkArea.h"
17
18 #include "DocIterator.h"
19 #include "FuncRequest.h"
20 #include "qt_helpers.h"
21 #include "support/docstring.h"
22 #include "support/Timeout.h"
23
24 #include <QAbstractScrollArea>
25 #include <QMouseEvent>
26 #include <QPixmap>
27 #include <QResizeEvent>
28 #include <QTabBar>
29 #include <QTabWidget>
30 #include <QTimer>
31
32 class QContextMenuEvent;
33 class QDragEnterEvent;
34 class QDropEvent;
35 class QKeyEvent;
36 class QWheelEvent;
37 class QPaintEvent;
38 class QToolButton;
39 class QWidget;
40
41 #ifdef CursorShape
42 #undef CursorShape
43 #endif
44
45 namespace lyx {
46
47 class Buffer;
48
49 namespace frontend {
50
51 class GuiCompleter;
52 class GuiView;
53 class GuiWorkArea;
54
55 /// for emulating triple click
56 class DoubleClick {
57 public:
58         ///
59         DoubleClick() : state(Qt::NoButton), active(false) {}
60         ///
61         DoubleClick(QMouseEvent * e) : state(e->button()), active(true) {}
62         ///
63         bool operator==(QMouseEvent const & e) { return state == e.button(); }
64         ///
65 public:
66         ///
67         Qt::MouseButton state;
68         ///
69         bool active;
70 };
71
72 /** Qt only emits mouse events when the mouse is being moved, but
73  *  we want to generate 'pseudo' mouse events when the mouse button is
74  *  pressed and the mouse cursor is below the bottom, or above the top
75  *  of the work area. In this way, we'll be able to continue scrolling
76  *  (and selecting) the text.
77  *
78  *  This class stores all the parameters needed to make this happen.
79  */
80 class SyntheticMouseEvent
81 {
82 public:
83         SyntheticMouseEvent();
84
85         FuncRequest cmd;
86         Timeout timeout;
87         bool restart_timeout;
88         int x_old;
89         int y_old;
90         int min_scrollbar_old;
91         int max_scrollbar_old;
92 };
93
94
95 /**
96  * Implementation of the work area (buffer view GUI)
97 */
98 class CursorWidget;
99
100 class GuiWorkArea : public QAbstractScrollArea, public WorkArea
101 {
102         Q_OBJECT
103
104 public:
105         ///
106         GuiWorkArea(QWidget *);
107         ///
108         GuiWorkArea(Buffer & buffer, GuiView & gv);
109         ///
110         ~GuiWorkArea();
111
112         ///
113         void init();
114         ///
115         void setBuffer(Buffer &);
116         ///
117         void setGuiView(GuiView &);
118         ///
119         void setFullScreen(bool full_screen);
120         /// is GuiView in fullscreen mode?
121         bool isFullScreen();
122         ///
123         void scheduleRedraw() { schedule_redraw_ = true; }
124         ///
125         BufferView & bufferView();
126         ///
127         BufferView const & bufferView() const;
128         ///
129         void redraw(bool update_metrics);
130         ///
131         void stopBlinkingCursor();
132         ///
133         void startBlinkingCursor();
134         /// Process Key pressed event.
135         /// This needs to be public because it is accessed externally by GuiView.
136         void processKeySym(KeySymbol const & key, KeyModifier mod);
137         ///
138         void resizeBufferView();
139
140         bool inDialogMode() const { return dialog_mode_; }
141         void setDialogMode(bool mode) { dialog_mode_ = mode; }
142
143         ///
144         GuiCompleter & completer() { return *completer_; }
145
146
147         /// Return the GuiView this workArea belongs to
148         GuiView const & view() const { return *lyx_view_; }
149         GuiView & view() { return *lyx_view_; }
150
151 Q_SIGNALS:
152         ///
153         void titleChanged(GuiWorkArea *);
154
155 private Q_SLOTS:
156         /// Scroll the BufferView.
157         /**
158           * This is a slot for the valueChanged() signal of the vertical scrollbar.
159           * \p value value of the scrollbar.
160         */
161         void scrollTo(int value);
162         /// timer to limit triple clicks
163         void doubleClickTimeout();
164         /// toggle the cursor's visibility
165         void toggleCursor();
166         /// close this work area.
167         /// Slot for Buffer::closing signal.
168         void close();
169         /// Slot to restore proper scrollbar behaviour.
170         void fixVerticalScrollBar();
171
172 private:
173         friend class GuiCompleter;
174
175         /// update the passed area.
176         void update(int x, int y, int w, int h);
177         ///
178         void updateScreen();
179
180         /// paint the cursor and store the background
181         virtual void showCursor(int x, int y, int h,
182                 bool l_shape, bool rtl, bool completable);
183
184         /// hide the cursor
185         virtual void removeCursor();
186
187         /// This function should be called to update the buffer readonly status.
188         void setReadOnly(bool);
189
190         /// Update window titles of all users.
191         void updateWindowTitle();
192         ///
193         bool event(QEvent *);
194         ///
195         void contextMenuEvent(QContextMenuEvent *);
196         ///
197         void focusInEvent(QFocusEvent *);
198         ///
199         void focusOutEvent(QFocusEvent *);
200         /// repaint part of the widget
201         void paintEvent(QPaintEvent * ev);
202         /// widget has been resized
203         void resizeEvent(QResizeEvent * ev);
204         /// mouse button press
205         void mousePressEvent(QMouseEvent * ev);
206         /// mouse button release
207         void mouseReleaseEvent(QMouseEvent * ev);
208         /// mouse double click of button
209         void mouseDoubleClickEvent(QMouseEvent * ev);
210         /// mouse motion
211         void mouseMoveEvent(QMouseEvent * ev);
212         /// wheel event
213         void wheelEvent(QWheelEvent * ev);
214         /// key press
215         void keyPressEvent(QKeyEvent * ev);
216         /// IM events
217         void inputMethodEvent(QInputMethodEvent * ev);
218         /// IM query
219         QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
220
221         /// The slot connected to SyntheticMouseEvent::timeout.
222         void generateSyntheticMouseEvent();
223         ///
224         void dispatch(FuncRequest const & cmd0, KeyModifier = NoModifier);
225         /// hide the visible cursor, if it is visible
226         void hideCursor();
227         /// show the cursor if it is not visible
228         void showCursor();
229         ///
230         void updateScrollbar();
231
232         ///
233         BufferView * buffer_view_;
234         /// Read only Buffer status cache.
235         bool read_only_;
236         ///
237         GuiView * lyx_view_;
238         /// is the cursor currently displayed
239         bool cursor_visible_;
240
241         ///
242         QTimer cursor_timeout_;
243         ///
244         SyntheticMouseEvent synthetic_mouse_event_;
245         ///
246         DoubleClick dc_event_;
247
248         ///
249         CursorWidget * cursor_;
250         ///
251         QPixmap screen_;
252         ///
253         bool need_resize_;
254         ///
255         bool schedule_redraw_;
256         ///
257         int preedit_lines_;
258
259         ///
260         GuiCompleter * completer_;
261
262         /// Special mode in which Esc and Enter (with or without Shift)
263         /// are ignored
264         bool dialog_mode_;
265         /// store the position of the rightclick when the mouse is
266         /// pressed. This is used to get the correct context menu 
267         /// when the menu is actually shown (after releasing on Windwos).
268         QPoint context_target_pos_;
269 }; // GuiWorkArea
270
271
272 class EmbeddedWorkArea : public GuiWorkArea
273 {
274         Q_OBJECT
275 public:
276         ///
277         EmbeddedWorkArea(QWidget *);
278         ~EmbeddedWorkArea();
279
280         /// Dummy methods for Designer.
281         void setWidgetResizable(bool) {}
282         void setWidget(QWidget *) {}
283
284         QSize sizeHint () const;
285         ///
286         void disable();
287
288 protected:
289         ///
290         void closeEvent(QCloseEvent * ev);
291         ///
292         void hideEvent(QHideEvent *ev);
293
294 private:
295         /// Embedded Buffer.
296         Buffer * buffer_;
297 }; // EmbeddedWorkArea
298
299
300 /// A tabbed set of GuiWorkAreas.
301 class TabWorkArea : public QTabWidget
302 {
303         Q_OBJECT
304 public:
305         TabWorkArea(QWidget * parent = 0);
306
307         ///
308         void setFullScreen(bool full_screen);
309         void showBar(bool show);
310         void closeAll();
311         bool setCurrentWorkArea(GuiWorkArea *);
312         GuiWorkArea * addWorkArea(Buffer & buffer, GuiView & view);
313         bool removeWorkArea(GuiWorkArea *);
314         GuiWorkArea * currentWorkArea();
315         GuiWorkArea * workArea(Buffer & buffer);
316
317 Q_SIGNALS:
318         ///
319         void currentWorkAreaChanged(GuiWorkArea *);
320         ///
321         void lastWorkAreaRemoved();
322
323 public Q_SLOTS:
324         /// close current buffer, or the one given by \c clicked_tab_
325         void closeCurrentBuffer();
326         /// hide current tab, or the one given by \c clicked_tab_
327         void hideCurrentTab();
328         /// close the tab given by \c index
329         void closeTab(int index);
330         ///
331         void updateTabTexts();
332         
333 private Q_SLOTS:
334         ///
335         void on_currentTabChanged(int index);
336         ///
337         void showContextMenu(const QPoint & pos);
338         ///
339         void moveTab(int fromIndex, int toIndex);
340         ///
341         void mouseDoubleClickEvent(QMouseEvent * event);
342
343 private:
344         ///
345         int clicked_tab_;
346         ///
347         QToolButton * closeBufferButton;
348 }; // TabWorkArea
349
350
351 class DragTabBar : public QTabBar
352 {
353         Q_OBJECT
354 public:
355         ///
356         DragTabBar(QWidget * parent = 0);
357
358 #if QT_VERSION < 0x040300
359         ///
360         int tabAt(QPoint const & position) const;
361 #endif
362
363 protected:
364         ///
365         void mousePressEvent(QMouseEvent * event);
366         ///
367         void mouseMoveEvent(QMouseEvent * event);
368         ///
369         void dragEnterEvent(QDragEnterEvent * event);
370         ///
371         void dropEvent(QDropEvent * event);
372
373 private:
374         ///
375         QPoint dragStartPos_;
376         ///
377         int dragCurrentIndex_;
378
379 Q_SIGNALS:
380         ///
381         void tabMoveRequested(int fromIndex, int toIndex);
382 };
383
384 } // namespace frontend
385 } // namespace lyx
386
387 #endif // WORKAREA_H