]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
b752ad39887f6557af6e74828a75474c2a86debe
[lyx.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 "frontends/LyXView.h"
22
23 #include "graphics/LoaderQueue.h"
24
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/package.h"
28
29 #include "BufferView.h"
30 #include "Color.h"
31 #include "debug.h"
32 #include "funcrequest.h"
33 #include "lyx_main.h"
34 #include "lyxfunc.h"
35 #include "lyxrc.h"
36
37 #include <QApplication>
38 #include <QClipboard>
39 #include <QEventLoop>
40 #include <QFileOpenEvent>
41 #include <QLocale>
42 #include <QLibraryInfo>
43 #include <QTextCodec>
44 #include <QTimer>
45 #include <QTranslator>
46 #include <QWidget>
47
48 #ifdef Q_WS_X11
49 #include <X11/Xlib.h>
50 #endif
51
52 #include <boost/bind.hpp>
53
54 using std::string;
55 using std::endl;
56
57 // in QLyXKeySym.C
58 extern void initEncodings();
59
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 and MACX specific stuff
62 // at the end of this file...
63 ///////////////////////////////////////////////////////////////
64
65 namespace {
66
67 int getDPI()
68 {
69         QWidget w;
70         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
71 }
72
73 } // namespace anon
74
75
76 namespace lyx {
77
78 frontend::Application * createApplication(int & argc, char * argv[])
79 {
80         return new frontend::GuiApplication(argc, argv);
81 }
82
83
84 namespace frontend {
85
86 GuiApplication * guiApp;
87
88
89 GuiApplication::~GuiApplication()
90 {
91         socket_callbacks_.clear();
92 }
93
94
95 GuiApplication::GuiApplication(int & argc, char ** argv)
96         : QApplication(argc, argv), Application(argc, argv)
97 {
98         // Qt bug? setQuitOnLastWindowClosed(true); does not work
99         setQuitOnLastWindowClosed(false);
100
101 #ifdef Q_WS_X11
102         // doubleClickInterval() is 400 ms on X11 which is just too long.
103         // On Windows and Mac OS X, the operating system's value is used.
104         // On Microsoft Windows, calling this function sets the double
105         // click interval for all applications. So we don't!
106         QApplication::setDoubleClickInterval(300);
107 #endif
108
109         // install translation file for Qt built-in dialogs
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         guiApp = this;
155 }
156
157 Clipboard& GuiApplication::clipboard()
158 {
159         return clipboard_;
160 }
161
162
163 Selection& GuiApplication::selection()
164 {
165         return selection_;
166 }
167
168
169 int const GuiApplication::exec()
170 {
171         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
172         return QApplication::exec();
173 }
174
175
176 void GuiApplication::exit(int status)
177 {
178         QApplication::exit(status);
179 }
180
181
182 void GuiApplication::execBatchCommands()
183 {
184         LyX::ref().execBatchCommands();
185 }
186
187
188 string const GuiApplication::romanFontName()
189 {
190         QFont font;
191         font.setKerning(false);
192         font.setStyleHint(QFont::Serif);
193         font.setFamily("serif");
194
195         return fromqstr(QFontInfo(font).family());
196 }
197
198
199 string const GuiApplication::sansFontName()
200 {
201         QFont font;
202         font.setKerning(false);
203         font.setStyleHint(QFont::SansSerif);
204         font.setFamily("sans");
205
206         return fromqstr(QFontInfo(font).family());
207 }
208
209
210 string const GuiApplication::typewriterFontName()
211 {
212         QFont font;
213         font.setKerning(false);
214         font.setStyleHint(QFont::TypeWriter);
215         font.setFamily("monospace");
216
217         return fromqstr(QFontInfo(font).family());
218 }
219
220
221 bool GuiApplication::event(QEvent * e)
222 {
223         switch(e->type()) {
224         case QEvent::FileOpen: {
225                 // Open a file; this happens only on Mac OS X for now
226                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
227                 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
228                                           fromqstr(foe->file())));
229                 return true;
230         }
231         default:
232                 return false;
233         }
234 }
235
236
237 void GuiApplication::syncEvents()
238 {
239         // This is the ONLY place where processEvents may be called.
240         // During screen update/ redraw, this method is disabled to
241         // prevent keyboard events being handed to the LyX core, where
242         // they could cause re-entrant calls to screen update.
243         processEvents(QEventLoop::ExcludeUserInputEvents);
244 }
245
246
247 bool GuiApplication::getRgbColor(LColor_color col,
248         lyx::RGBColor & rgbcol)
249 {
250         QColor const & qcol = color_cache_.get(col);
251         if (!qcol.isValid()) {
252                 rgbcol.r = 0;
253                 rgbcol.g = 0;
254                 rgbcol.b = 0;
255                 return false;
256         }
257         rgbcol.r = qcol.red();
258         rgbcol.g = qcol.green();
259         rgbcol.b = qcol.blue();
260         return true;
261 }
262
263
264 string const GuiApplication::hexName(LColor_color col)
265 {
266         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
267 }
268
269
270 void GuiApplication::updateColor(LColor_color)
271 {
272         // FIXME: Bleh, can't we just clear them all at once ?
273         color_cache_.clear();
274 }
275
276
277 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
278 {
279         socket_callbacks_[fd] =
280                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
281 }
282
283
284 void GuiApplication::unregisterSocketCallback(int fd)
285 {
286         socket_callbacks_.erase(fd);
287 }
288
289 ////////////////////////////////////////////////////////////////////////
290 // X11 specific stuff goes here...
291 #ifdef Q_WS_X11
292 bool GuiApplication::x11EventFilter(XEvent * xev)
293 {
294         if (!currentView())
295                 return false;
296
297         BufferView * bv = currentView()->view();
298
299         switch (xev->type) {
300         case SelectionRequest:
301                 lyxerr[Debug::GUI] << "X requested selection." << endl;
302                 if (bv) {
303                         lyx::docstring const sel = bv->requestSelection();
304                         if (!sel.empty())
305                                 selection_.put(sel);
306                 }
307                 break;
308         case SelectionClear:
309                 lyxerr[Debug::GUI] << "Lost selection." << endl;
310                 if (bv)
311                         bv->clearSelection();
312                 break;
313         }
314         return false;
315 }
316 #endif
317
318
319 } // namespace frontend
320 } // namespace lyx
321
322 #include "GuiApplication_moc.cpp"