]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiApplication.C
Replace crash on startup with a memory leak.
[features.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 #include "socket_callback.h"
20
21 #include "graphics/LoaderQueue.h"
22
23 #include "support/lstrings.h"
24 #include "support/os.h"
25 #include "support/package.h"
26
27 #include "BufferView.h"
28 #include "Color.h"
29 #include "debug.h"
30 #include "lyx_main.h"
31 #include "lyxrc.h"
32
33 #include <QApplication>
34 #include <QClipboard>
35 #include <QEventLoop>
36 #include <QLocale>
37 #include <QLibraryInfo>
38 #include <QTextCodec>
39 #include <QTranslator>
40 #include <QWidget>
41
42 #ifdef Q_WS_X11
43 #include <X11/Xlib.h>
44 #endif
45
46 #include <boost/bind.hpp>
47
48 using lyx::support::subst;
49
50 using std::string;
51 using std::endl;
52
53 // in QLyXKeySym.C
54 extern void initEncodings();
55
56 ///////////////////////////////////////////////////////////////
57 // You can find other X11 and MACX specific stuff
58 // at the end of this file...
59 ///////////////////////////////////////////////////////////////
60
61 namespace {
62
63 int getDPI()
64 {
65         QWidget w;
66         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
67 }
68
69 } // namespace anon
70
71
72 lyx::frontend::GuiApplication * guiApp;
73
74 namespace lyx {
75
76 lyx::frontend::Application * createApplication(int & argc, char * argv[])
77 {
78         // FIXME: it would be great if we could just do:
79         //return new lyx::frontend::GuiApplication(argc, argv);
80
81 #if defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN)
82         static lyx::frontend::GuiApplication app(argc, argv);
83         return &app;
84 #else
85         return new lyx::frontend::GuiApplication(argc, argv);
86 #endif
87 }
88
89
90 namespace frontend {
91
92 GuiApplication::GuiApplication(int & argc, char ** argv)
93         : QApplication(argc, argv), Application(argc, argv)
94 {
95 #ifdef Q_WS_X11
96         // doubleClickInterval() is 400 ms on X11 witch is just too long.
97         // On Windows and Mac OS X, the operating system's value is used.
98         // On Microsoft Windows, calling this function sets the double
99         // click interval for all applications. So we don't!
100         QApplication::setDoubleClickInterval(300);
101 #endif
102
103 #ifdef Q_WS_MACX
104         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
105                               NewAEEventHandlerUPP(handleOpenDocuments),
106                               0, false);
107 #endif
108
109         // install translation file for Qt built-in dialogs
110         // These are only installed since Qt 3.2.x
111         QTranslator qt_trans;
112         QString language_name = QString("qt_") + QLocale::system().name();
113         language_name.truncate(5);
114         if (qt_trans.load(language_name,
115                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
116         {
117                 qApp->installTranslator(&qt_trans);
118                 // even if the language calls for RtL, don't do that
119                 qApp->setLayoutDirection(Qt::LeftToRight);
120                 lyxerr[Debug::GUI]
121                         << "Successfully installed Qt translations for locale "
122                         << fromqstr(language_name) << std::endl;
123         } else
124                 lyxerr[Debug::GUI]
125                         << "Could not find  Qt translations for locale "
126                         << fromqstr(language_name) << std::endl;
127
128 /*#ifdef Q_WS_MACX
129         // These translations are meant to break Qt/Mac menu merging
130         // algorithm on some entries. It lists the menu names that
131         // should not be moved to the LyX menu
132         QTranslator aqua_trans(0);
133         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
134                                              "do_not_merge_me"));
135         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
136                                              "do_not_merge_me"));
137         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
138                                              "do_not_merge_me"));
139         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
140                                              "do_not_merge_me"));
141
142         qApp->installTranslator(&aqua_trans);
143 #endif
144 */
145         using namespace lyx::graphics;
146
147         Image::newImage = boost::bind(&QLImage::newImage);
148         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
149
150         // needs to be done before reading lyxrc
151         lyxrc.dpi = getDPI();
152
153         LoaderQueue::setPriority(10,100);
154
155         guiApp = this;
156 }
157
158
159 Clipboard& GuiApplication::clipboard()
160 {
161         return clipboard_;
162 }
163
164
165 Selection& GuiApplication::selection()
166 {
167         return selection_;
168 }
169
170
171 int const GuiApplication::exec()
172 {
173         return QApplication::exec();
174 }
175
176
177 void GuiApplication::exit(int status)
178 {
179         QApplication::exit(status);
180 }
181
182
183 string const GuiApplication::romanFontName()
184 {
185         QFont font;
186         font.setStyleHint(QFont::Serif);
187         font.setFamily("serif");
188
189         return fromqstr(QFontInfo(font).family());
190 }
191
192
193 string const GuiApplication::sansFontName()
194 {
195         QFont font;
196         font.setStyleHint(QFont::SansSerif);
197         font.setFamily("sans");
198
199         return fromqstr(QFontInfo(font).family());
200 }
201
202
203 string const GuiApplication::typewriterFontName()
204 {
205         QFont font;
206         font.setStyleHint(QFont::TypeWriter);
207         font.setFamily("monospace");
208
209         return fromqstr(QFontInfo(font).family());
210 }
211
212
213 void GuiApplication::syncEvents()
214 {
215         // This is the ONLY place where processEvents may be called.
216         // During screen update/ redraw, this method is disabled to
217         // prevent keyboard events being handed to the LyX core, where
218         // they could cause re-entrant calls to screen update.
219         processEvents(QEventLoop::ExcludeUserInputEvents);
220 }
221
222
223 bool GuiApplication::getRgbColor(LColor_color col,
224                                                                  lyx::RGBColor & rgbcol)
225 {
226         QColor const & qcol = color_cache_.get(col);
227         if (!qcol.isValid()) {
228                 rgbcol.r = 0;
229                 rgbcol.g = 0;
230                 rgbcol.b = 0;
231                 return false;
232         }
233         rgbcol.r = qcol.red();
234         rgbcol.g = qcol.green();
235         rgbcol.b = qcol.blue();
236         return true;
237 }
238
239
240 string const GuiApplication::hexName(LColor_color col)
241 {
242         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
243 }
244
245
246 void GuiApplication::updateColor(LColor_color)
247 {
248         // FIXME: Bleh, can't we just clear them all at once ?
249         color_cache_.clear();
250 }
251
252
253 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
254 {
255         socket_callbacks_[fd] =
256                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
257 }
258
259
260 void GuiApplication::unregisterSocketCallback(int fd)
261 {
262         socket_callbacks_.erase(fd);
263 }
264
265 ////////////////////////////////////////////////////////////////////////
266 // X11 specific stuff goes here...
267 #ifdef Q_WS_X11
268 bool GuiApplication::x11EventFilter(XEvent * xev)
269 {
270         switch (xev->type) {
271         case SelectionRequest:
272                 lyxerr[Debug::GUI] << "X requested selection." << endl;
273                 if (buffer_view_) {
274                         lyx::docstring const sel = buffer_view_->requestSelection();
275                         if (!sel.empty())
276                                 selection_.put(sel);
277                 }
278                 break;
279         case SelectionClear:
280                 lyxerr[Debug::GUI] << "Lost selection." << endl;
281                 if (buffer_view_)
282                         buffer_view_->clearSelection();
283                 break;
284         }
285         return false;
286 }
287 #endif
288
289
290 ////////////////////////////////////////////////////////////////////////
291 // Mac OSX specific stuff goes here...
292
293 #ifdef Q_WS_MACX
294 namespace{
295
296 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
297  {
298         DescType returnedType;
299         Size actualSize;
300         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
301                                       typeWildCard, &returnedType, nil, 0,
302                                       &actualSize);
303         switch (err) {
304         case errAEDescNotFound:
305                 return noErr;
306         case noErr:
307                 return errAEEventNotHandled;
308         default:
309                 return err;
310         }
311  }
312
313 } // namespace
314
315 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
316                                        AppleEvent* /*reply*/, long /*refCon*/)
317 {
318         QString s_arg;
319         AEDescList documentList;
320         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
321                                    &documentList);
322         if (err != noErr)
323                 return err;
324
325         err = checkAppleEventForMissingParams(*inEvent);
326         if (err == noErr) {
327                 long documentCount;
328                 err = AECountItems(&documentList, &documentCount);
329                 for (long documentIndex = 1;
330                      err == noErr && documentIndex <= documentCount;
331                      documentIndex++) {
332                         DescType returnedType;
333                         Size actualSize;
334                         AEKeyword keyword;
335                         FSRef ref;
336                         char qstr_buf[1024];
337                         err = AESizeOfNthItem(&documentList, documentIndex,
338                                               &returnedType, &actualSize);
339                         if (err == noErr) {
340                                 err = AEGetNthPtr(&documentList, documentIndex,
341                                                   typeFSRef, &keyword,
342                                                   &returnedType, (Ptr)&ref,
343                                                   sizeof(FSRef), &actualSize);
344                                 if (err == noErr) {
345                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
346                                                       1024);
347                                         s_arg=QString::fromUtf8(qstr_buf);
348 //                                      buffer_view_->workAreaDispatch(
349 //                                              FuncRequest(LFUN_FILE_OPEN,
350 //                                                          fromqstr(s_arg)));
351                                         break;
352                                 }
353                         }
354                 } // for ...
355         }
356         AEDisposeDesc(&documentList);
357
358         return err;
359 }
360
361 bool GuiApplication::macEventFilter(EventRef event)
362 {
363         if (GetEventClass(event) == kEventClassAppleEvent) {
364                 EventRecord eventrec;
365                 ConvertEventRefToEventRecord(event, &eventrec);
366                 AEProcessAppleEvent(&eventrec);
367
368                 return false;
369         }
370         return false;
371 }
372
373 #endif  // Q_WS_MACX
374
375 } // namespace frontend
376 } // namespace lyx