]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
hopefully fix tex2lyx linking.
[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 #include "socket_callback.h"
20
21 #include "frontends/LyXView.h"
22
23 #include "graphics/LoaderQueue.h"
24
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/package.h"
28
29 #include "BufferView.h"
30 #include "Color.h"
31 #include "debug.h"
32 #include "funcrequest.h"
33 #include "lyx_main.h"
34 #include "lyxfunc.h"
35 #include "lyxrc.h"
36
37 #include <QApplication>
38 #include <QClipboard>
39 #include <QEventLoop>
40 #include <QLocale>
41 #include <QLibraryInfo>
42 #include <QTextCodec>
43 #include <QTranslator>
44 #include <QWidget>
45
46 #ifdef Q_WS_X11
47 #include <X11/Xlib.h>
48 #endif
49
50 #include <boost/bind.hpp>
51
52 using std::string;
53 using std::endl;
54
55 // in QLyXKeySym.C
56 extern void initEncodings();
57
58 ///////////////////////////////////////////////////////////////
59 // You can find other X11 and MACX specific stuff
60 // at the end of this file...
61 ///////////////////////////////////////////////////////////////
62
63 namespace {
64
65 int getDPI()
66 {
67         QWidget w;
68         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
69 }
70
71 } // namespace anon
72
73
74 namespace lyx {
75
76 frontend::Application * createApplication(int & argc, char * argv[])
77 {
78         return new frontend::GuiApplication(argc, argv);
79 }
80
81
82 namespace frontend {
83
84 GuiApplication * guiApp;
85
86
87 GuiApplication::~GuiApplication()
88 {
89         socket_callbacks_.clear();
90 }
91
92
93 GuiApplication::GuiApplication(int & argc, char ** argv)
94         : QApplication(argc, argv), Application(argc, argv)
95 {
96 #ifdef Q_WS_X11
97         // doubleClickInterval() is 400 ms on X11 which is just too long.
98         // On Windows and Mac OS X, the operating system's value is used.
99         // On Microsoft Windows, calling this function sets the double
100         // click interval for all applications. So we don't!
101         QApplication::setDoubleClickInterval(300);
102 #endif
103
104 #ifdef Q_WS_MACX
105         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
106                               NewAEEventHandlerUPP(handleOpenDocuments),
107                               0, false);
108 #endif
109
110         // install translation file for Qt built-in dialogs
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         setQuitOnLastWindowClosed(false);
156         QObject::connect(this, SIGNAL(lastWindowClosed()),
157                 this, SLOT(quitLyX()));
158
159         guiApp = this;
160 }
161
162
163 void GuiApplication::quitLyX()
164 {
165         theLyXFunc().setLyXView(0);
166
167         // trigger LFUN_LYX_QUIT instead of QApplication::quit() directly
168         // since LFUN_LYX_QUIT may have more cleanup stuff
169         dispatch(FuncRequest(LFUN_LYX_QUIT, "force"));
170 }
171
172
173 Clipboard& GuiApplication::clipboard()
174 {
175         return clipboard_;
176 }
177
178
179 Selection& GuiApplication::selection()
180 {
181         return selection_;
182 }
183
184
185 int const GuiApplication::exec()
186 {
187         return QApplication::exec();
188 }
189
190
191 void GuiApplication::exit(int status)
192 {
193         QApplication::exit(status);
194 }
195
196
197 string const GuiApplication::romanFontName()
198 {
199         QFont font;
200         font.setKerning(false);
201         font.setStyleHint(QFont::Serif);
202         font.setFamily("serif");
203
204         return fromqstr(QFontInfo(font).family());
205 }
206
207
208 string const GuiApplication::sansFontName()
209 {
210         QFont font;
211         font.setKerning(false);
212         font.setStyleHint(QFont::SansSerif);
213         font.setFamily("sans");
214
215         return fromqstr(QFontInfo(font).family());
216 }
217
218
219 string const GuiApplication::typewriterFontName()
220 {
221         QFont font;
222         font.setKerning(false);
223         font.setStyleHint(QFont::TypeWriter);
224         font.setFamily("monospace");
225
226         return fromqstr(QFontInfo(font).family());
227 }
228
229
230 void GuiApplication::syncEvents()
231 {
232         // This is the ONLY place where processEvents may be called.
233         // During screen update/ redraw, this method is disabled to
234         // prevent keyboard events being handed to the LyX core, where
235         // they could cause re-entrant calls to screen update.
236         processEvents(QEventLoop::ExcludeUserInputEvents);
237 }
238
239
240 bool GuiApplication::getRgbColor(LColor_color col,
241         lyx::RGBColor & rgbcol)
242 {
243         QColor const & qcol = color_cache_.get(col);
244         if (!qcol.isValid()) {
245                 rgbcol.r = 0;
246                 rgbcol.g = 0;
247                 rgbcol.b = 0;
248                 return false;
249         }
250         rgbcol.r = qcol.red();
251         rgbcol.g = qcol.green();
252         rgbcol.b = qcol.blue();
253         return true;
254 }
255
256
257 string const GuiApplication::hexName(LColor_color col)
258 {
259         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
260 }
261
262
263 void GuiApplication::updateColor(LColor_color)
264 {
265         // FIXME: Bleh, can't we just clear them all at once ?
266         color_cache_.clear();
267 }
268
269
270 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
271 {
272         socket_callbacks_[fd] =
273                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
274 }
275
276
277 void GuiApplication::unregisterSocketCallback(int fd)
278 {
279         socket_callbacks_.erase(fd);
280 }
281
282 ////////////////////////////////////////////////////////////////////////
283 // X11 specific stuff goes here...
284 #ifdef Q_WS_X11
285 bool GuiApplication::x11EventFilter(XEvent * xev)
286 {
287         BufferView * bv = currentView().view();
288
289         switch (xev->type) {
290         case SelectionRequest:
291                 lyxerr[Debug::GUI] << "X requested selection." << endl;
292                 if (bv) {
293                         lyx::docstring const sel = bv->requestSelection();
294                         if (!sel.empty())
295                                 selection_.put(sel);
296                 }
297                 break;
298         case SelectionClear:
299                 lyxerr[Debug::GUI] << "Lost selection." << endl;
300                 if (bv)
301                         bv->clearSelection();
302                 break;
303         }
304         return false;
305 }
306 #endif
307
308
309 ////////////////////////////////////////////////////////////////////////
310 // Mac OSX specific stuff goes here...
311
312 #ifdef Q_WS_MACX
313 namespace{
314
315 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
316  {
317         DescType returnedType;
318         Size actualSize;
319         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
320                                       typeWildCard, &returnedType, nil, 0,
321                                       &actualSize);
322         switch (err) {
323         case errAEDescNotFound:
324                 return noErr;
325         case noErr:
326                 return errAEEventNotHandled;
327         default:
328                 return err;
329         }
330  }
331
332 } // namespace
333
334 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
335                                        AppleEvent* /*reply*/, long /*refCon*/)
336 {
337         QString s_arg;
338         AEDescList documentList;
339         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
340                                    &documentList);
341         if (err != noErr)
342                 return err;
343
344         err = checkAppleEventForMissingParams(*inEvent);
345         if (err == noErr) {
346                 long documentCount;
347                 err = AECountItems(&documentList, &documentCount);
348                 for (long documentIndex = 1;
349                      err == noErr && documentIndex <= documentCount;
350                      documentIndex++) {
351                         DescType returnedType;
352                         Size actualSize;
353                         AEKeyword keyword;
354                         FSRef ref;
355                         char qstr_buf[1024];
356                         err = AESizeOfNthItem(&documentList, documentIndex,
357                                               &returnedType, &actualSize);
358                         if (err == noErr) {
359                                 err = AEGetNthPtr(&documentList, documentIndex,
360                                                   typeFSRef, &keyword,
361                                                   &returnedType, (Ptr)&ref,
362                                                   sizeof(FSRef), &actualSize);
363                                 if (err == noErr) {
364                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
365                                                       1024);
366                                         s_arg=QString::fromUtf8(qstr_buf);
367 //                                      bv->workAreaDispatch(
368 //                                              FuncRequest(LFUN_FILE_OPEN,
369 //                                                          fromqstr(s_arg)));
370                                         break;
371                                 }
372                         }
373                 } // for ...
374         }
375         AEDisposeDesc(&documentList);
376
377         return err;
378 }
379
380 bool GuiApplication::macEventFilter(EventRef event)
381 {
382         if (GetEventClass(event) == kEventClassAppleEvent) {
383                 EventRecord eventrec;
384                 ConvertEventRefToEventRecord(event, &eventrec);
385                 AEProcessAppleEvent(&eventrec);
386
387                 return false;
388         }
389         return false;
390 }
391
392 #endif  // Q_WS_MACX
393
394 } // namespace frontend
395 } // namespace lyx
396
397 #include "GuiApplication_moc.cpp"