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