]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/lyx_gui.C
* src/frontends/*/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         ~LQApplication();
118 #ifdef Q_WS_X11
119         bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
120 #endif
121 #ifdef Q_WS_MACX
122         bool macEventFilter(EventRef event);
123 #endif
124 };
125
126
127 LQApplication::LQApplication(int & argc, char ** argv)
128         : QApplication(argc, argv)
129 {
130 #ifdef Q_WS_MACX
131         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
132                               NewAEEventHandlerUPP(handleOpenDocuments),
133                               0, false);
134 #endif
135 }
136
137
138 LQApplication::~LQApplication()
139 {}
140
141
142 #ifdef Q_WS_MACX
143 bool LQApplication::macEventFilter(EventRef event)
144 {
145         if (GetEventClass(event) == kEventClassAppleEvent) {
146                 EventRecord eventrec;
147                 ConvertEventRefToEventRecord(event, &eventrec);
148                 AEProcessAppleEvent(&eventrec);
149
150                 return false;
151         }
152         return false;
153 }
154 #endif
155
156
157 namespace lyx_gui {
158
159 bool use_gui = true;
160
161 void parse_init(int & argc, char * argv[])
162 {
163         // Force adding of font path _before_ QApplication is initialized
164         FontLoader::initFontPath();
165
166         static LQApplication app(argc, argv);
167
168 #if QT_VERSION >= 0x030200
169         // install translation file for Qt built-in dialogs
170         // These are only installed since Qt 3.2.x
171         static QTranslator qt_trans(0);
172         if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
173                           qInstallPathTranslations())) {
174                 app.installTranslator(&qt_trans);
175                 // even if the language calls for RtL, don't do that
176                 app.setReverseLayout(false);
177                 lyxerr[Debug::GUI]
178                         << "Successfully installed Qt translations for locale "
179                         << QTextCodec::locale() << std::endl;
180         } else
181                 lyxerr[Debug::GUI]
182                         << "Could not find  Qt translations for locale "
183                         << QTextCodec::locale() << std::endl;
184 #endif
185
186 #ifdef Q_WS_MACX
187         // These translations are meant to break Qt/Mac menu merging
188         // algorithm on some entries. It lists the menu names that
189         // should not be moved to the LyX menu
190         static QTranslator aqua_trans(0);
191         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
192                                              "do_not_merge_me"));
193         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
194                                              "do_not_merge_me"));
195         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
196                                              "do_not_merge_me"));
197         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
198                                              "do_not_merge_me"));
199
200         app.installTranslator(&aqua_trans);
201 #endif
202
203         using namespace lyx::graphics;
204
205         Image::newImage = boost::bind(&QLImage::newImage);
206         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
207
208         // needs to be done before reading lyxrc
209         lyxrc.dpi = getDPI();
210
211         LoaderQueue::setPriority(10,100);
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         delete lyxsocket;
254         delete lyxserver;
255         lyxserver = 0;
256 }
257
258
259 void sync_events()
260 {
261         // This is the ONLY place where processEvents may be called.
262         // During screen update/ redraw, this method is disabled to
263         // prevent keyboard events being handed to the LyX core, where
264         // they could cause re-entrant calls to screen update.
265 #if QT_VERSION >= 0x030100
266         qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
267 #endif
268 }
269
270
271 void exit()
272 {
273         delete lyxsocket;
274         delete lyxserver;
275         lyxserver = 0;
276
277         // we cannot call qApp->exit(0) - that could return us
278         // into a static dialog return in the lyx code (for example,
279         // load autosave file QMessageBox. We have to just get the hell
280         // out.
281
282         ::exit(0);
283 }
284
285
286 FuncStatus getStatus(FuncRequest const & ev)
287 {
288         FuncStatus flag;
289         switch (ev.action) {
290         case LFUN_DIALOG_SHOW:
291                 if (ev.argument == "preamble")
292                         flag.unknown(true);
293                 break;
294         case LFUN_TOOLTIPS_TOGGLE:
295                 flag.unknown(true);
296                 break;
297         default:
298                 break;
299         }
300
301         return flag;
302 }
303
304
305 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
306 {
307         QColor const & qcol = lcolorcache.get(col);
308         if (!qcol.isValid()) {
309                 rgbcol.r = 0;
310                 rgbcol.g = 0;
311                 rgbcol.b = 0;
312                 return false;
313         }
314         rgbcol.r = qcol.red();
315         rgbcol.g = qcol.green();
316         rgbcol.b = qcol.blue();
317         return true;
318 }
319
320
321 string const hexname(LColor_color col)
322 {
323         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
324 }
325
326
327 void update_color(LColor_color)
328 {
329         // FIXME: Bleh, can't we just clear them all at once ?
330         lcolorcache.clear();
331 }
332
333
334 void update_fonts()
335 {
336         fontloader.update();
337 }
338
339
340 bool font_available(LyXFont const & font)
341 {
342         return fontloader.available(font);
343 }
344
345
346 void register_socket_callback(int fd, boost::function<void()> func)
347 {
348         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
349 }
350
351
352 void unregister_socket_callback(int fd)
353 {
354         socket_callbacks.erase(fd);
355 }
356
357
358 string const roman_font_name()
359 {
360         if (!use_gui)
361                 return "serif";
362
363         QFont font;
364         font.setStyleHint(QFont::Serif);
365         font.setFamily("serif");
366
367         return fromqstr(QFontInfo(font).family());
368 }
369
370
371 string const sans_font_name()
372 {
373         if (!use_gui)
374                 return "sans";
375
376         QFont font;
377         font.setStyleHint(QFont::SansSerif);
378         font.setFamily("sans");
379
380         return fromqstr(QFontInfo(font).family());
381 }
382
383
384 string const typewriter_font_name()
385 {
386         if (!use_gui)
387                 return "monospace";
388
389         QFont font;
390         font.setStyleHint(QFont::TypeWriter);
391         font.setFamily("monospace");
392
393         return fromqstr(QFontInfo(font).family());
394 }
395
396 }; // namespace lyx_gui