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;
79 // FIXME: wrong place !
80 LyXServer * lyxserver;
81 LyXServerSocket * lyxsocket;
88 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
91 map<int, shared_ptr<socket_callback> > socket_callbacks;
104 extern void initEncodings();
107 extern bool lyxX11EventFilter(XEvent * xev);
111 extern bool macEventFilter(EventRef event);
113 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
117 class LQApplication : public QApplication
120 LQApplication(int & argc, char ** argv);
122 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
125 bool macEventFilter(EventRef event);
130 LQApplication::LQApplication(int & argc, char ** argv)
131 : QApplication(argc, argv)
134 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
135 NewAEEventHandlerUPP(handleOpenDocuments),
142 bool LQApplication::macEventFilter(EventRef event)
144 if (GetEventClass(event) == kEventClassAppleEvent) {
145 EventRecord eventrec;
146 ConvertEventRefToEventRecord(event, &eventrec);
147 AEProcessAppleEvent(&eventrec);
161 void exec(int & argc, char * argv[])
163 // Force adding of font path _before_ QApplication is initialized
164 FontLoader::initFontPath();
166 LQApplication app(argc, argv);
168 // install translation file for Qt built-in dialogs
169 // These are only installed since Qt 3.2.x
170 QTranslator qt_trans(0);
171 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
172 qInstallPathTranslations())) {
173 qApp->installTranslator(&qt_trans);
174 // even if the language calls for RtL, don't do that
175 qApp->setReverseLayout(false);
177 << "Successfully installed Qt translations for locale "
178 << QTextCodec::locale() << std::endl;
181 << "Could not find Qt translations for locale "
182 << QTextCodec::locale() << std::endl;
185 // These translations are meant to break Qt/Mac menu merging
186 // algorithm on some entries. It lists the menu names that
187 // should not be moved to the LyX menu
188 QTranslator aqua_trans(0);
189 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
191 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
193 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
195 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
198 app.installTranslator(&aqua_trans);
201 using namespace lyx::graphics;
203 Image::newImage = boost::bind(&QLImage::newImage);
204 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
206 // needs to be done before reading lyxrc
207 lyxrc.dpi = getDPI();
209 LoaderQueue::setPriority(10,100);
211 LyX::ref().exec2(argc, argv);
219 void start(string const & batch, vector<string> const & files,
220 unsigned int width, unsigned int height, int posx, int posy)
222 // this can't be done before because it needs the Languages object
225 boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
226 LyX::ref().addLyXView(view_ptr);
228 QtView & view = *view_ptr.get();
230 if (posx != -1 && posy != -1)
231 view.move(QPoint(posx, posy));
236 // FIXME: some code below needs moving
238 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
239 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
240 os::internal_path(package().temp_dir() + "/lyxsocket"));
242 for_each(files.begin(), files.end(),
243 bind(&BufferView::loadLyXFile, view.view(), _1, true));
245 // handle the batch commands the user asked for
246 if (!batch.empty()) {
247 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
259 // This is the ONLY place where processEvents may be called.
260 // During screen update/ redraw, this method is disabled to
261 // prevent keyboard events being handed to the LyX core, where
262 // they could cause re-entrant calls to screen update.
263 #if QT_VERSION >= 0x030100
264 qApp->processEvents(QEventLoop::ExcludeUserInput);
269 void exit(int status)
273 // we cannot call QApplication::exit(status) - that could return us
274 // into a static dialog return in the lyx code (for example,
275 // load autosave file QMessageBox. We have to just get the hell
282 FuncStatus getStatus(FuncRequest const & ev)
286 case LFUN_DIALOG_SHOW:
287 if (ev.argument == "preamble")
290 case LFUN_TOOLTIPS_TOGGLE:
301 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
303 QColor const & qcol = lcolorcache.get(col);
304 if (!qcol.isValid()) {
310 rgbcol.r = qcol.red();
311 rgbcol.g = qcol.green();
312 rgbcol.b = qcol.blue();
317 string const hexname(LColor_color col)
319 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
323 void update_color(LColor_color)
325 // FIXME: Bleh, can't we just clear them all at once ?
336 bool font_available(LyXFont const & font)
338 return fontloader.available(font);
342 void register_socket_callback(int fd, boost::function<void()> func)
344 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
348 void unregister_socket_callback(int fd)
350 socket_callbacks.erase(fd);
354 string const roman_font_name()
360 font.setStyleHint(QFont::Serif);
361 font.setFamily("serif");
363 return fromqstr(QFontInfo(font).family());
367 string const sans_font_name()
373 font.setStyleHint(QFont::SansSerif);
374 font.setFamily("sans");
376 return fromqstr(QFontInfo(font).family());
380 string const typewriter_font_name()
386 font.setStyleHint(QFont::TypeWriter);
387 font.setFamily("monospace");
389 return fromqstr(QFontInfo(font).family());
392 }; // namespace lyx_gui