]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
d7293c3004688d56b77f2025d3f2f7e817ef8c05
[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 "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 #else
84         lyx::frontend::GuiApplication app(argc, argv);
85 #endif
86
87         return &app;
88 }
89
90
91 namespace frontend {
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 witch 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         // These are only installed since Qt 3.2.x
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
160 Clipboard& GuiApplication::clipboard()
161 {
162         return clipboard_;
163 }
164
165
166 Selection& GuiApplication::selection()
167 {
168         return selection_;
169 }
170
171
172 int const GuiApplication::exec()
173 {
174         return QApplication::exec();
175 }
176
177
178 void GuiApplication::exit(int status)
179 {
180         QApplication::exit(status);
181 }
182
183
184 string const GuiApplication::romanFontName()
185 {
186         QFont font;
187         font.setStyleHint(QFont::Serif);
188         font.setFamily("serif");
189
190         return fromqstr(QFontInfo(font).family());
191 }
192
193
194 string const GuiApplication::sansFontName()
195 {
196         QFont font;
197         font.setStyleHint(QFont::SansSerif);
198         font.setFamily("sans");
199
200         return fromqstr(QFontInfo(font).family());
201 }
202
203
204 string const GuiApplication::typewriterFontName()
205 {
206         QFont font;
207         font.setStyleHint(QFont::TypeWriter);
208         font.setFamily("monospace");
209
210         return fromqstr(QFontInfo(font).family());
211 }
212
213
214 void GuiApplication::syncEvents()
215 {
216         // This is the ONLY place where processEvents may be called.
217         // During screen update/ redraw, this method is disabled to
218         // prevent keyboard events being handed to the LyX core, where
219         // they could cause re-entrant calls to screen update.
220         processEvents(QEventLoop::ExcludeUserInputEvents);
221 }
222
223
224 bool GuiApplication::getRgbColor(LColor_color col,
225                                                                  lyx::RGBColor & rgbcol)
226 {
227         QColor const & qcol = color_cache_.get(col);
228         if (!qcol.isValid()) {
229                 rgbcol.r = 0;
230                 rgbcol.g = 0;
231                 rgbcol.b = 0;
232                 return false;
233         }
234         rgbcol.r = qcol.red();
235         rgbcol.g = qcol.green();
236         rgbcol.b = qcol.blue();
237         return true;
238 }
239
240
241 string const GuiApplication::hexName(LColor_color col)
242 {
243         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
244 }
245
246
247 void GuiApplication::updateColor(LColor_color)
248 {
249         // FIXME: Bleh, can't we just clear them all at once ?
250         color_cache_.clear();
251 }
252
253
254 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
255 {
256         socket_callbacks_[fd] =
257                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
258 }
259
260
261 void GuiApplication::unregisterSocketCallback(int fd)
262 {
263         socket_callbacks_.erase(fd);
264 }
265
266 ////////////////////////////////////////////////////////////////////////
267 // X11 specific stuff goes here...
268 #ifdef Q_WS_X11
269 bool GuiApplication::x11EventFilter(XEvent * xev)
270 {
271         switch (xev->type) {
272         case SelectionRequest:
273                 lyxerr[Debug::GUI] << "X requested selection." << endl;
274                 if (buffer_view_) {
275                         lyx::docstring const sel = buffer_view_->requestSelection();
276                         if (!sel.empty())
277                                 selection_.put(sel);
278                 }
279                 break;
280         case SelectionClear:
281                 lyxerr[Debug::GUI] << "Lost selection." << endl;
282                 if (buffer_view_)
283                         buffer_view_->clearSelection();
284                 break;
285         }
286         return false;
287 }
288 #endif
289
290
291 ////////////////////////////////////////////////////////////////////////
292 // Mac OSX specific stuff goes here...
293
294 #ifdef Q_WS_MACX
295 namespace{
296
297 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
298  {
299         DescType returnedType;
300         Size actualSize;
301         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
302                                       typeWildCard, &returnedType, nil, 0,
303                                       &actualSize);
304         switch (err) {
305         case errAEDescNotFound:
306                 return noErr;
307         case noErr:
308                 return errAEEventNotHandled;
309         default:
310                 return err;
311         }
312  }
313
314 } // namespace
315
316 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
317                                        AppleEvent* /*reply*/, long /*refCon*/)
318 {
319         QString s_arg;
320         AEDescList documentList;
321         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
322                                    &documentList);
323         if (err != noErr)
324                 return err;
325
326         err = checkAppleEventForMissingParams(*inEvent);
327         if (err == noErr) {
328                 long documentCount;
329                 err = AECountItems(&documentList, &documentCount);
330                 for (long documentIndex = 1;
331                      err == noErr && documentIndex <= documentCount;
332                      documentIndex++) {
333                         DescType returnedType;
334                         Size actualSize;
335                         AEKeyword keyword;
336                         FSRef ref;
337                         char qstr_buf[1024];
338                         err = AESizeOfNthItem(&documentList, documentIndex,
339                                               &returnedType, &actualSize);
340                         if (err == noErr) {
341                                 err = AEGetNthPtr(&documentList, documentIndex,
342                                                   typeFSRef, &keyword,
343                                                   &returnedType, (Ptr)&ref,
344                                                   sizeof(FSRef), &actualSize);
345                                 if (err == noErr) {
346                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
347                                                       1024);
348                                         s_arg=QString::fromUtf8(qstr_buf);
349 //                                      buffer_view_->workAreaDispatch(
350 //                                              FuncRequest(LFUN_FILE_OPEN,
351 //                                                          fromqstr(s_arg)));
352                                         break;
353                                 }
354                         }
355                 } // for ...
356         }
357         AEDisposeDesc(&documentList);
358
359         return err;
360 }
361
362 bool GuiApplication::macEventFilter(EventRef event)
363 {
364         if (GetEventClass(event) == kEventClassAppleEvent) {
365                 EventRecord eventrec;
366                 ConvertEventRefToEventRecord(event, &eventrec);
367                 AEProcessAppleEvent(&eventrec);
368
369                 return false;
370         }
371         return false;
372 }
373
374 #endif  // Q_WS_MACX
375
376 } // namespace frontend
377 } // namespace lyx