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