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