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