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