]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QWorkArea.C
The Cygwin path fix.
[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 #include "QWorkArea.h"
14
15 #include "lcolorcache.h"
16 #include "qt_helpers.h"
17
18 #include "debug.h"
19 #include "funcrequest.h"
20 #include "LColor.h"
21
22 #include <qapplication.h>
23 #include <qclipboard.h>
24 #include <qdragobject.h>
25 #include <qlayout.h>
26 #include <qmainwindow.h>
27 #include <qpainter.h>
28
29 #ifdef Q_WS_X11
30 #include <X11/Xlib.h>
31 #endif
32
33 #ifdef Q_WS_MACX
34 #include <Carbon/Carbon.h>
35 #endif
36
37 using std::endl;
38 using std::string;
39
40 namespace {
41 QWorkArea const * wa_ptr = 0;
42 }
43
44 QWorkArea::QWorkArea(LyXView &, 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         setAcceptDrops(true);
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 #ifdef Q_WS_MACX
64         wa_ptr = this;
65 #endif
66         show();
67 }
68
69
70 QWorkArea::~QWorkArea()
71 {
72 }
73
74
75 void QWorkArea::setScrollbarParams(int h, int pos, int line_h)
76 {
77         // do what cursor movement does (some grey)
78         h += height() / 4;
79
80         int max = std::max(0, h - height());
81
82         scrollbar_->setRange(0, max);
83         content_->trackScrollbar(false);
84         scrollbar_->setValue(pos);
85         content_->trackScrollbar(true);
86         scrollbar_->setLineStep(line_h);
87         scrollbar_->setPageStep(height());
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 #ifdef Q_WS_MACX
110 namespace{
111 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
112  {
113         DescType returnedType;
114         Size actualSize;
115         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
116                                       typeWildCard, &returnedType, nil, 0,
117                                       &actualSize);
118         switch (err) {
119         case errAEDescNotFound:
120                 return noErr;
121         case noErr:
122                 return errAEEventNotHandled;
123         default:
124                 return err;
125         }
126  }
127 }
128
129 pascal OSErr handleOpenDocuments(const AppleEvent* inEvent,
130                                  AppleEvent* /*reply*/, long /*refCon*/)
131 {
132         QString s_arg;
133         AEDescList documentList;
134         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
135                                    &documentList);
136         if (err != noErr)
137                 return err;
138
139         err = checkAppleEventForMissingParams(*inEvent);
140         if (err == noErr) {
141                 long documentCount;
142                 err = AECountItems(&documentList, &documentCount);
143                 for (long documentIndex = 1;
144                      err == noErr && documentIndex <= documentCount;
145                      documentIndex++) {
146                         DescType returnedType;
147                         Size actualSize;
148                         AEKeyword keyword;
149                         FSRef ref;
150                         char qstr_buf[1024];
151                         err = AESizeOfNthItem(&documentList, documentIndex,
152                                               &returnedType, &actualSize);
153                         if (err == noErr) {
154                                 err = AEGetNthPtr(&documentList, documentIndex,
155                                                   typeFSRef, &keyword,
156                                                   &returnedType, (Ptr)&ref,
157                                                   sizeof(FSRef), &actualSize);
158                                 if (err == noErr) {
159                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
160                                                       1024);
161                                         s_arg=QString::fromUtf8(qstr_buf);
162                                         wa_ptr->dispatch(
163                                                 FuncRequest(LFUN_FILE_OPEN,
164                                                             fromqstr(s_arg)));
165                                         break;
166                                 }
167                         }
168                 } // for ...
169         }
170         AEDisposeDesc(&documentList);
171         return err;
172 }
173 #endif  // Q_WS_MACX
174
175 void QWorkArea::haveSelection(bool own) const
176 {
177         wa_ptr = this;
178
179 #if QT_VERSION >= 300
180         if (!QApplication::clipboard()->supportsSelection())
181                 return;
182
183         if (own) {
184                 QApplication::clipboard()->setSelectionMode(true);
185                 QApplication::clipboard()->setText(QString());
186         }
187         // We don't need to do anything if own = false, as this case is
188         // handled by QT.
189 #endif
190 }
191
192
193 string const QWorkArea::getClipboard() const
194 {
195 #if QT_VERSION >= 300
196         QApplication::clipboard()->setSelectionMode(true);
197 #endif
198         QString str = QApplication::clipboard()->text();
199         if (str.isNull())
200                 return string();
201         return fromqstr(str);
202 }
203
204
205 void QWorkArea::putClipboard(string const & str) const
206 {
207 #if QT_VERSION >= 300
208         QApplication::clipboard()->setSelectionMode(true);
209 #endif
210         QApplication::clipboard()->setText(toqstr(str));
211 }
212
213
214 void QWorkArea::dragEnterEvent(QDragEnterEvent * event)
215 {
216         event->accept(QUriDrag::canDecode(event));
217 }
218
219
220 void QWorkArea::dropEvent(QDropEvent* event)
221 {
222         QStringList files;
223
224         if (QUriDrag::decodeLocalFiles(event, files)) {
225                 lyxerr[Debug::GUI] << "QWorkArea::dropEvent: got URIs!"
226                                    << endl;
227                 for (QStringList::Iterator i = files.begin();
228                      i!=files.end(); ++i)
229                         dispatch(FuncRequest(LFUN_FILE_OPEN, fromqstr(*i)));
230         }
231 }