]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/lyx_gui.C
app -> qApp, forgot this with the last patch
[lyx.git] / src / frontends / qt4 / lyx_gui.C
1 /**
2  * \file qt4/lyx_gui.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "lyx_gui.h"
15
16 // FIXME: move this stuff out again
17 #include "bufferlist.h"
18 #include "BufferView.h"
19 #include "Color.h"
20 #include "funcrequest.h"
21 #include "LColor.h"
22 #include "lyx_main.h"
23 #include "LyXAction.h"
24 #include "lyxfunc.h"
25 #include "lyxrc.h"
26 #include "lyxserver.h"
27 #include "lyxsocket.h"
28
29 #include "graphics/LoaderQueue.h"
30
31 #include "support/lstrings.h"
32 #include "support/os.h"
33 #include "support/package.h"
34 #include "debug.h"
35
36 // Dear Lord, deliver us from Evil, aka the Qt headers
37 // Qt defines a macro 'signals' that clashes with a boost namespace.
38 // All is well if the namespace is visible first.
39 #include <boost/signal.hpp> // FIXME: Is this needed? (Lgb)
40 #include <boost/bind.hpp>
41 #include <boost/shared_ptr.hpp>
42
43 #include "QtView.h"
44 #include "lcolorcache.h"
45 #include "qfont_loader.h"
46 #include "QLImage.h"
47 #include "qt_helpers.h"
48 #include "socket_callback.h"
49
50 #ifdef Q_WS_MACX
51 #include <Carbon/Carbon.h>
52 #endif
53
54 #include <QApplication>
55 #include <QEventLoop>
56 #include <QTranslator>
57 #include <QTextCodec>
58
59 using lyx::support::ltrim;
60 using lyx::support::package;
61
62 using lyx::frontend::QtView;
63
64 namespace os = lyx::support::os;
65
66 using boost::shared_ptr;
67
68 #ifndef CXX_GLOBAL_CSTD
69 using std::exit;
70 #endif
71
72 using std::map;
73 using std::vector;
74 using std::string;
75
76
77 extern BufferList bufferlist;
78
79 // FIXME: wrong place !
80 LyXServer * lyxserver;
81 LyXServerSocket * lyxsocket;
82
83 namespace {
84
85 int getDPI()
86 {
87         QWidget w;
88         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
89 }
90
91 map<int, shared_ptr<socket_callback> > socket_callbacks;
92
93 void cleanup()
94 {
95         delete lyxsocket;
96         lyxsocket = 0;
97         delete lyxserver;
98         lyxserver = 0;
99 }
100
101 } // namespace anon
102
103 // in QLyXKeySym.C
104 extern void initEncodings();
105
106 #ifdef Q_WS_X11
107 extern bool lyxX11EventFilter(XEvent * xev);
108 #endif
109
110 #ifdef Q_WS_MACX
111 extern bool macEventFilter(EventRef event);
112 extern pascal OSErr
113 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
114                     long /*refCon*/);
115 #endif
116
117 class LQApplication : public QApplication
118 {
119 public:
120         LQApplication(int & argc, char ** argv);
121 #ifdef Q_WS_X11
122         bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
123 #endif
124 #ifdef Q_WS_MACX
125         bool macEventFilter(EventRef event);
126 #endif
127 };
128
129
130 LQApplication::LQApplication(int & argc, char ** argv)
131         : QApplication(argc, argv)
132 {
133 #ifdef Q_WS_MACX
134         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
135                               NewAEEventHandlerUPP(handleOpenDocuments),
136                               0, false);
137 #endif
138 }
139
140
141 #ifdef Q_WS_MACX
142 bool LQApplication::macEventFilter(EventRef event)
143 {
144         if (GetEventClass(event) == kEventClassAppleEvent) {
145                 EventRecord eventrec;
146                 ConvertEventRefToEventRecord(event, &eventrec);
147                 AEProcessAppleEvent(&eventrec);
148
149                 return false;
150         }
151         return false;
152 }
153 #endif
154
155
156 namespace lyx_gui {
157
158 bool use_gui = true;
159
160
161 void exec(int & argc, char * argv[])
162 {       
163         // Force adding of font path _before_ QApplication is initialized
164         FontLoader::initFontPath();
165
166         LQApplication app(argc, argv);
167
168         // install translation file for Qt built-in dialogs
169         // These are only installed since Qt 3.2.x
170         QTranslator qt_trans(0);
171         if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
172                           qInstallPathTranslations())) {
173                 qApp->installTranslator(&qt_trans);
174                 // even if the language calls for RtL, don't do that
175                 qApp->setReverseLayout(false);
176                 lyxerr[Debug::GUI]
177                         << "Successfully installed Qt translations for locale "
178                         << QTextCodec::locale() << std::endl;
179         } else
180                 lyxerr[Debug::GUI]
181                         << "Could not find  Qt translations for locale "
182                         << QTextCodec::locale() << std::endl;
183
184 /*#ifdef Q_WS_MACX
185         // These translations are meant to break Qt/Mac menu merging
186         // algorithm on some entries. It lists the menu names that
187         // should not be moved to the LyX menu
188         QTranslator aqua_trans(0);
189         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
190                                              "do_not_merge_me"));
191         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
192                                              "do_not_merge_me"));
193         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
194                                              "do_not_merge_me"));
195         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
196                                              "do_not_merge_me"));
197
198         qApp->installTranslator(&aqua_trans);
199 #endif
200 */
201         using namespace lyx::graphics;
202
203         Image::newImage = boost::bind(&QLImage::newImage);
204         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
205
206         // needs to be done before reading lyxrc
207         lyxrc.dpi = getDPI();
208
209         LoaderQueue::setPriority(10,100);
210
211         LyX::ref().exec2(argc, argv);
212 }
213
214
215 void parse_lyxrc()
216 {}
217
218
219 void start(string const & batch, vector<string> const & files,
220            unsigned int width, unsigned int height, int posx, int posy)
221 {
222         // this can't be done before because it needs the Languages object
223         initEncodings();
224
225         boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
226         LyX::ref().addLyXView(view_ptr);
227
228         QtView & view = *view_ptr.get();
229
230         if (posx != -1 && posy != -1)
231                 view.move(QPoint(posx, posy));
232
233         view.show();
234         view.init();
235
236         // FIXME: some code below needs moving
237
238         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
239         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
240                           os::internal_path(package().temp_dir() + "/lyxsocket"));
241
242         for_each(files.begin(), files.end(),
243                  bind(&BufferView::loadLyXFile, view.view(), _1, true));
244
245         // handle the batch commands the user asked for
246         if (!batch.empty()) {
247                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
248         }
249
250         qApp->exec();
251
252         // FIXME
253         cleanup();
254 }
255
256
257 void sync_events()
258 {
259         // This is the ONLY place where processEvents may be called.
260         // During screen update/ redraw, this method is disabled to
261         // prevent keyboard events being handed to the LyX core, where
262         // they could cause re-entrant calls to screen update.
263 #if QT_VERSION >= 0x030100
264         qApp->processEvents(QEventLoop::ExcludeUserInput);
265 #endif
266 }
267
268
269 void exit(int status)
270 {
271         cleanup();
272
273         // we cannot call QApplication::exit(status) - that could return us
274         // into a static dialog return in the lyx code (for example,
275         // load autosave file QMessageBox. We have to just get the hell
276         // out.
277
278         ::exit(status);
279 }
280
281
282 FuncStatus getStatus(FuncRequest const & ev)
283 {
284         FuncStatus flag;
285         switch (ev.action) {
286         case LFUN_DIALOG_SHOW:
287                 if (ev.argument == "preamble")
288                         flag.unknown(true);
289                 break;
290         case LFUN_TOOLTIPS_TOGGLE:
291                 flag.unknown(true);
292                 break;
293         default:
294                 break;
295         }
296
297         return flag;
298 }
299
300
301 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
302 {
303         QColor const & qcol = lcolorcache.get(col);
304         if (!qcol.isValid()) {
305                 rgbcol.r = 0;
306                 rgbcol.g = 0;
307                 rgbcol.b = 0;
308                 return false;
309         }
310         rgbcol.r = qcol.red();
311         rgbcol.g = qcol.green();
312         rgbcol.b = qcol.blue();
313         return true;
314 }
315
316
317 string const hexname(LColor_color col)
318 {
319         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
320 }
321
322
323 void update_color(LColor_color)
324 {
325         // FIXME: Bleh, can't we just clear them all at once ?
326         lcolorcache.clear();
327 }
328
329
330 void update_fonts()
331 {
332         fontloader.update();
333 }
334
335
336 bool font_available(LyXFont const & font)
337 {
338         return fontloader.available(font);
339 }
340
341
342 void register_socket_callback(int fd, boost::function<void()> func)
343 {
344         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
345 }
346
347
348 void unregister_socket_callback(int fd)
349 {
350         socket_callbacks.erase(fd);
351 }
352
353
354 string const roman_font_name()
355 {
356         if (!use_gui)
357                 return "serif";
358
359         QFont font;
360         font.setStyleHint(QFont::Serif);
361         font.setFamily("serif");
362
363         return fromqstr(QFontInfo(font).family());
364 }
365
366
367 string const sans_font_name()
368 {
369         if (!use_gui)
370                 return "sans";
371
372         QFont font;
373         font.setStyleHint(QFont::SansSerif);
374         font.setFamily("sans");
375
376         return fromqstr(QFontInfo(font).family());
377 }
378
379
380 string const typewriter_font_name()
381 {
382         if (!use_gui)
383                 return "monospace";
384
385         QFont font;
386         font.setStyleHint(QFont::TypeWriter);
387         font.setFamily("monospace");
388
389         return fromqstr(QFontInfo(font).family());
390 }
391
392 }; // namespace lyx_gui