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