]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Application.C
GUI API Cleanup step 2: merge of the "younes" branch. What's missing
[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), buffer_view_(0)
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
63 void Application::setBufferView(BufferView * buffer_view)
64 {
65         buffer_view_ = buffer_view;
66 }
67
68
69 ////////////////////////////////////////////////////////////////////////
70 // X11 specific stuff goes here...
71 #ifdef Q_WS_X11
72 bool Application::x11EventFilter(XEvent * xev)
73 {
74         switch (xev->type) {
75         case SelectionRequest:
76                 lyxerr[Debug::GUI] << "X requested selection." << endl;
77                 if (buffer_view_)
78                         buffer_view_->selectionRequested();
79                 break;
80         case SelectionClear:
81                 lyxerr[Debug::GUI] << "Lost selection." << endl;
82                 if (buffer_view_)
83                         buffer_view_->selectionLost();
84                 break;
85         }
86         return false;
87 }
88 #endif
89
90
91 ////////////////////////////////////////////////////////////////////////
92 // Mac OSX specific stuff goes here...
93
94 #ifdef Q_WS_MACX
95 namespace{
96 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
97  {
98         DescType returnedType;
99         Size actualSize;
100         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
101                                       typeWildCard, &returnedType, nil, 0,
102                                       &actualSize);
103         switch (err) {
104         case errAEDescNotFound:
105                 return noErr;
106         case noErr:
107                 return errAEEventNotHandled;
108         default:
109                 return err;
110         }
111  }
112 } // namespace
113
114 OSErr Application::handleOpenDocuments(const AppleEvent* inEvent,
115                                        AppleEvent* /*reply*/, long /*refCon*/)
116 {
117         QString s_arg;
118         AEDescList documentList;
119         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
120                                    &documentList);
121         if (err != noErr)
122                 return err;
123
124         err = checkAppleEventForMissingParams(*inEvent);
125         if (err == noErr) {
126                 long documentCount;
127                 err = AECountItems(&documentList, &documentCount);
128                 for (long documentIndex = 1;
129                      err == noErr && documentIndex <= documentCount;
130                      documentIndex++) {
131                         DescType returnedType;
132                         Size actualSize;
133                         AEKeyword keyword;
134                         FSRef ref;
135                         char qstr_buf[1024];
136                         err = AESizeOfNthItem(&documentList, documentIndex,
137                                               &returnedType, &actualSize);
138                         if (err == noErr) {
139                                 err = AEGetNthPtr(&documentList, documentIndex,
140                                                   typeFSRef, &keyword,
141                                                   &returnedType, (Ptr)&ref,
142                                                   sizeof(FSRef), &actualSize);
143                                 if (err == noErr) {
144                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
145                                                       1024);
146                                         s_arg=QString::fromUtf8(qstr_buf);
147 //                                      buffer_view_->workAreaDispatch(
148 //                                              FuncRequest(LFUN_FILE_OPEN,
149 //                                                          fromqstr(s_arg)));
150                                         break;
151                                 }
152                         }
153                 } // for ...
154         }
155         AEDisposeDesc(&documentList);
156
157         return err;
158 }
159
160 bool Application::macEventFilter(EventRef event)
161 {
162         if (GetEventClass(event) == kEventClassAppleEvent) {
163                 EventRecord eventrec;
164                 ConvertEventRefToEventRecord(event, &eventrec);
165                 AEProcessAppleEvent(&eventrec);
166
167                 return false;
168         }
169         return false;
170 }
171
172 #endif  // Q_WS_MACX
173
174 } // namespace frontend
175 } // namespace lyx