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