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