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.h>
54 #include <qpaintdevicemetrics.h>
55 #include <qtranslator.h>
56 #include <qtextcodec.h>
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 QPaintDeviceMetrics pdm(&w);
84 return int(0.5 * (pdm.logicalDpiX() + pdm.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 qfont_loader::initFontPath();
162 static LQApplication app(argc, argv);
164 #if QT_VERSION >= 0x030200
165 // install translation file for Qt built-in dialogs
166 // These are only installed since Qt 3.2.x
167 static QTranslator qt_trans(0);
168 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
169 qInstallPathTranslations())) {
170 app.installTranslator(&qt_trans);
171 // even if the language calls for RtL, don't do that
172 app.setReverseLayout(false);
174 << "Successfully installed Qt translations for locale "
175 << QTextCodec::locale() << std::endl;
178 << "Could not find Qt translations for locale "
179 << QTextCodec::locale() << std::endl;
183 // These translations are meant to break Qt/Mac menu merging
184 // algorithm on some entries. It lists the menu names that
185 // should not be moved to the LyX menu
186 static QTranslator aqua_trans(0);
187 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
189 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
191 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
193 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
196 app.installTranslator(&aqua_trans);
199 using namespace lyx::graphics;
201 Image::newImage = boost::bind(&QLImage::newImage);
202 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
204 // needs to be done before reading lyxrc
205 lyxrc.dpi = getDPI();
209 LoaderQueue::setPriority(10,100);
217 void start(string const & batch, vector<string> const & files)
220 unsigned int width = 690;
221 unsigned int height = 510;
223 boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
224 LyX::ref().addLyXView(view_ptr);
226 QtView & view = *view_ptr.get();
230 // FIXME: some code below needs moving
232 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
233 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
234 os::internal_path(package().temp_dir() + "/lyxsocket"));
236 for_each(files.begin(), files.end(),
237 bind(&BufferView::loadLyXFile, view.view(), _1, true));
239 // handle the batch commands the user asked for
240 if (!batch.empty()) {
241 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
255 qApp->processEvents();
265 // we cannot call qApp->exit(0) - that could return us
266 // into a static dialog return in the lyx code (for example,
267 // load autosave file QMessageBox. We have to just get the hell
274 FuncStatus getStatus(FuncRequest const & ev)
278 case LFUN_DIALOG_SHOW:
279 if (ev.argument == "preamble")
282 case LFUN_TOOLTIPS_TOGGLE:
293 string const hexname(LColor_color col)
295 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
299 void update_color(LColor_color)
301 // FIXME: Bleh, can't we just clear them all at once ?
312 bool font_available(LyXFont const & font)
314 return fontloader.available(font);
318 void register_socket_callback(int fd, boost::function<void()> func)
320 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
324 void unregister_socket_callback(int fd)
326 socket_callbacks.erase(fd);
330 string const roman_font_name()
336 font.setStyleHint(QFont::Serif);
337 font.setFamily("serif");
339 return fromqstr(QFontInfo(font).family());
343 string const sans_font_name()
349 font.setStyleHint(QFont::SansSerif);
350 font.setFamily("sans");
352 return fromqstr(QFontInfo(font).family());
356 string const typewriter_font_name()
362 font.setStyleHint(QFont::TypeWriter);
363 font.setFamily("monospace");
365 return fromqstr(QFontInfo(font).family());
368 }; // namespace lyx_gui