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