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