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