]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Application.C
* [qt3, xforms, gtk]/GuiImplementation::newWorkArea: return 0
[lyx.git] / src / frontends / qt4 / Application.C
1 /**
2  * \file qt4/Application.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include "GuiWorkArea.h"
14 #include "Application.h"
15
16 #include "qt_helpers.h"
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "support/lstrings.h"
20
21 #include <QApplication>
22 #include <QEventLoop>
23 #include <QTranslator>
24 #include <QTextCodec>
25 #include <QClipboard>
26
27 #ifdef Q_WS_X11
28 #include <X11/Xlib.h>
29 #endif
30
31 using lyx::support::subst;
32
33 using std::string;
34 using std::endl;
35
36 ///////////////////////////////////////////////////////////////
37 // You can find other X11 and MACX specific stuff
38 // at the end of this file...
39 ///////////////////////////////////////////////////////////////
40
41 namespace lyx {
42 namespace frontend {
43
44 Application::Application(int & argc, char ** argv)
45         : QApplication(argc, argv), work_area_(NULL)
46 {
47 #ifdef Q_WS_X11
48         // doubleClickInterval() is 400 ms on X11 witch is just too long.
49         // On Windows and Mac OS X, the operating system's value is used.
50         // On Microsoft Windows, calling this function sets the double
51         // click interval for all applications. So we don't!
52         QApplication::setDoubleClickInterval(300);
53 #endif
54
55 #ifdef Q_WS_MACX
56         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
57                               NewAEEventHandlerUPP(handleOpenDocuments),
58                               0, false);
59 #endif
60 }
61
62 void Application::connect(GuiWorkArea * work_area)
63 {
64         work_area_ = work_area;
65 }
66
67
68 ////////////////////////////////////////////////////////////////////////
69 // X11 specific stuff goes here...
70 #ifdef Q_WS_X11
71 bool Application::x11EventFilter(XEvent * xev)
72 {
73         switch (xev->type) {
74         case SelectionRequest:
75                 lyxerr[Debug::GUI] << "X requested selection." << endl;
76                 if (work_area_)
77                         work_area_->view().view()->selectionRequested();
78                 break;
79         case SelectionClear:
80                 lyxerr[Debug::GUI] << "Lost selection." << endl;
81                 if (work_area_)
82                         work_area_->view().view()->selectionLost();
83                 break;
84         }
85         return false;
86 }
87 #endif
88
89
90 ////////////////////////////////////////////////////////////////////////
91 // Mac OSX specific stuff goes here...
92
93 #ifdef Q_WS_MACX
94 namespace{
95 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
96  {
97         DescType returnedType;
98         Size actualSize;
99         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
100                                       typeWildCard, &returnedType, nil, 0,
101                                       &actualSize);
102         switch (err) {
103         case errAEDescNotFound:
104                 return noErr;
105         case noErr:
106                 return errAEEventNotHandled;
107         default:
108                 return err;
109         }
110  }
111 } // namespace
112
113 OSErr Application::handleOpenDocuments(const AppleEvent* inEvent,
114                                        AppleEvent* /*reply*/, long /*refCon*/)
115 {
116         QString s_arg;
117         AEDescList documentList;
118         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
119                                    &documentList);
120         if (err != noErr)
121                 return err;
122
123         err = checkAppleEventForMissingParams(*inEvent);
124         if (err == noErr) {
125                 long documentCount;
126                 err = AECountItems(&documentList, &documentCount);
127                 for (long documentIndex = 1;
128                      err == noErr && documentIndex <= documentCount;
129                      documentIndex++) {
130                         DescType returnedType;
131                         Size actualSize;
132                         AEKeyword keyword;
133                         FSRef ref;
134                         char qstr_buf[1024];
135                         err = AESizeOfNthItem(&documentList, documentIndex,
136                                               &returnedType, &actualSize);
137                         if (err == noErr) {
138                                 err = AEGetNthPtr(&documentList, documentIndex,
139                                                   typeFSRef, &keyword,
140                                                   &returnedType, (Ptr)&ref,
141                                                   sizeof(FSRef), &actualSize);
142                                 if (err == noErr) {
143                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
144                                                       1024);
145                                         s_arg=QString::fromUtf8(qstr_buf);
146                                         work_area_->view().view()->workAreaDispatch(
147                                                 FuncRequest(LFUN_FILE_OPEN,
148                                                             fromqstr(s_arg)));
149                                         break;
150                                 }
151                         }
152                 } // for ...
153         }
154         AEDisposeDesc(&documentList);
155
156         return err;
157 }
158
159 bool Application::macEventFilter(EventRef event)
160 {
161         if (GetEventClass(event) == kEventClassAppleEvent) {
162                 EventRecord eventrec;
163                 ConvertEventRefToEventRecord(event, &eventrec);
164                 AEProcessAppleEvent(&eventrec);
165
166                 return false;
167         }
168         return false;
169 }
170
171 #endif  // Q_WS_MACX
172
173 } // namespace frontend
174 } // namespace lyx