]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QWorkArea.C
better selection and scrolling behaviour
[lyx.git] / src / frontends / qt2 / QWorkArea.C
1 /**
2  * \file QWorkArea.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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "debug.h"
18 #include "LyXView.h"
19 #include "version.h" // lyx_version
20
21 #include "support/filetools.h" // LibFileSearch
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
24
25 #include "QWorkArea.h"
26
27 #include <qapplication.h>
28 #include <qevent.h>
29 #include <qpainter.h>
30 #include <qmainwindow.h>
31 #include <qlayout.h>
32 #include <qclipboard.h>
33
34 #ifdef Q_WS_X11
35 #include <X11/Xlib.h>
36 #endif
37
38 #include <cmath>
39 #include <cctype>
40
41 using std::endl;
42 using std::abs;
43 using std::hex;
44
45
46 QWorkArea::QWorkArea(int, int, int, int)
47         : WorkArea(), QWidget(qApp->mainWidget()), painter_(*this)
48 {
49         scrollbar_ = new QScrollBar(QScrollBar::Vertical, this);
50         content_ = new QContentPane(this);
51
52         (static_cast<QMainWindow*>(qApp->mainWidget()))->setCentralWidget(this);
53
54         setFocusProxy(content_);
55
56         content_->show();
57
58         content_->setBackgroundColor(lcolor.getX11Name(LColor::background).c_str());
59
60         QHBoxLayout * vl = new QHBoxLayout(this);
61         vl->addWidget(content_, 5);
62         vl->addWidget(scrollbar_, 0);
63
64         show();
65 }
66
67
68 QWorkArea::~QWorkArea()
69 {
70 }
71
72
73 void QWorkArea::setScrollbarParams(int h, int pos, int line_h)
74 {
75         // do what cursor movement does (some grey)
76         h += height() / 4;
77
78         int max = std::max(0, h - height());
79
80         scrollbar_->setRange(0, max);
81         scrollbar_->setValue(pos);
82         scrollbar_->setLineStep(line_h);
83         scrollbar_->setPageStep(height());
84 }
85
86 namespace {
87 QWorkArea const * wa_ptr = 0;
88 }
89
90 #ifdef Q_WS_X11
91 bool lyxX11EventFilter(XEvent * xev)
92 {
93         switch (xev->type) {
94         case SelectionRequest:
95                 lyxerr[Debug::GUI] << "X requested selection." << endl;
96                 if (wa_ptr)
97                         wa_ptr->selectionRequested();
98                 break;
99         case SelectionClear:
100                 lyxerr[Debug::GUI] << "Lost selection." << endl;
101                 if (wa_ptr)
102                         wa_ptr->selectionLost();
103                 break;
104         }
105         return false;
106 }
107 #endif
108
109 void QWorkArea::haveSelection(bool own) const
110 {
111         wa_ptr = this;
112
113 #if QT_VERSION >= 300
114         if (!QApplication::clipboard()->supportsSelection())
115                 return;
116
117         if (own) {
118                 QApplication::clipboard()->setSelectionMode(true);
119                 QApplication::clipboard()->setText(QString());
120         }
121         // We don't need to do anything if own = false, as this case is
122         // handled by QT.
123 #endif
124 }
125
126
127 string const QWorkArea::getClipboard() const
128 {
129 #if QT_VERSION >= 300
130         QApplication::clipboard()->setSelectionMode(true);
131 #endif
132         QString str = QApplication::clipboard()->text();
133         if (str.isNull())
134                 return string();
135         return str.latin1();
136 }
137
138
139 void QWorkArea::putClipboard(string const & str) const
140 {
141 #if QT_VERSION >= 300
142         QApplication::clipboard()->setSelectionMode(true);
143 #endif
144         QApplication::clipboard()->setText(str.c_str());
145 }