]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.cpp
fix building with automake; break scons, cmake and qmake
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
1 /**
2  * \file qt4/GuiApplication.cpp
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/FileName.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/Package.h"
29
30 #include "BufferView.h"
31 #include "Color.h"
32 #include "debug.h"
33 #include "FuncRequest.h"
34 #include "gettext.h"
35 #include "LyX.h"
36 #include "LyXFunc.h"
37 #include "LyXRC.h"
38
39 #include <QApplication>
40 #include <QClipboard>
41 #include <QEventLoop>
42 #include <QFileOpenEvent>
43 #include <QLocale>
44 #include <QLibraryInfo>
45 #include <QTextCodec>
46 #include <QTimer>
47 #include <QTranslator>
48 #include <QWidget>
49
50 #ifdef Q_WS_X11
51 #include <X11/Xatom.h>
52 #include <X11/Xlib.h>
53 #endif
54
55 #include <boost/bind.hpp>
56
57 #include <exception>
58
59 using std::string;
60 using std::endl;
61
62 ///////////////////////////////////////////////////////////////
63 // You can find other X11 specific stuff
64 // at the end of this file...
65 ///////////////////////////////////////////////////////////////
66
67 namespace {
68
69 int getDPI()
70 {
71         QWidget w;
72         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
73 }
74
75 } // namespace anon
76
77
78 namespace lyx {
79
80 using support::FileName;
81
82 frontend::Application * createApplication(int & argc, char * argv[])
83 {
84         return new frontend::GuiApplication(argc, argv);
85 }
86
87
88 namespace frontend {
89
90 GuiApplication * guiApp;
91
92
93 GuiApplication::~GuiApplication()
94 {
95         socket_callbacks_.clear();
96 }
97
98
99 GuiApplication::GuiApplication(int & argc, char ** argv)
100         : QApplication(argc, argv), Application(argc, argv)
101 {
102         // Qt bug? setQuitOnLastWindowClosed(true); does not work
103         setQuitOnLastWindowClosed(false);
104
105 #ifdef Q_WS_X11
106         // doubleClickInterval() is 400 ms on X11 which is just too long.
107         // On Windows and Mac OS X, the operating system's value is used.
108         // On Microsoft Windows, calling this function sets the double
109         // click interval for all applications. So we don't!
110         QApplication::setDoubleClickInterval(300);
111 #endif
112
113         // install translation file for Qt built-in dialogs
114         QString language_name = QString("qt_") + QLocale::system().name();
115         
116         // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN). 
117         // Short-named translator can be loaded from a long name, but not the
118         // opposite. Therefore, long name should be used without truncation.
119         // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
120         if (qt_trans_.load(language_name,
121                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
122         {
123                 installTranslator(&qt_trans_);
124                 // even if the language calls for RtL, don't do that
125                 setLayoutDirection(Qt::LeftToRight);
126                 LYXERR(Debug::GUI)
127                         << "Successfully installed Qt translations for locale "
128                         << fromqstr(language_name) << std::endl;
129         } else
130                 LYXERR(Debug::GUI)
131                         << "Could not find  Qt translations for locale "
132                         << fromqstr(language_name) << std::endl;
133
134 #ifdef Q_WS_MACX
135         // This allows to translate the strings that appear in the LyX menu.
136         addMenuTranslator();
137 #endif
138
139         using namespace lyx::graphics;
140
141         Image::newImage = boost::bind(&QLImage::newImage);
142         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
143
144         // needs to be done before reading lyxrc
145         lyxrc.dpi = getDPI();
146
147         LoaderQueue::setPriority(10,100);
148
149         guiApp = this;
150 }
151
152
153 Clipboard& GuiApplication::clipboard()
154 {
155         return clipboard_;
156 }
157
158
159 Selection& GuiApplication::selection()
160 {
161         return selection_;
162 }
163
164
165 int const GuiApplication::exec()
166 {
167         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
168         return QApplication::exec();
169 }
170
171
172 void GuiApplication::exit(int status)
173 {
174         QApplication::exit(status);
175 }
176
177
178 void GuiApplication::execBatchCommands()
179 {
180         LyX::ref().execBatchCommands();
181 }
182
183
184 string const GuiApplication::romanFontName()
185 {
186         QFont font;
187         font.setKerning(false);
188         font.setStyleHint(QFont::Serif);
189         font.setFamily("serif");
190
191         return fromqstr(QFontInfo(font).family());
192 }
193
194
195 string const GuiApplication::sansFontName()
196 {
197         QFont font;
198         font.setKerning(false);
199         font.setStyleHint(QFont::SansSerif);
200         font.setFamily("sans");
201
202         return fromqstr(QFontInfo(font).family());
203 }
204
205
206 string const GuiApplication::typewriterFontName()
207 {
208         QFont font;
209         font.setKerning(false);
210         font.setStyleHint(QFont::TypeWriter);
211         font.setFamily("monospace");
212
213         return fromqstr(QFontInfo(font).family());
214 }
215
216
217 bool GuiApplication::event(QEvent * e)
218 {
219         switch(e->type()) {
220         case QEvent::FileOpen: {
221                 // Open a file; this happens only on Mac OS X for now
222                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
223
224                 if (!currentView() || !currentView()->view())
225                         // The application is not properly initialized yet.
226                         // So we acknowledge the event and delay the file opening
227                         // until LyX is ready.
228                         // FIXME UNICODE: FileName accept an utf8 encoded string.
229                         LyX::ref().addFileToLoad(FileName(fromqstr(foe->file())));
230                 else
231                         lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
232                                 qstring_to_ucs4(foe->file())));
233
234                 e->accept();
235                 return true;
236         }
237         default:
238                 return QApplication::event(e);
239         }
240 }
241
242
243 bool GuiApplication::notify(QObject * receiver, QEvent * event)
244 {
245         bool return_value;
246         try {
247                 return_value = QApplication::notify(receiver, event);
248         }
249         catch (std::exception  const & e) {
250                 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
251                 LyX::cref().emergencyCleanup();
252                 abort();
253         }
254         catch (...) {
255                 lyxerr << "Caught some really weird exception..." << endl;
256                 LyX::cref().emergencyCleanup();
257                 abort();
258         }
259
260         return return_value;
261 }
262
263
264 void GuiApplication::syncEvents()
265 {
266         // This is the ONLY place where processEvents may be called.
267         // During screen update/ redraw, this method is disabled to
268         // prevent keyboard events being handed to the LyX core, where
269         // they could cause re-entrant calls to screen update.
270         processEvents(QEventLoop::ExcludeUserInputEvents);
271 }
272
273
274 bool GuiApplication::getRgbColor(Color_color col,
275         RGBColor & rgbcol)
276 {
277         QColor const & qcol = color_cache_.get(col);
278         if (!qcol.isValid()) {
279                 rgbcol.r = 0;
280                 rgbcol.g = 0;
281                 rgbcol.b = 0;
282                 return false;
283         }
284         rgbcol.r = qcol.red();
285         rgbcol.g = qcol.green();
286         rgbcol.b = qcol.blue();
287         return true;
288 }
289
290
291 string const GuiApplication::hexName(Color_color col)
292 {
293         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
294 }
295
296
297 void GuiApplication::updateColor(Color_color)
298 {
299         // FIXME: Bleh, can't we just clear them all at once ?
300         color_cache_.clear();
301 }
302
303
304 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
305 {
306         socket_callbacks_[fd] =
307                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
308 }
309
310
311 void GuiApplication::unregisterSocketCallback(int fd)
312 {
313         socket_callbacks_.erase(fd);
314 }
315
316 ////////////////////////////////////////////////////////////////////////
317 // X11 specific stuff goes here...
318 #ifdef Q_WS_X11
319 bool GuiApplication::x11EventFilter(XEvent * xev)
320 {
321         if (!currentView())
322                 return false;
323
324         switch (xev->type) {
325         case SelectionRequest: {
326                 if (xev->xselectionrequest.selection != XA_PRIMARY)
327                         break;
328                 LYXERR(Debug::GUI) << "X requested selection." << endl;
329                 BufferView * bv = currentView()->view();
330                 if (bv) {
331                         docstring const sel = bv->requestSelection();
332                         if (!sel.empty())
333                                 selection_.put(sel);
334                 }
335                 break;
336         }
337         case SelectionClear: {
338                 if (xev->xselectionclear.selection != XA_PRIMARY)
339                         break;
340                 LYXERR(Debug::GUI) << "Lost selection." << endl;
341                 BufferView * bv = currentView()->view();
342                 if (bv)
343                         bv->clearSelection();
344                 break;
345         }
346         }
347         return false;
348 }
349 #endif
350
351
352 ////////////////////////////////////////////////////////////////////////
353 // Mac specific stuff goes here...
354
355 class MenuTranslator : public QTranslator {
356 public:
357         virtual ~MenuTranslator() {};
358         virtual QString translate(const char * context, 
359                                   const char * sourceText, 
360                                   const char * comment = 0) const;
361 };
362
363
364 QString MenuTranslator::translate(const char * /*context*/, 
365                                   const char * sourceText, 
366                                   const char *) const
367 {
368         string const s = sourceText;
369         if (s == N_("About %1") || s == N_("Preferences") 
370             || s == N_("Reconfigure") || s == N_("Quit %1"))
371                 return qt_(s);
372         else 
373                 return QString();
374 }
375
376
377 void GuiApplication::addMenuTranslator()
378 {
379         menu_trans_.reset(new MenuTranslator());
380         installTranslator(menu_trans_.get());
381 }
382
383
384 } // namespace frontend
385 } // namespace lyx
386
387 #include "GuiApplication_moc.cpp"