]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
enable Font cache only for MacOSX and inline width() for other platform.
[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 string const GuiApplication::romanFontName()
163 {
164         QFont font;
165         font.setStyleHint(QFont::Serif);
166         font.setFamily("serif");
167
168         return fromqstr(QFontInfo(font).family());
169 }
170
171
172 string const GuiApplication::sansFontName()
173 {
174         QFont font;
175         font.setStyleHint(QFont::SansSerif);
176         font.setFamily("sans");
177
178         return fromqstr(QFontInfo(font).family());
179 }
180
181
182 string const GuiApplication::typewriterFontName()
183 {
184         QFont font;
185         font.setStyleHint(QFont::TypeWriter);
186         font.setFamily("monospace");
187
188         return fromqstr(QFontInfo(font).family());
189 }
190
191
192 ////////////////////////////////////////////////////////////////////////
193 // X11 specific stuff goes here...
194 #ifdef Q_WS_X11
195 bool GuiApplication::x11EventFilter(XEvent * xev)
196 {
197         switch (xev->type) {
198         case SelectionRequest:
199                 lyxerr[Debug::GUI] << "X requested selection." << endl;
200                 if (buffer_view_) {
201                         lyx::docstring const sel = buffer_view_->requestSelection();
202                         if (!sel.empty())
203                                 selection_.put(sel);
204                 }
205                 break;
206         case SelectionClear:
207                 lyxerr[Debug::GUI] << "Lost selection." << endl;
208                 if (buffer_view_)
209                         buffer_view_->clearSelection();
210                 break;
211         }
212         return false;
213 }
214 #endif
215
216
217 ////////////////////////////////////////////////////////////////////////
218 // Mac OSX specific stuff goes here...
219
220 #ifdef Q_WS_MACX
221 namespace{
222
223 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
224  {
225         DescType returnedType;
226         Size actualSize;
227         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
228                                       typeWildCard, &returnedType, nil, 0,
229                                       &actualSize);
230         switch (err) {
231         case errAEDescNotFound:
232                 return noErr;
233         case noErr:
234                 return errAEEventNotHandled;
235         default:
236                 return err;
237         }
238  }
239
240 } // namespace
241
242 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
243                                        AppleEvent* /*reply*/, long /*refCon*/)
244 {
245         QString s_arg;
246         AEDescList documentList;
247         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
248                                    &documentList);
249         if (err != noErr)
250                 return err;
251
252         err = checkAppleEventForMissingParams(*inEvent);
253         if (err == noErr) {
254                 long documentCount;
255                 err = AECountItems(&documentList, &documentCount);
256                 for (long documentIndex = 1;
257                      err == noErr && documentIndex <= documentCount;
258                      documentIndex++) {
259                         DescType returnedType;
260                         Size actualSize;
261                         AEKeyword keyword;
262                         FSRef ref;
263                         char qstr_buf[1024];
264                         err = AESizeOfNthItem(&documentList, documentIndex,
265                                               &returnedType, &actualSize);
266                         if (err == noErr) {
267                                 err = AEGetNthPtr(&documentList, documentIndex,
268                                                   typeFSRef, &keyword,
269                                                   &returnedType, (Ptr)&ref,
270                                                   sizeof(FSRef), &actualSize);
271                                 if (err == noErr) {
272                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
273                                                       1024);
274                                         s_arg=QString::fromUtf8(qstr_buf);
275 //                                      buffer_view_->workAreaDispatch(
276 //                                              FuncRequest(LFUN_FILE_OPEN,
277 //                                                          fromqstr(s_arg)));
278                                         break;
279                                 }
280                         }
281                 } // for ...
282         }
283         AEDisposeDesc(&documentList);
284
285         return err;
286 }
287
288 bool GuiApplication::macEventFilter(EventRef event)
289 {
290         if (GetEventClass(event) == kEventClassAppleEvent) {
291                 EventRecord eventrec;
292                 ConvertEventRefToEventRecord(event, &eventrec);
293                 AEProcessAppleEvent(&eventrec);
294
295                 return false;
296         }
297         return false;
298 }
299
300 #endif  // Q_WS_MACX
301
302 } // namespace frontend
303 } // namespace lyx