]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiWorkArea.h
a0fcf737860106bf8d3930b241a8605462968df1
[features.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 };
89
90
91 /**
92  * Implementation of the work area (buffer view GUI)
93 */
94 class CursorWidget;
95
96 class GuiWorkArea : public QAbstractScrollArea, public WorkArea
97 {
98         Q_OBJECT
99
100 public:
101         ///
102         GuiWorkArea(QWidget *);
103         ///
104         GuiWorkArea(Buffer & buffer, GuiView & gv);
105         ///
106         ~GuiWorkArea();
107
108         ///
109         void init();
110         ///
111         void setBuffer(Buffer &);
112         ///
113         void setGuiView(GuiView &);
114         ///
115         void setFullScreen(bool full_screen);
116         /// is GuiView in fullscreen mode?
117         bool isFullScreen();
118         ///
119         void scheduleRedraw() { schedule_redraw_ = true; }
120         ///
121         BufferView & bufferView();
122         ///
123         BufferView const & bufferView() const;
124         ///
125         void redraw(bool update_metrics);
126         ///
127         void stopBlinkingCursor();
128         ///
129         void startBlinkingCursor();
130         /// Process Key pressed event.
131         /// This needs to be public because it is accessed externally by GuiView.
132         void processKeySym(KeySymbol const & key, KeyModifier mod);
133         ///
134         void resizeBufferView();
135
136         bool inDialogMode() const { return dialog_mode_; }
137         void setDialogMode(bool mode) { dialog_mode_ = mode; }
138
139         ///
140         GuiCompleter & completer() { return *completer_; }
141
142         Qt::CursorShape cursorShape() const;
143         void setCursorShape(Qt::CursorShape shape);
144
145
146         /// Return the GuiView this workArea belongs to
147         GuiView const & view() const { return *lyx_view_; }
148         GuiView & view() { return *lyx_view_; }
149
150 Q_SIGNALS:
151         ///
152         void titleChanged(GuiWorkArea *);
153
154 private Q_SLOTS:
155         /// Scroll the BufferView.
156         /**
157           * This is a slot for the valueChanged() signal of the vertical scrollbar.
158           * \p value value of the scrollbar.
159         */
160         void scrollTo(int value);
161         /// timer to limit triple clicks
162         void doubleClickTimeout();
163         /// toggle the cursor's visibility
164         void toggleCursor();
165         /// close this work area.
166         /// Slot for Buffer::closing signal.
167         void close();
168         /// Slot to restore proper scrollbar behaviour.
169         void fixVerticalScrollBar();
170
171 private:
172         friend class GuiCompleter;
173
174         /// update the passed area.
175         void update(int x, int y, int w, int h);
176         ///
177         void updateScreen();
178
179         /// paint the cursor and store the background
180         virtual void showCursor(int x, int y, int h,
181                 bool l_shape, bool rtl, bool completable);
182
183         /// hide the cursor
184         virtual void removeCursor();
185
186         /// This function should be called to update the buffer readonly status.
187         void setReadOnly(bool);
188
189         /// Update window titles of all users.
190         void updateWindowTitle();
191         ///
192         bool event(QEvent *);
193         ///
194         void contextMenuEvent(QContextMenuEvent *);
195         ///
196         void focusInEvent(QFocusEvent *);
197         ///
198         void focusOutEvent(QFocusEvent *);
199         /// repaint part of the widget
200         void paintEvent(QPaintEvent * ev);
201         /// widget has been resized
202         void resizeEvent(QResizeEvent * ev);
203         /// mouse button press
204         void mousePressEvent(QMouseEvent * ev);
205         /// mouse button release
206         void mouseReleaseEvent(QMouseEvent * ev);
207         /// mouse double click of button
208         void mouseDoubleClickEvent(QMouseEvent * ev);
209         /// mouse motion
210         void mouseMoveEvent(QMouseEvent * ev);
211         /// wheel event
212         void wheelEvent(QWheelEvent * ev);
213         /// key press
214         void keyPressEvent(QKeyEvent * ev);
215         /// IM events
216         void inputMethodEvent(QInputMethodEvent * ev);
217         /// IM query
218         QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
219
220         /// The slot connected to SyntheticMouseEvent::timeout.
221         void generateSyntheticMouseEvent();
222         ///
223         void dispatch(FuncRequest const & cmd0, KeyModifier = NoModifier);
224         /// hide the visible cursor, if it is visible
225         void hideCursor();
226         /// show the cursor if it is not visible
227         void showCursor();
228         ///
229         void updateScrollbar();
230
231         ///
232         BufferView * buffer_view_;
233         /// Read only Buffer status cache.
234         bool read_only_;
235         ///
236         GuiView * lyx_view_;
237         /// is the cursor currently displayed
238         bool cursor_visible_;
239
240         ///
241         QTimer cursor_timeout_;
242         ///
243         SyntheticMouseEvent synthetic_mouse_event_;
244         ///
245         DoubleClick dc_event_;
246
247         ///
248         CursorWidget * cursor_;
249         ///
250         QPixmap screen_;
251         ///
252         bool need_resize_;
253         ///
254         bool schedule_redraw_;
255         ///
256         int preedit_lines_;
257
258         ///
259         GuiCompleter * completer_;
260
261         /// Special mode in which Esc and Enter (with or without Shift)
262         /// are ignored
263         bool dialog_mode_;
264         /// store the position of the rightclick when the mouse is
265         /// pressed. This is used to get the correct context menu 
266         /// when the menu is actually shown (after releasing on Windwos).
267         QPoint context_target_pos_;
268 }; // GuiWorkArea
269
270
271 class EmbeddedWorkArea : public GuiWorkArea
272 {
273         Q_OBJECT
274 public:
275         ///
276         EmbeddedWorkArea(QWidget *);
277         ~EmbeddedWorkArea();
278
279         /// Dummy methods for Designer.
280         void setWidgetResizable(bool) {}
281         void setWidget(QWidget *) {}
282
283         QSize sizeHint () const;
284         ///
285         void disable();
286
287 protected:
288         ///
289         void closeEvent(QCloseEvent * ev);
290         ///
291         void hideEvent(QHideEvent *ev);
292
293 private:
294         /// Embedded Buffer.
295         Buffer * buffer_;
296 }; // EmbeddedWorkArea
297
298
299 /// A tabbed set of GuiWorkAreas.
300 class TabWorkArea : public QTabWidget
301 {
302         Q_OBJECT
303 public:
304         TabWorkArea(QWidget * parent = 0);
305
306         ///
307         void setFullScreen(bool full_screen);
308         void showBar(bool show);
309         void closeAll();
310         bool setCurrentWorkArea(GuiWorkArea *);
311         GuiWorkArea * addWorkArea(Buffer & buffer, GuiView & view);
312         bool removeWorkArea(GuiWorkArea *);
313         GuiWorkArea * currentWorkArea();
314         GuiWorkArea * workArea(Buffer & buffer);
315         GuiWorkArea * workArea(int index);
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