]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
Some cleanup
[lyx.git] / src / frontends / qt4 / GuiApplication.C
1 /**
2  * \file qt4/GuiApplication.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 <config.h>
14
15 #include "GuiApplication.h"
16
17 #include "qt_helpers.h"
18 #include "QLImage.h"
19
20 #include "graphics/LoaderQueue.h"
21
22 #include "support/lstrings.h"
23 #include "support/os.h"
24 #include "support/package.h"
25
26 #include "BufferView.h"
27 #include "lyx_main.h"
28 #include "lyxrc.h"
29 #include "debug.h"
30
31 #include <QApplication>
32 #include <QClipboard>
33 #include <QEventLoop>
34 #include <QLocale>
35 #include <QLibraryInfo>
36 #include <QTextCodec>
37 #include <QTranslator>
38 #include <QWidget>
39
40 #ifdef Q_WS_X11
41 #include <X11/Xlib.h>
42 #endif
43
44 #include <boost/bind.hpp>
45
46 using lyx::support::subst;
47
48 using std::string;
49 using std::endl;
50
51 // in QLyXKeySym.C
52 extern void initEncodings();
53
54 ///////////////////////////////////////////////////////////////
55 // You can find other X11 and MACX specific stuff
56 // at the end of this file...
57 ///////////////////////////////////////////////////////////////
58
59 namespace {
60
61 int getDPI()
62 {
63         QWidget w;
64         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
65 }
66
67 } // namespace anon
68
69
70 namespace lyx {
71 namespace frontend {
72
73 GuiApplication::GuiApplication(int & argc, char ** argv)
74         : QApplication(argc, argv), Application(argc, argv)
75 {
76 #ifdef Q_WS_X11
77         // doubleClickInterval() is 400 ms on X11 witch is just too long.
78         // On Windows and Mac OS X, the operating system's value is used.
79         // On Microsoft Windows, calling this function sets the double
80         // click interval for all applications. So we don't!
81         QApplication::setDoubleClickInterval(300);
82 #endif
83
84 #ifdef Q_WS_MACX
85         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
86                               NewAEEventHandlerUPP(handleOpenDocuments),
87                               0, false);
88 #endif
89
90         // install translation file for Qt built-in dialogs
91         // These are only installed since Qt 3.2.x
92         QTranslator qt_trans;
93         QString language_name = QString("qt_") + QLocale::system().name();
94         language_name.truncate(5);
95         if (qt_trans.load(language_name,
96                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
97         {
98                 qApp->installTranslator(&qt_trans);
99                 // even if the language calls for RtL, don't do that
100                 qApp->setLayoutDirection(Qt::LeftToRight);
101                 lyxerr[Debug::GUI]
102                         << "Successfully installed Qt translations for locale "
103                         << fromqstr(language_name) << std::endl;
104         } else
105                 lyxerr[Debug::GUI]
106                         << "Could not find  Qt translations for locale "
107                         << fromqstr(language_name) << std::endl;
108
109 /*#ifdef Q_WS_MACX
110         // These translations are meant to break Qt/Mac menu merging
111         // algorithm on some entries. It lists the menu names that
112         // should not be moved to the LyX menu
113         QTranslator aqua_trans(0);
114         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
115                                              "do_not_merge_me"));
116         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
117                                              "do_not_merge_me"));
118         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
119                                              "do_not_merge_me"));
120         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
121                                              "do_not_merge_me"));
122
123         qApp->installTranslator(&aqua_trans);
124 #endif
125 */
126         using namespace lyx::graphics;
127
128         Image::newImage = boost::bind(&QLImage::newImage);
129         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
130
131         // needs to be done before reading lyxrc
132         lyxrc.dpi = getDPI();
133
134         LoaderQueue::setPriority(10,100);
135 }
136
137
138 Clipboard& GuiApplication::clipboard()
139 {
140         return clipboard_;
141 }
142
143
144 Selection& GuiApplication::selection()
145 {
146         return selection_;
147 }
148
149
150 int const GuiApplication::exec()
151 {
152         return QApplication::exec();
153 }
154
155
156 void GuiApplication::exit(int status)
157 {
158         QApplication::exit(status);
159 }
160
161
162
163
164 ////////////////////////////////////////////////////////////////////////
165 // X11 specific stuff goes here...
166 #ifdef Q_WS_X11
167 bool GuiApplication::x11EventFilter(XEvent * xev)
168 {
169         switch (xev->type) {
170         case SelectionRequest:
171                 lyxerr[Debug::GUI] << "X requested selection." << endl;
172                 if (buffer_view_) {
173                         lyx::docstring const sel = buffer_view_->requestSelection();
174                         if (!sel.empty())
175                                 selection_.put(sel);
176                 }
177                 break;
178         case SelectionClear:
179                 lyxerr[Debug::GUI] << "Lost selection." << endl;
180                 if (buffer_view_)
181                         buffer_view_->clearSelection();
182                 break;
183         }
184         return false;
185 }
186 #endif
187
188
189 ////////////////////////////////////////////////////////////////////////
190 // Mac OSX specific stuff goes here...
191
192 #ifdef Q_WS_MACX
193 namespace{
194
195 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
196  {
197         DescType returnedType;
198         Size actualSize;
199         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
200                                       typeWildCard, &returnedType, nil, 0,
201                                       &actualSize);
202         switch (err) {
203         case errAEDescNotFound:
204                 return noErr;
205         case noErr:
206                 return errAEEventNotHandled;
207         default:
208                 return err;
209         }
210  }
211
212 } // namespace
213
214 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
215                                        AppleEvent* /*reply*/, long /*refCon*/)
216 {
217         QString s_arg;
218         AEDescList documentList;
219         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
220                                    &documentList);
221         if (err != noErr)
222                 return err;
223
224         err = checkAppleEventForMissingParams(*inEvent);
225         if (err == noErr) {
226                 long documentCount;
227                 err = AECountItems(&documentList, &documentCount);
228                 for (long documentIndex = 1;
229                      err == noErr && documentIndex <= documentCount;
230                      documentIndex++) {
231                         DescType returnedType;
232                         Size actualSize;
233                         AEKeyword keyword;
234                         FSRef ref;
235                         char qstr_buf[1024];
236                         err = AESizeOfNthItem(&documentList, documentIndex,
237                                               &returnedType, &actualSize);
238                         if (err == noErr) {
239                                 err = AEGetNthPtr(&documentList, documentIndex,
240                                                   typeFSRef, &keyword,
241                                                   &returnedType, (Ptr)&ref,
242                                                   sizeof(FSRef), &actualSize);
243                                 if (err == noErr) {
244                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
245                                                       1024);
246                                         s_arg=QString::fromUtf8(qstr_buf);
247 //                                      buffer_view_->workAreaDispatch(
248 //                                              FuncRequest(LFUN_FILE_OPEN,
249 //                                                          fromqstr(s_arg)));
250                                         break;
251                                 }
252                         }
253                 } // for ...
254         }
255         AEDisposeDesc(&documentList);
256
257         return err;
258 }
259
260 bool GuiApplication::macEventFilter(EventRef event)
261 {
262         if (GetEventClass(event) == kEventClassAppleEvent) {
263                 EventRecord eventrec;
264                 ConvertEventRefToEventRecord(event, &eventrec);
265                 AEProcessAppleEvent(&eventrec);
266
267                 return false;
268         }
269         return false;
270 }
271
272 #endif  // Q_WS_MACX
273
274 } // namespace frontend
275 } // namespace lyx