3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
16 // FIXME: move this stuff out again
17 #include "bufferlist.h"
18 #include "BufferView.h"
19 #include "funcrequest.h"
22 #include "LyXAction.h"
25 #include "lyxserver.h"
26 #include "lyxsocket.h"
28 #include "graphics/LoaderQueue.h"
30 #include "support/lstrings.h"
31 #include "support/os.h"
32 #include "support/package.h"
35 // Dear Lord, deliver us from Evil, aka the Qt headers
36 // Qt defines a macro 'signals' that clashes with a boost namespace.
37 // All is well if the namespace is visible first.
38 #include <boost/signal.hpp> // FIXME: Is this needed? (Lgb)
39 #include <boost/bind.hpp>
40 #include <boost/shared_ptr.hpp>
43 #include "lcolorcache.h"
44 #include "qfont_loader.h"
46 #include "qt_helpers.h"
47 #include "socket_callback.h"
50 #include <Carbon/Carbon.h>
53 #include <QApplication>
55 #include <QTranslator>
58 using lyx::support::ltrim;
59 using lyx::support::package;
61 using lyx::frontend::QtView;
63 namespace os = lyx::support::os;
65 using boost::shared_ptr;
67 #ifndef CXX_GLOBAL_CSTD
76 extern BufferList bufferlist;
83 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
86 map<int, shared_ptr<socket_callback> > socket_callbacks;
90 // FIXME: wrong place !
91 LyXServer * lyxserver;
92 LyXServerSocket * lyxsocket;
95 extern void initEncodings();
98 extern bool lyxX11EventFilter(XEvent * xev);
102 extern bool macEventFilter(EventRef event);
104 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
108 class LQApplication : public QApplication
111 LQApplication(int & argc, char ** argv);
114 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
117 bool macEventFilter(EventRef event);
122 LQApplication::LQApplication(int & argc, char ** argv)
123 : QApplication(argc, argv)
126 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
127 NewAEEventHandlerUPP(handleOpenDocuments),
133 LQApplication::~LQApplication()
138 bool LQApplication::macEventFilter(EventRef event)
140 if (GetEventClass(event) == kEventClassAppleEvent) {
141 EventRecord eventrec;
142 ConvertEventRefToEventRecord(event, &eventrec);
143 AEProcessAppleEvent(&eventrec);
156 void parse_init(int & argc, char * argv[])
158 // Force adding of font path _before_ QApplication is initialized
159 FontLoader::initFontPath();
161 static LQApplication app(argc, argv);
163 // install translation file for Qt built-in dialogs
164 // These are only installed since Qt 3.2.x
165 static QTranslator qt_trans(0);
166 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
167 qInstallPathTranslations())) {
168 app.installTranslator(&qt_trans);
169 // even if the language calls for RtL, don't do that
170 app.setReverseLayout(false);
172 << "Successfully installed Qt translations for locale "
173 << QTextCodec::locale() << std::endl;
176 << "Could not find Qt translations for locale "
177 << QTextCodec::locale() << std::endl;
180 // These translations are meant to break Qt/Mac menu merging
181 // algorithm on some entries. It lists the menu names that
182 // should not be moved to the LyX menu
183 static QTranslator aqua_trans(0);
184 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
186 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
188 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
190 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
193 app.installTranslator(&aqua_trans);
196 using namespace lyx::graphics;
198 Image::newImage = boost::bind(&QLImage::newImage);
199 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
201 // needs to be done before reading lyxrc
202 lyxrc.dpi = getDPI();
204 LoaderQueue::setPriority(10,100);
212 void start(string const & batch, vector<string> const & files)
214 // this can't be done before because it needs the Languages object
218 unsigned int width = 690;
219 unsigned int height = 510;
221 boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
222 LyX::ref().addLyXView(view_ptr);
224 QtView & view = *view_ptr.get();
228 // FIXME: some code below needs moving
230 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
231 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
232 os::internal_path(package().temp_dir() + "/lyxsocket"));
234 for_each(files.begin(), files.end(),
235 bind(&BufferView::loadLyXFile, view.view(), _1, true));
237 // handle the batch commands the user asked for
238 if (!batch.empty()) {
239 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
253 // This is the ONLY place where processEvents may be called.
254 // During screen update/ redraw, this method is disabled to
255 // prevent keyboard events being handed to the LyX core, where
256 // they could cause re-entrant calls to screen update.
257 #if QT_VERSION >= 0x030100
258 qApp->processEvents(QEventLoop::ExcludeUserInput);
269 // we cannot call qApp->exit(0) - that could return us
270 // into a static dialog return in the lyx code (for example,
271 // load autosave file QMessageBox. We have to just get the hell
278 FuncStatus getStatus(FuncRequest const & ev)
282 case LFUN_DIALOG_SHOW:
283 if (ev.argument == "preamble")
286 case LFUN_TOOLTIPS_TOGGLE:
297 string const hexname(LColor_color col)
299 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
303 void update_color(LColor_color)
305 // FIXME: Bleh, can't we just clear them all at once ?
316 bool font_available(LyXFont const & font)
318 return fontloader.available(font);
322 void register_socket_callback(int fd, boost::function<void()> func)
324 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
328 void unregister_socket_callback(int fd)
330 socket_callbacks.erase(fd);
334 string const roman_font_name()
340 font.setStyleHint(QFont::Serif);
341 font.setFamily("serif");
343 return fromqstr(QFontInfo(font).family());
347 string const sans_font_name()
353 font.setStyleHint(QFont::SansSerif);
354 font.setFamily("sans");
356 return fromqstr(QFontInfo(font).family());
360 string const typewriter_font_name()
366 font.setStyleHint(QFont::TypeWriter);
367 font.setFamily("monospace");
369 return fromqstr(QFontInfo(font).family());
372 }; // namespace lyx_gui