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