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