]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QWorkArea.C
clean up Abdel's cleanup
[lyx.git] / src / frontends / qt3 / 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 "frontends/LyXView.h"
14 #include "BufferView.h"
15
16 #include "QWorkArea.h"
17
18 #include "lcolorcache.h"
19 #include "qt_helpers.h"
20
21 #include "debug.h"
22 #include "funcrequest.h"
23 #include "LColor.h"
24 #include "support/os.h"
25
26 #include <qapplication.h>
27 #include <qclipboard.h>
28 #include <qdragobject.h>
29 #include <qlayout.h>
30 #include <qmainwindow.h>
31 #include <qpainter.h>
32
33 #ifdef Q_WS_X11
34 #include <X11/Xlib.h>
35 #endif
36
37 #ifdef Q_WS_MACX
38 #include <Carbon/Carbon.h>
39 #endif
40
41 using std::endl;
42 using std::string;
43
44 namespace os = lyx::support::os;
45
46 namespace {
47 lyx::frontend::QWorkArea * wa_ptr = 0;
48 }
49
50 namespace lyx {
51 namespace frontend {
52
53 QWorkArea::QWorkArea(LyXView & owner, int, int)
54         : QWidget(qApp->mainWidget()), owner_(owner), painter_(*this)
55 {
56         scrollbar_ = new QScrollBar(QScrollBar::Vertical, this);
57         content_ = new QContentPane(this);
58
59         (static_cast<QMainWindow*>(qApp->mainWidget()))->setCentralWidget(this);
60
61         setFocusProxy(content_);
62         setAcceptDrops(true);
63
64         content_->show();
65
66         // It is said that this helps reduce flicker
67         content_->setBackgroundMode(NoBackground);
68
69         QHBoxLayout * vl = new QHBoxLayout(this);
70         vl->addWidget(content_, 5);
71         vl->addWidget(scrollbar_, 0);
72
73 #ifdef Q_WS_MACX
74         wa_ptr = this;
75 #endif
76         show();
77 }
78
79
80 QWorkArea::~QWorkArea()
81 {
82 }
83
84
85 void QWorkArea::setScrollbarParams(int h, int pos, int line_h)
86 {
87         scrollbar_->setTracking(false);
88         // do what cursor movement does (some grey)
89         h += height() / 4;
90
91         int max = std::max(0, h - height());
92
93         scrollbar_->setRange(0, max);
94         content_->trackScrollbar(false);
95         scrollbar_->setValue(pos);
96         content_->trackScrollbar(true);
97         scrollbar_->setLineStep(line_h);
98         scrollbar_->setPageStep(height());
99
100         scrollbar_->setTracking(true);
101 }
102
103 } // namespace frontend
104 } // namespace lyx
105
106
107 #ifdef Q_WS_X11
108 bool lyxX11EventFilter(XEvent * xev)
109 {
110         switch (xev->type) {
111         case SelectionRequest:
112                 lyxerr[Debug::GUI] << "X requested selection." << endl;
113                 if (wa_ptr)
114                         wa_ptr->view().view()->selectionRequested();
115                 break;
116         case SelectionClear:
117                 lyxerr[Debug::GUI] << "Lost selection." << endl;
118                 if (wa_ptr)
119                         wa_ptr->view().view()->selectionLost();
120                 break;
121         }
122         return false;
123 }
124 #endif
125
126 #ifdef Q_WS_MACX
127 namespace{
128 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
129  {
130         DescType returnedType;
131         Size actualSize;
132         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
133                                       typeWildCard, &returnedType, nil, 0,
134                                       &actualSize);
135         switch (err) {
136         case errAEDescNotFound:
137                 return noErr;
138         case noErr:
139                 return errAEEventNotHandled;
140         default:
141                 return err;
142         }
143  }
144 }
145
146 pascal OSErr handleOpenDocuments(const AppleEvent* inEvent,
147                                  AppleEvent* /*reply*/, long /*refCon*/)
148 {
149         QString s_arg;
150         AEDescList documentList;
151         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
152                                    &documentList);
153         if (err != noErr)
154                 return err;
155
156         err = checkAppleEventForMissingParams(*inEvent);
157         if (err == noErr) {
158                 long documentCount;
159                 err = AECountItems(&documentList, &documentCount);
160                 for (long documentIndex = 1;
161                      err == noErr && documentIndex <= documentCount;
162                      documentIndex++) {
163                         DescType returnedType;
164                         Size actualSize;
165                         AEKeyword keyword;
166                         FSRef ref;
167                         char qstr_buf[1024];
168                         err = AESizeOfNthItem(&documentList, documentIndex,
169                                               &returnedType, &actualSize);
170                         if (err == noErr) {
171                                 err = AEGetNthPtr(&documentList, documentIndex,
172                                                   typeFSRef, &keyword,
173                                                   &returnedType, (Ptr)&ref,
174                                                   sizeof(FSRef), &actualSize);
175                                 if (err == noErr) {
176                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
177                                                       1024);
178                                         s_arg=QString::fromUtf8(qstr_buf);
179                                         wa_ptr->view().view()->workAreaDispatch(
180                                                 FuncRequest(LFUN_FILE_OPEN,
181                                                             fromqstr(s_arg)));
182                                         break;
183                                 }
184                         }
185                 } // for ...
186         }
187         AEDisposeDesc(&documentList);
188         return err;
189 }
190 #endif  // Q_WS_MACX
191
192
193 namespace lyx {
194 namespace frontend {
195
196 void QWorkArea::haveSelection(bool own)
197 {
198         wa_ptr = const_cast<QWorkArea*>(this);
199
200         if (!QApplication::clipboard()->supportsSelection())
201                 return;
202
203         if (own) {
204                 QApplication::clipboard()->setSelectionMode(true);
205                 QApplication::clipboard()->setText(QString());
206         }
207         // We don't need to do anything if own = false, as this case is
208         // handled by QT.
209 }
210
211
212 void QWorkArea::dragEnterEvent(QDragEnterEvent * event)
213 {
214         event->accept(QUriDrag::canDecode(event));
215 }
216
217
218 void QWorkArea::dropEvent(QDropEvent * event)
219 {
220         QStringList files;
221
222         if (QUriDrag::decodeLocalFiles(event, files)) {
223                 lyxerr[Debug::GUI] << "QWorkArea::dropEvent: got URIs!"
224                                    << endl;
225                 for (QStringList::Iterator i = files.begin();
226                      i != files.end(); ++i) {
227                         string const file = os::internal_path(fromqstr(*i));
228                         view().view()->workAreaDispatch(FuncRequest(LFUN_FILE_OPEN, file));
229                 }
230         }
231 }
232
233 } // namespace frontend
234 } // namespace lyx