]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QWorkArea.C
This is the merging of the GUI API cleanup branch that was developed in svn+ssh:...
[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         // do what cursor movement does (some grey)
92         h += height() / 4;
93
94         int max = std::max(0, h - height());
95
96         scrollbar_->setRange(0, max);
97         content_->trackScrollbar(false);
98         scrollbar_->setValue(pos);
99         content_->trackScrollbar(true);
100         scrollbar_->setLineStep(line_h);
101         scrollbar_->setPageStep(height());
102 }
103
104 } // namespace frontend
105 } // namespace lyx
106
107
108 #ifdef Q_WS_X11
109 bool lyxX11EventFilter(XEvent * xev)
110 {
111         switch (xev->type) {
112         case SelectionRequest:
113                 lyxerr[Debug::GUI] << "X requested selection." << endl;
114                 if (wa_ptr)
115                         wa_ptr->view().view()->selectionRequested();
116                 break;
117         case SelectionClear:
118                 lyxerr[Debug::GUI] << "Lost selection." << endl;
119                 if (wa_ptr)
120                         wa_ptr->view().view()->selectionLost();
121                 break;
122         }
123         return false;
124 }
125 #endif
126
127 #ifdef Q_WS_MACX
128 namespace{
129 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
130  {
131         DescType returnedType;
132         Size actualSize;
133         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
134                                       typeWildCard, &returnedType, nil, 0,
135                                       &actualSize);
136         switch (err) {
137         case errAEDescNotFound:
138                 return noErr;
139         case noErr:
140                 return errAEEventNotHandled;
141         default:
142                 return err;
143         }
144  }
145 }
146
147 pascal OSErr handleOpenDocuments(const AppleEvent* inEvent,
148                                  AppleEvent* /*reply*/, long /*refCon*/)
149 {
150         QString s_arg;
151         AEDescList documentList;
152         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
153                                    &documentList);
154         if (err != noErr)
155                 return err;
156
157         err = checkAppleEventForMissingParams(*inEvent);
158         if (err == noErr) {
159                 long documentCount;
160                 err = AECountItems(&documentList, &documentCount);
161                 for (long documentIndex = 1;
162                      err == noErr && documentIndex <= documentCount;
163                      documentIndex++) {
164                         DescType returnedType;
165                         Size actualSize;
166                         AEKeyword keyword;
167                         FSRef ref;
168                         char qstr_buf[1024];
169                         err = AESizeOfNthItem(&documentList, documentIndex,
170                                               &returnedType, &actualSize);
171                         if (err == noErr) {
172                                 err = AEGetNthPtr(&documentList, documentIndex,
173                                                   typeFSRef, &keyword,
174                                                   &returnedType, (Ptr)&ref,
175                                                   sizeof(FSRef), &actualSize);
176                                 if (err == noErr) {
177                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
178                                                       1024);
179                                         s_arg=QString::fromUtf8(qstr_buf);
180                                         wa_ptr->view().view()->workAreaDispatch(
181                                                 FuncRequest(LFUN_FILE_OPEN,
182                                                             fromqstr(s_arg)));
183                                         break;
184                                 }
185                         }
186                 } // for ...
187         }
188         AEDisposeDesc(&documentList);
189         return err;
190 }
191 #endif  // Q_WS_MACX
192
193
194 namespace lyx {
195 namespace frontend {
196
197 void QWorkArea::haveSelection(bool own)
198 {
199         wa_ptr = const_cast<QWorkArea*>(this);
200
201         if (!QApplication::clipboard()->supportsSelection())
202                 return;
203
204         if (own) {
205                 QApplication::clipboard()->setSelectionMode(true);
206                 QApplication::clipboard()->setText(QString());
207         }
208         // We don't need to do anything if own = false, as this case is
209         // handled by QT.
210 }
211
212
213 string const QWorkArea::getClipboard() const
214 {
215         QApplication::clipboard()->setSelectionMode(true);
216         QString str = QApplication::clipboard()->text();
217         if (str.isNull())
218                 return string();
219         return internalLineEnding(fromqstr(str));
220 }
221
222
223 void QWorkArea::putClipboard(string const & str)
224 {
225         QApplication::clipboard()->setSelectionMode(true);
226         QApplication::clipboard()->setText(toqstr(externalLineEnding(str)));
227 }
228
229
230 void QWorkArea::dragEnterEvent(QDragEnterEvent * event)
231 {
232         event->accept(QUriDrag::canDecode(event));
233 }
234
235
236 void QWorkArea::dropEvent(QDropEvent * event)
237 {
238         QStringList files;
239
240         if (QUriDrag::decodeLocalFiles(event, files)) {
241                 lyxerr[Debug::GUI] << "QWorkArea::dropEvent: got URIs!"
242                                    << endl;
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));
247                 }
248         }
249 }
250
251 } // namespace frontend
252 } // namespace lyx