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"
20 #include "funcrequest.h"
23 #include "LyXAction.h"
26 #include "lyxserver.h"
27 #include "lyxsocket.h"
29 #include "graphics/LoaderQueue.h"
31 #include "support/lstrings.h"
32 #include "support/os.h"
33 #include "support/package.h"
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>
44 #include "lcolorcache.h"
45 #include "qfont_loader.h"
47 #include "qt_helpers.h"
48 #include "socket_callback.h"
51 #include <Carbon/Carbon.h>
54 #include <QApplication>
56 #include <QTranslator>
59 using lyx::support::ltrim;
60 using lyx::support::package;
62 using lyx::frontend::QtView;
64 namespace os = lyx::support::os;
66 using boost::shared_ptr;
68 #ifndef CXX_GLOBAL_CSTD
77 extern BufferList bufferlist;
84 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
87 map<int, shared_ptr<socket_callback> > socket_callbacks;
91 // FIXME: wrong place !
92 LyXServer * lyxserver;
93 LyXServerSocket * lyxsocket;
96 extern void initEncodings();
99 extern bool lyxX11EventFilter(XEvent * xev);
103 extern bool macEventFilter(EventRef event);
105 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
109 class LQApplication : public QApplication
112 LQApplication(int & argc, char ** argv);
115 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
118 bool macEventFilter(EventRef event);
123 LQApplication::LQApplication(int & argc, char ** argv)
124 : QApplication(argc, argv)
127 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
128 NewAEEventHandlerUPP(handleOpenDocuments),
134 LQApplication::~LQApplication()
139 bool LQApplication::macEventFilter(EventRef event)
141 if (GetEventClass(event) == kEventClassAppleEvent) {
142 EventRecord eventrec;
143 ConvertEventRefToEventRecord(event, &eventrec);
144 AEProcessAppleEvent(&eventrec);
157 void parse_init(int & argc, char * argv[])
159 // Force adding of font path _before_ QApplication is initialized
160 FontLoader::initFontPath();
162 static LQApplication app(argc, argv);
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);
173 << "Successfully installed Qt translations for locale "
174 << QTextCodec::locale() << std::endl;
177 << "Could not find Qt translations for locale "
178 << QTextCodec::locale() << std::endl;
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,
187 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
189 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
191 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
194 app.installTranslator(&aqua_trans);
197 using namespace lyx::graphics;
199 Image::newImage = boost::bind(&QLImage::newImage);
200 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
202 // needs to be done before reading lyxrc
203 lyxrc.dpi = getDPI();
205 LoaderQueue::setPriority(10,100);
213 void start(string const & batch, vector<string> const & files)
215 // this can't be done before because it needs the Languages object
219 unsigned int width = 690;
220 unsigned int height = 510;
222 boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
223 LyX::ref().addLyXView(view_ptr);
225 QtView & view = *view_ptr.get();
229 // FIXME: some code below needs moving
231 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
232 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
233 os::internal_path(package().temp_dir() + "/lyxsocket"));
235 for_each(files.begin(), files.end(),
236 bind(&BufferView::loadLyXFile, view.view(), _1, true));
238 // handle the batch commands the user asked for
239 if (!batch.empty()) {
240 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
254 // This is the ONLY place where processEvents may be called.
255 // During screen update/ redraw, this method is disabled to
256 // prevent keyboard events being handed to the LyX core, where
257 // they could cause re-entrant calls to screen update.
258 #if QT_VERSION >= 0x030100
259 qApp->processEvents(QEventLoop::ExcludeUserInput);
270 // we cannot call qApp->exit(0) - that could return us
271 // into a static dialog return in the lyx code (for example,
272 // load autosave file QMessageBox. We have to just get the hell
279 FuncStatus getStatus(FuncRequest const & ev)
283 case LFUN_DIALOG_SHOW:
284 if (ev.argument == "preamble")
287 case LFUN_TOOLTIPS_TOGGLE:
298 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
300 QColor const & qcol = lcolorcache.get(col);
301 if (!qcol.isValid()) {
307 rgbcol.r = qcol.red();
308 rgbcol.g = qcol.green();
309 rgbcol.b = qcol.blue();
314 string const hexname(LColor_color col)
316 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
320 void update_color(LColor_color)
322 // FIXME: Bleh, can't we just clear them all at once ?
333 bool font_available(LyXFont const & font)
335 return fontloader.available(font);
339 void register_socket_callback(int fd, boost::function<void()> func)
341 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
345 void unregister_socket_callback(int fd)
347 socket_callbacks.erase(fd);
351 string const roman_font_name()
357 font.setStyleHint(QFont::Serif);
358 font.setFamily("serif");
360 return fromqstr(QFontInfo(font).family());
364 string const sans_font_name()
370 font.setStyleHint(QFont::SansSerif);
371 font.setFamily("sans");
373 return fromqstr(QFontInfo(font).family());
377 string const typewriter_font_name()
383 font.setStyleHint(QFont::TypeWriter);
384 font.setFamily("monospace");
386 return fromqstr(QFontInfo(font).family());
389 }; // namespace lyx_gui