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