]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QWorkArea.C
35f5891c64e850331bc464fc68c463e8c95a1095
[lyx.git] / src / frontends / qt4 / 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  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include <boost/current_function.hpp>
15
16 #include "QWorkArea.h"
17 #include "QLPainter.h"
18 #include "QLyXKeySym.h"
19
20 #include "lcolorcache.h"
21 #include "qt_helpers.h"
22 #include "BufferView.h"
23 #include "debug.h"
24 #include "funcrequest.h"
25 #include "LColor.h"
26 #include "support/os.h"
27
28 #include <QApplication>
29 #include <QClipboard>
30 #include <QLayout>
31 #include <QMainWindow>
32 #include <QMimeData>
33 #include <QUrl>
34 #include <QDragEnterEvent>
35 #include <QPixmap>
36 #include <QPainter>
37 #include <QScrollBar>
38
39 #include <boost/bind.hpp>
40
41 ///////////////////////////////////////////////////////////////
42 // Specific stuff
43 #ifdef Q_WS_X11
44 #include <X11/Xlib.h>
45 #endif
46
47 #ifdef Q_WS_MACX
48 #include <Carbon/Carbon.h>
49 #include <support/lstrings.h>
50 using lyx::support::subst;
51 #endif
52
53 // You can find other qt-immodule, X11 and MACX specific stuff
54 // at the end of this file...
55 ///////////////////////////////////////////////////////////////
56
57 using std::endl;
58 using std::string;
59
60 namespace os = lyx::support::os;
61
62 namespace {
63
64 QWorkArea * wa_ptr = 0;
65
66 /// return the LyX key state from Qt's
67 key_modifier::state q_key_state(Qt::ButtonState state)
68 {
69         key_modifier::state k = key_modifier::none;
70         if (state & Qt::ControlModifier)
71                 k |= key_modifier::ctrl;
72         if (state & Qt::ShiftModifier)
73                 k |= key_modifier::shift;
74         if (state & Qt::AltModifier)
75                 k |= key_modifier::alt;
76         return k;
77 }
78
79
80 /// return the LyX mouse button state from Qt's
81 mouse_button::state q_button_state(Qt::ButtonState button)
82 {
83         mouse_button::state b = mouse_button::none;
84         switch (button) {
85                 case Qt::LeftButton:
86                         b = mouse_button::button1;
87                         break;
88                 case Qt::MidButton:
89                         b = mouse_button::button2;
90                         break;
91                 case Qt::RightButton:
92                         b = mouse_button::button3;
93                         break;
94                 default:
95                         break;
96         }
97         return b;
98 }
99
100
101 /// return the LyX mouse button state from Qt's
102 mouse_button::state q_motion_state(Qt::ButtonState state)
103 {
104         mouse_button::state b = mouse_button::none;
105         if (state & Qt::LeftButton)
106                 b |= mouse_button::button1;
107         if (state & Qt::MidButton)
108                 b |= mouse_button::button2;
109         if (state & Qt::RightButton)
110                 b |= mouse_button::button3;
111         return b;
112 }
113
114 } // namespace anon
115
116 // This is a 'heartbeat' generating synthetic mouse move events when the
117 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
118 SyntheticMouseEvent::SyntheticMouseEvent()
119         : timeout(200), restart_timeout(true),
120           x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
121 {}
122
123
124 QWorkArea::QWorkArea(LyXView & owner, int w, int h)
125     : QAbstractScrollArea(qApp->mainWidget()), WorkArea(), view_(owner), painter_(this)
126 {
127         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
128         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
129
130         (static_cast<QMainWindow*>(qApp->mainWidget()))->setCentralWidget(this);
131
132         setAcceptDrops(true);
133
134         setMinimumSize(100, 70);
135
136         viewport()->setAutoFillBackground(false);
137         viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
138
139         viewport()->setFocusPolicy(Qt::WheelFocus);
140         viewport()->setFocus();
141         setFocusPolicy(Qt::WheelFocus);
142
143         viewport()->setCursor(Qt::IBeamCursor);
144
145         resize(w, h);
146         show();
147         workWidth_ = w;
148         workHeight_ = h;
149
150         synthetic_mouse_event_.timeout.timeout.connect(
151                 boost::bind(&QWorkArea::generateSyntheticMouseEvent,
152                             this));
153
154         // Initialize the vertical Scroll Bar
155         QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
156                 this, SLOT(adjustViewWithScrollBar(int)));
157
158         // PageStep only depends on the viewport height.
159         verticalScrollBar()->setPageStep(workHeight_);
160
161         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
162                 << "\n Area width\t" << width()
163                 << "\n Area height\t" << height()
164                 << "\n viewport width\t" << viewport()->width()
165                 << "\n viewport height\t" << viewport()->height()
166                 << endl;
167
168 /*
169         // This is the keyboard buffering stuff...
170         // I don't see any need for this under windows. The keyboard is reactive
171     // enough...
172
173         if ( !QObject::connect(&step_timer_, SIGNAL(timeout()),
174                 this, SLOT(keyeventTimeout())) )
175                         lyxerr[Debug::GUI] << "ERROR: keyeventTimeout cannot connect!" << endl;
176
177         // Start the timer, one-shot.
178         step_timer_.start(50, true);
179 */
180
181         ///////////////////////////////////////////////////////////////////////
182         // Specific stuff goes here...
183
184 #if USE_INPUT_METHODS
185         // to make qt-immodule work
186         setInputMethodEnabled(true);
187 #endif
188
189 #ifdef Q_WS_X11
190         // doubleClickInterval() is 400 ms on X11 witch is just too long.
191         // On Windows and Mac OS X, the operating system's value is used.
192         // On Microsoft Windows, calling this function sets the double
193         // click interval for all applications. So we don't!
194         QApplication::setDoubleClickInterval(300);
195 #endif
196
197 #ifdef Q_WS_MACX
198         wa_ptr = this;
199 #endif
200 }
201
202 QWorkArea::~QWorkArea()
203 {
204 }
205
206 void QWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
207 {
208         // do what cursor movement does (some grey)
209         h += height() / 4;
210         int scroll_max_ = std::max(0, h - height());
211
212         verticalScrollBar()->setRange(0, scroll_max_);
213         verticalScrollBar()->setSliderPosition(scroll_pos);
214         verticalScrollBar()->setLineStep(scroll_line_step);
215 }
216
217 void QWorkArea::adjustViewWithScrollBar(int action)
218 {
219         /*
220         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
221                 << " verticalScrollBar val=" << verticalScrollBar()->value()
222                 << " verticalScrollBar pos=" << verticalScrollBar()->sliderPosition()
223                 << " min=" << verticalScrollBar()->minimum()
224                 << " max=" << verticalScrollBar()->maximum()
225                 << " pagestep=" << verticalScrollBar()->pageStep()
226                 << " linestep=" << verticalScrollBar()->lineStep()
227                 << endl;
228         */
229         view_.view()->scrollDocView(verticalScrollBar()->sliderPosition());
230 }
231
232
233 void QWorkArea::haveSelection(bool own) const
234 {
235         /// \todo ask X11 and MAC devels why this wa_ptr is useful.
236         wa_ptr = const_cast<QWorkArea*>(this);
237
238         if (!QApplication::clipboard()->supportsSelection())
239                 return;
240
241         if (own) {
242                 QApplication::clipboard()->setText(QString(), QClipboard::Selection);
243         }
244         // We don't need to do anything if own = false, as this case is
245         // handled by QT.
246 }
247
248
249 string const QWorkArea::getClipboard() const
250 {
251         QString str = QApplication::clipboard()->text(QClipboard::Selection);
252         lyxerr[Debug::ACTION] << "getClipboard: " << (const char*) str << endl;
253         if (str.isNull())
254                 return string();
255 #ifdef Q_WS_MACX
256         // The MAC clipboard uses \r for lineendings, and we use \n
257         return subst(fromqstr(str), '\r', '\n');
258 #else
259         return fromqstr(str);
260 #endif
261 }
262
263
264 void QWorkArea::putClipboard(string const & str) const
265 {
266 #ifdef Q_WS_MACX
267         // The MAC clipboard uses \r for lineendings, and we use \n
268         QApplication::clipboard()->setText(toqstr(subst(str, '\n', '\r')),
269                                            QClipboard::Selection);
270 #else
271         QApplication::clipboard()->setText(toqstr(str), QClipboard::Selection);
272 #endif
273         lyxerr[Debug::ACTION] << "putClipboard: " << str << endl;
274 }
275
276
277 void QWorkArea::dragEnterEvent(QDragEnterEvent * event)
278 {
279         if (event->mimeData()->hasUrls())
280                 event->accept();
281         /// \todo Ask lyx-devel is this is enough:
282         /// if (event->mimeData()->hasFormat("text/plain"))
283         ///     event->acceptProposedAction();
284
285 }
286
287
288 void QWorkArea::dropEvent(QDropEvent* event)
289 {
290         QList<QUrl> files = event->mimeData()->urls();
291         if (files.isEmpty())
292                 return;
293
294         lyxerr[Debug::GUI] << "QWorkArea::dropEvent: got URIs!" << endl;
295         for (int i = 0; i!=files.size(); ++i) {
296                 string const file = os::internal_path(fromqstr(files.at(i).toString()));
297                 if (!file.empty())
298                         view_.view()->workAreaDispatch(FuncRequest(LFUN_FILE_OPEN, file));
299         }
300 }
301
302
303 void QWorkArea::mousePressEvent(QMouseEvent * e)
304 {
305         if (dc_event_.active && dc_event_ == *e) {
306                 dc_event_.active = false;
307                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
308                         dc_event_.x, dc_event_.y,
309                         q_button_state(dc_event_.state));
310                 view_.view()->workAreaDispatch(cmd);
311                 return;
312         }
313
314         FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
315                               q_button_state(e->button()));
316         view_.view()->workAreaDispatch(cmd);
317 }
318
319
320 void QWorkArea::mouseReleaseEvent(QMouseEvent * e)
321 {
322         if (synthetic_mouse_event_.timeout.running())
323                 synthetic_mouse_event_.timeout.stop();
324
325         FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
326                               q_button_state(e->button()));
327         view_.view()->workAreaDispatch(cmd);
328 }
329
330
331 void QWorkArea::mouseMoveEvent(QMouseEvent * e)
332 {
333         FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
334                               q_motion_state(e->state()));
335
336         // If we're above or below the work area...
337         if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
338                 // Make sure only a synthetic event can cause a page scroll,
339                 // so they come at a steady rate:
340                 if (e->y() <= 20)
341                         // _Force_ a scroll up:
342                         cmd.y = -40;
343                 else
344                         cmd.y = viewport()->height();
345                 // Store the event, to be handled when the timeout expires.
346                 synthetic_mouse_event_.cmd = cmd;
347
348                 if (synthetic_mouse_event_.timeout.running())
349                         // Discard the event. Note that it _may_ be handled
350                         // when the timeout expires if
351                         // synthetic_mouse_event_.cmd has not been overwritten.
352                         // Ie, when the timeout expires, we handle the
353                         // most recent event but discard all others that
354                         // occurred after the one used to start the timeout
355                         // in the first place.
356                         return;
357                 else {
358                         synthetic_mouse_event_.restart_timeout = true;
359                         synthetic_mouse_event_.timeout.start();
360                         // Fall through to handle this event...
361                 }
362
363         } else if (synthetic_mouse_event_.timeout.running()) {
364                 // Store the event, to be possibly handled when the timeout
365                 // expires.
366                 // Once the timeout has expired, normal control is returned
367                 // to mouseMoveEvent (restart_timeout = false).
368                 // This results in a much smoother 'feel' when moving the
369                 // mouse back into the work area.
370                 synthetic_mouse_event_.cmd = cmd;
371                 synthetic_mouse_event_.restart_timeout = false;
372                 return;
373         }
374
375         // Has anything changed on-screen since the last QMouseEvent
376         // was received?
377         double const scrollbar_value = verticalScrollBar()->value();
378         if (e->x() != synthetic_mouse_event_.x_old ||
379             e->y() != synthetic_mouse_event_.y_old ||
380             scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
381                 // Yes it has. Store the params used to check this.
382                 synthetic_mouse_event_.x_old = e->x();
383                 synthetic_mouse_event_.y_old = e->y();
384                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
385
386                 // ... and dispatch the event to the LyX core.
387                 view_.view()->workAreaDispatch(cmd);
388         }
389 }
390
391
392 void QWorkArea::wheelEvent(QWheelEvent * e)
393 {
394         // Wheel rotation by one notch results in a delta() of 120 (see
395         // documentation of QWheelEvent)
396         int const lines = QApplication::wheelScrollLines() * e->delta() / 120;
397         verticalScrollBar()->setValue(verticalScrollBar()->value() -
398                         lines *  verticalScrollBar()->lineStep());
399         adjustViewWithScrollBar();
400 }
401
402
403 void QWorkArea::generateSyntheticMouseEvent()
404 {
405         // Set things off to generate the _next_ 'pseudo' event.
406         if (synthetic_mouse_event_.restart_timeout)
407                 synthetic_mouse_event_.timeout.start();
408
409         // Has anything changed on-screen since the last timeout signal
410         // was received?
411         double const scrollbar_value = verticalScrollBar()->value();
412         if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
413                 // Yes it has. Store the params used to check this.
414                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
415
416                 // ... and dispatch the event to the LyX core.
417                 view_.view()->workAreaDispatch(synthetic_mouse_event_.cmd);
418         }
419 }
420
421 void QWorkArea::keyPressEvent(QKeyEvent * e)
422 {
423         lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
424                 << " count=" << e->count()
425                 << " text=" << (const char *) e->text()
426                 << " isAutoRepeat=" << e->isAutoRepeat()
427                 << " key=" << e->key()
428                 << endl;
429
430 //      keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
431
432     boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
433     sym->set(e);
434     view_.view()->workAreaKeyPress(sym, q_key_state(e->state()));
435
436 }
437
438 // This is not used for now...
439 void QWorkArea::keyeventTimeout()
440 {
441         bool handle_autos = true;
442
443         while (!keyeventQueue_.empty()) {
444                 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
445
446                 // We never handle more than one auto repeated
447                 // char in a list of queued up events.
448                 if (!handle_autos && ev->isAutoRepeat()) {
449                         keyeventQueue_.pop();
450                         continue;
451                 }
452
453                 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
454                 sym->set(ev.get());
455
456                 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
457                                    << " count=" << ev->count()
458                                    << " text=" << (const char *) ev->text()
459                                    << " isAutoRepeat=" << ev->isAutoRepeat()
460                                    << " key=" << ev->key()
461                                    << endl;
462
463                 view_.view()->workAreaKeyPress(sym, q_key_state(ev->state()));
464                 keyeventQueue_.pop();
465
466                 handle_autos = false;
467         }
468
469         // Restart the timer.
470         step_timer_.start(25, true);
471 }
472
473
474 void QWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
475 {
476         dc_event_ = double_click(e);
477
478         if (!dc_event_.active)
479                 return;
480
481         dc_event_.active = false;
482
483         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
484                 dc_event_.x, dc_event_.y,
485                 q_button_state(dc_event_.state));
486         view_.view()->workAreaDispatch(cmd);
487 }
488
489
490 void QWorkArea::resizeEvent(QResizeEvent * resizeEvent)
491 {
492         workWidth_ = viewport()->width();
493         workHeight_ = viewport()->height();
494
495         verticalScrollBar()->setPageStep(viewport()->height());
496
497 //      screen_device_ = QPixmap(viewport()->width(), viewport()->height());
498 //      paint_device_ = QImage(viewport()->width(), viewport()->height(), QImage::Format_RGB32);
499         paint_device_ = QPixmap(viewport()->width(), viewport()->height());
500
501         view_.view()->workAreaResize();
502
503         /*
504         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
505                 << "\n QWidget width\t" << this->QWidget::width()
506                 << "\n QWidget height\t" << this->QWidget::height()
507                 << "\n viewport width\t" << viewport()->width()
508                 << "\n viewport height\t" << viewport()->height()
509                 << "\n QResizeEvent rect left\t" << rect().left()
510                 << "\n QResizeEvent rect right\t" << rect().right()
511                 << endl;
512                 */
513 }
514
515 void QWorkArea::update(int x, int y, int w, int h)
516 {
517         //screen_device_.fromImage(paint_device_);
518         //QPainter q(&screen_device_);
519         //q.drawImage(x, y, paint_device_.copy(x, y, w, h));
520
521         viewport()->update(x, y, w, h);
522 }
523
524 void QWorkArea::paintEvent(QPaintEvent * e)
525 {
526         /*
527         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
528                 << "\n QWidget width\t" << this->width()
529                 << "\n QWidget height\t" << this->height()
530                 << "\n viewport width\t" << viewport()->width()
531                 << "\n viewport height\t" << viewport()->height()
532                 << "\n pixmap width\t" << pixmap_->width()
533                 << "\n pixmap height\t" << pixmap_->height()
534                 << "\n QPaintEvent x\t" << e->rect().x()
535                 << "\n QPaintEvent y\t" << e->rect().y()
536                 << "\n QPaintEvent w\t" << e->rect().width()
537                 << "\n QPaintEvent h\t" << e->rect().height()
538                 << endl;
539         */
540         QPainter q(viewport());
541         q.drawPixmap(e->rect(), paint_device_, e->rect());
542
543         if (show_vcursor_)
544                 q.drawPixmap(cursor_x_, cursor_y_, vcursor_);
545
546         if (show_hcursor_)
547                 q.drawPixmap(cursor_x_, cursor_y_ + cursor_h_ - 1, hcursor_);
548 }
549
550
551 QPixmap QWorkArea::copyScreen(int x, int y, int w, int h) const
552 {
553         return paint_device_.copy(x, y, w, h);
554 }
555
556 void QWorkArea::drawScreen(int x, int y, QPixmap pixmap)
557 {
558         QPainter q(&paint_device_);
559         q.drawPixmap(x, y, pixmap);
560         viewport()->update(x, y, pixmap.width(), pixmap.height());
561 }
562
563 ///////////////////////////////////////////////////////////////
564 // LyXSreen overloaded methods:
565
566 WorkArea & QWorkArea::workarea()
567 {
568 //      return static_cast<QWorkArea &> (*this);
569         return *this;
570 }
571
572
573 void QWorkArea::expose(int x, int y, int w, int h)
574 {
575 //      lyxerr[Debug::GUI] << "expose " << w << 'x' << h
576 //              << '+' << x << '+' << y << std::endl;
577
578         update(x, y, w, h);
579 }
580
581
582 void QWorkArea::showCursor(int x, int y, int h, Cursor_Shape shape)
583 {
584         if (!qApp->focusWidget())
585                 return;
586
587         show_vcursor_ = true;
588
589         QColor const & required_color = lcolorcache.get(LColor::cursor);
590
591         if (x==cursor_x_ && y==cursor_y_ && h==cursor_h_
592                 && cursor_color_ == required_color
593                 && cursor_shape_ == shape) {
594                 show_hcursor_ = lshape_cursor_;
595                 viewport()->update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
596                 return;
597         }
598
599         // Cache the dimensions of the cursor.
600         cursor_x_ = x;
601         cursor_y_ = y;
602         cursor_h_ = h;
603         cursor_color_ = required_color;
604         cursor_shape_ = shape;
605
606         switch (cursor_shape_) {
607         case BAR_SHAPE:
608                 // FIXME the cursor width shouldn't be hard-coded!
609                 cursor_w_ = 2;
610                 lshape_cursor_ = false;
611                 break;
612         case L_SHAPE:
613                 cursor_w_ = cursor_h_ / 3;
614                 lshape_cursor_ = true;
615                 break;
616         case REVERSED_L_SHAPE:
617                 cursor_w_ = cursor_h_ / 3;
618                 cursor_x_ -= cursor_w_ - 1;
619                 lshape_cursor_ = true;
620                 break;
621         }
622
623         // We cache two pixmaps:
624         // 1 the vertical line of the cursor.
625         // 2 the horizontal line of the L-shaped cursor (if necessary).
626         
627         // Draw the new (vertical) cursor.
628         vcursor_ = QPixmap(cursor_w_, cursor_h_);
629         vcursor_.fill(cursor_color_);
630
631         // Draw the new (horizontal) cursor if necessary.
632         if (lshape_cursor_) {
633                 hcursor_ = QPixmap(cursor_w_, 1);
634                 hcursor_.fill(cursor_color_);
635                 show_hcursor_ = true;
636         }
637
638         viewport()->update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
639 }
640
641
642 void QWorkArea::removeCursor()
643 {
644         show_vcursor_ = false;
645         show_hcursor_ = false;
646
647         viewport()->update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
648 }
649
650 ///////////////////////////////////////////////////////////////
651 ///////////////////////////////////////////////////////////////
652 // Specific stuff
653
654 ////////////////////////////////////////////////////////////////////////
655 // qt-immodule specific stuff goes here...
656
657 #if USE_INPUT_METHODS
658 // to make qt-immodule work
659
660 void QWorkArea::inputMethodEvent(QInputMethodEvent * e)
661 {
662         QString const text = e->text();
663         if (!text.isEmpty()) {
664                 int key = 0;
665                 // needed to make math superscript work on some systems
666                 // ideally, such special coding should not be necessary
667                 if (text == "^")
668                         key = Qt::Key_AsciiCircum;
669                 QKeyEvent ev(QEvent::KeyPress, key, *text.ascii(), 0, text);
670                 keyPressEvent(&ev);
671         }
672         e->accept();
673 }
674 #endif
675
676
677 ////////////////////////////////////////////////////////////////////////
678 // X11 specific stuff goes here...
679
680 #ifdef Q_WS_X11
681 bool lyxX11EventFilter(XEvent * xev)
682 {
683         switch (xev->type) {
684         case SelectionRequest:
685                 lyxerr[Debug::GUI] << "X requested selection." << endl;
686                 if (wa_ptr)
687                         wa_ptr->view().view()->selectionRequested();
688                 break;
689         case SelectionClear:
690                 lyxerr[Debug::GUI] << "Lost selection." << endl;
691                 if (wa_ptr)
692                         wa_ptr->view().view()->selectionLost();
693                 break;
694         }
695         return false;
696 }
697 #endif
698
699
700 ////////////////////////////////////////////////////////////////////////
701 // Mac OSX specific stuff goes here...
702
703 #ifdef Q_WS_MACX
704 namespace{
705 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
706  {
707         DescType returnedType;
708         Size actualSize;
709         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
710                                       typeWildCard, &returnedType, nil, 0,
711                                       &actualSize);
712         switch (err) {
713         case errAEDescNotFound:
714                 return noErr;
715         case noErr:
716                 return errAEEventNotHandled;
717         default:
718                 return err;
719         }
720  }
721 }
722
723 pascal OSErr handleOpenDocuments(const AppleEvent* inEvent,
724                                  AppleEvent* /*reply*/, long /*refCon*/)
725 {
726         QString s_arg;
727         AEDescList documentList;
728         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
729                                    &documentList);
730         if (err != noErr)
731                 return err;
732
733         err = checkAppleEventForMissingParams(*inEvent);
734         if (err == noErr) {
735                 long documentCount;
736                 err = AECountItems(&documentList, &documentCount);
737                 for (long documentIndex = 1;
738                      err == noErr && documentIndex <= documentCount;
739                      documentIndex++) {
740                         DescType returnedType;
741                         Size actualSize;
742                         AEKeyword keyword;
743                         FSRef ref;
744                         char qstr_buf[1024];
745                         err = AESizeOfNthItem(&documentList, documentIndex,
746                                               &returnedType, &actualSize);
747                         if (err == noErr) {
748                                 err = AEGetNthPtr(&documentList, documentIndex,
749                                                   typeFSRef, &keyword,
750                                                   &returnedType, (Ptr)&ref,
751                                                   sizeof(FSRef), &actualSize);
752                                 if (err == noErr) {
753                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
754                                                       1024);
755                                         s_arg=QString::fromUtf8(qstr_buf);
756                                         wa_ptr->view().view()->workAreaDispatch(
757                                                 FuncRequest(LFUN_FILE_OPEN,
758                                                             fromqstr(s_arg)));
759                                         break;
760                                 }
761                         }
762                 } // for ...
763         }
764         AEDisposeDesc(&documentList);
765         return err;
766 }
767 #endif  // Q_WS_MACX
768
769 #include "QWorkArea_moc.cpp"