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