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