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