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