3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "frontends/LyXView.h"
14 #include "BufferView.h"
16 #include "QWorkArea.h"
18 #include "lcolorcache.h"
19 #include "qt_helpers.h"
22 #include "funcrequest.h"
24 #include "support/os.h"
26 #include <qapplication.h>
27 #include <qclipboard.h>
28 #include <qdragobject.h>
30 #include <qmainwindow.h>
38 #include <Carbon/Carbon.h>
42 #include <support/lstrings.h>
44 using lyx::support::subst;
49 namespace os = lyx::support::os;
52 QWorkArea * wa_ptr = 0;
55 QWorkArea::QWorkArea(LyXView & owner, int, int)
56 : WorkArea(), QWidget(qApp->mainWidget()), owner_(owner), painter_(*this)
58 scrollbar_ = new QScrollBar(QScrollBar::Vertical, this);
59 content_ = new QContentPane(this);
61 (static_cast<QMainWindow*>(qApp->mainWidget()))->setCentralWidget(this);
63 setFocusProxy(content_);
68 // It is said that this helps reduce flicker
69 content_->setBackgroundMode(NoBackground);
71 QHBoxLayout * vl = new QHBoxLayout(this);
72 vl->addWidget(content_, 5);
73 vl->addWidget(scrollbar_, 0);
82 QWorkArea::~QWorkArea()
87 void QWorkArea::setScrollbarParams(int h, int pos, int line_h)
89 // do what cursor movement does (some grey)
92 int max = std::max(0, h - height());
94 scrollbar_->setRange(0, max);
95 content_->trackScrollbar(false);
96 scrollbar_->setValue(pos);
97 content_->trackScrollbar(true);
98 scrollbar_->setLineStep(line_h);
99 scrollbar_->setPageStep(height());
103 bool lyxX11EventFilter(XEvent * xev)
106 case SelectionRequest:
107 lyxerr[Debug::GUI] << "X requested selection." << endl;
109 wa_ptr->view().view()->selectionRequested();
112 lyxerr[Debug::GUI] << "Lost selection." << endl;
114 wa_ptr->view().view()->selectionLost();
123 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
125 DescType returnedType;
127 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
128 typeWildCard, &returnedType, nil, 0,
131 case errAEDescNotFound:
134 return errAEEventNotHandled;
141 pascal OSErr handleOpenDocuments(const AppleEvent* inEvent,
142 AppleEvent* /*reply*/, long /*refCon*/)
145 AEDescList documentList;
146 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
151 err = checkAppleEventForMissingParams(*inEvent);
154 err = AECountItems(&documentList, &documentCount);
155 for (long documentIndex = 1;
156 err == noErr && documentIndex <= documentCount;
158 DescType returnedType;
163 err = AESizeOfNthItem(&documentList, documentIndex,
164 &returnedType, &actualSize);
166 err = AEGetNthPtr(&documentList, documentIndex,
168 &returnedType, (Ptr)&ref,
169 sizeof(FSRef), &actualSize);
171 FSRefMakePath(&ref, (UInt8*)qstr_buf,
173 s_arg=QString::fromUtf8(qstr_buf);
174 wa_ptr->view().view()->workAreaDispatch(
175 FuncRequest(LFUN_FILE_OPEN,
182 AEDisposeDesc(&documentList);
187 void QWorkArea::haveSelection(bool own) const
189 wa_ptr = const_cast<QWorkArea*>(this);
191 if (!QApplication::clipboard()->supportsSelection())
195 QApplication::clipboard()->setSelectionMode(true);
196 QApplication::clipboard()->setText(QString());
198 // We don't need to do anything if own = false, as this case is
203 string const QWorkArea::getClipboard() const
205 QApplication::clipboard()->setSelectionMode(true);
206 QString str = QApplication::clipboard()->text();
210 // The MAC clipboard uses \r for lineendings, and we use \n
211 return subst(fromqstr(str), '\r', '\n');
213 return fromqstr(str);
218 void QWorkArea::putClipboard(string const & str) const
220 QApplication::clipboard()->setSelectionMode(true);
222 // The MAC clipboard uses \r for lineendings, and we use \n
223 QApplication::clipboard()->setText(toqstr(subst(str, '\n', '\r')));
225 QApplication::clipboard()->setText(toqstr(str));
230 void QWorkArea::dragEnterEvent(QDragEnterEvent * event)
232 event->accept(QUriDrag::canDecode(event));
236 void QWorkArea::dropEvent(QDropEvent * event)
240 if (QUriDrag::decodeLocalFiles(event, files)) {
241 lyxerr[Debug::GUI] << "QWorkArea::dropEvent: got URIs!"
243 for (QStringList::Iterator i = files.begin();
244 i != files.end(); ++i) {
245 string const file = os::internal_path(fromqstr(*i));
246 view().view()->workAreaDispatch(FuncRequest(LFUN_FILE_OPEN, file));