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.h>
55 #if QT_VERSION >= 0x030100
56 #include <qeventloop.h>
58 #include <qpaintdevicemetrics.h>
59 #include <qtranslator.h>
60 #include <qtextcodec.h>
62 using lyx::support::ltrim;
63 using lyx::support::package;
65 using lyx::frontend::QtView;
67 namespace os = lyx::support::os;
69 using boost::shared_ptr;
71 #ifndef CXX_GLOBAL_CSTD
80 extern BufferList bufferlist;
82 // FIXME: wrong place !
83 LyXServer * lyxserver;
84 LyXServerSocket * lyxsocket;
91 QPaintDeviceMetrics pdm(&w);
92 return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
95 map<int, shared_ptr<socket_callback> > socket_callbacks;
108 extern void initEncodings();
111 extern bool lyxX11EventFilter(XEvent * xev);
116 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
120 class LQApplication : public QApplication
123 LQApplication(int & argc, char ** argv);
125 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
128 bool macEventFilter(EventRef event);
133 LQApplication::LQApplication(int & argc, char ** argv)
134 : QApplication(argc, argv)
137 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
138 NewAEEventHandlerUPP(handleOpenDocuments),
145 bool LQApplication::macEventFilter(EventRef event)
147 if (GetEventClass(event) == kEventClassAppleEvent) {
148 EventRecord eventrec;
149 ConvertEventRefToEventRecord(event, &eventrec);
150 AEProcessAppleEvent(&eventrec);
164 void exec(int & argc, char * argv[])
166 // Force adding of font path _before_ QApplication is initialized
167 FontLoader::initFontPath();
169 LQApplication app(argc, argv);
171 #if QT_VERSION >= 0x030200
172 // install translation file for Qt built-in dialogs
173 // These are only installed since Qt 3.2.x
174 QTranslator qt_trans(0);
175 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
176 qInstallPathTranslations())) {
177 qApp->installTranslator(&qt_trans);
178 // even if the language calls for RtL, don't do that
179 qApp->setReverseLayout(false);
181 << "Successfully installed Qt translations for locale "
182 << QTextCodec::locale() << std::endl;
185 << "Could not find Qt translations for locale "
186 << QTextCodec::locale() << std::endl;
190 // These translations are meant to break Qt/Mac menu merging
191 // algorithm on some entries. It lists the menu names that
192 // should not be moved to the LyX menu
193 QTranslator aqua_trans(0);
194 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
196 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
198 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
200 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
203 qApp->installTranslator(&aqua_trans);
206 using namespace lyx::graphics;
208 Image::newImage = boost::bind(&QLImage::newImage);
209 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
211 // needs to be done before reading lyxrc
212 lyxrc.dpi = getDPI();
214 LoaderQueue::setPriority(10,100);
216 LyX::ref().exec2(argc, argv);
224 void start(string const & batch, vector<string> const & files,
225 unsigned int width, unsigned int height, int posx, int posy)
227 // this can't be done before because it needs the Languages object
230 boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
231 LyX::ref().addLyXView(view_ptr);
233 QtView & view = *view_ptr.get();
235 if (posx != -1 && posy != -1)
236 view.move(QPoint(posx, posy));
241 // FIXME: some code below needs moving
243 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
244 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
245 os::internal_path(package().temp_dir() + "/lyxsocket"));
247 for_each(files.begin(), files.end(),
248 bind(&BufferView::loadLyXFile, view.view(), _1, true));
250 // handle the batch commands the user asked for
251 if (!batch.empty()) {
252 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
264 // This is the ONLY place where processEvents may be called.
265 // During screen update/ redraw, this method is disabled to
266 // prevent keyboard events being handed to the LyX core, where
267 // they could cause re-entrant calls to screen update.
268 #if QT_VERSION >= 0x030100
269 qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
274 void exit(int status)
278 // we cannot call QApplication::exit(status) - that could return us
279 // into a static dialog return in the lyx code (for example,
280 // load autosave file QMessageBox. We have to just get the hell
287 FuncStatus getStatus(FuncRequest const & ev)
291 case LFUN_DIALOG_SHOW:
292 if (ev.argument == "preamble")
295 case LFUN_TOOLTIPS_TOGGLE:
306 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
308 QColor const & qcol = lcolorcache.get(col);
309 if (!qcol.isValid()) {
315 rgbcol.r = qcol.red();
316 rgbcol.g = qcol.green();
317 rgbcol.b = qcol.blue();
322 string const hexname(LColor_color col)
324 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
328 void update_color(LColor_color)
330 // FIXME: Bleh, can't we just clear them all at once ?
341 bool font_available(LyXFont const & font)
343 return fontloader.available(font);
347 void register_socket_callback(int fd, boost::function<void()> func)
349 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
353 void unregister_socket_callback(int fd)
355 socket_callbacks.erase(fd);
359 string const roman_font_name()
365 font.setStyleHint(QFont::Serif);
366 font.setFamily("serif");
368 return fromqstr(QFontInfo(font).family());
372 string const sans_font_name()
378 font.setStyleHint(QFont::SansSerif);
379 font.setFamily("sans");
381 return fromqstr(QFontInfo(font).family());
385 string const typewriter_font_name()
391 font.setStyleHint(QFont::TypeWriter);
392 font.setFamily("monospace");
394 return fromqstr(QFontInfo(font).family());
397 }; // namespace lyx_gui