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