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.
18 // FIXME: move this stuff out again
19 #include "bufferlist.h"
20 #include "BufferView.h"
22 #include "funcrequest.h"
25 #include "LyXAction.h"
28 #include "lyxserver.h"
29 #include "lyxsocket.h"
31 #include "graphics/LoaderQueue.h"
33 #include "support/lstrings.h"
34 #include "support/os.h"
35 #include "support/package.h"
38 // Dear Lord, deliver us from Evil, aka the Qt headers
39 // Qt defines a macro 'signals' that clashes with a boost namespace.
40 // All is well if the namespace is visible first.
41 #include <boost/signal.hpp> // FIXME: Is this needed? (Lgb)
42 #include <boost/bind.hpp>
43 #include <boost/shared_ptr.hpp>
46 #include "lcolorcache.h"
47 #include "qfont_loader.h"
49 #include "qt_helpers.h"
50 #include "socket_callback.h"
53 #include <Carbon/Carbon.h>
56 #include <QApplication>
58 #include <QTranslator>
61 #include <QLibraryInfo>
63 using lyx::support::ltrim;
64 using lyx::support::package;
66 using lyx::frontend::QtView;
68 namespace os = lyx::support::os;
70 using boost::shared_ptr;
72 #ifndef CXX_GLOBAL_CSTD
81 extern BufferList bufferlist;
83 // FIXME: wrong place !
84 LyXServer * lyxserver;
85 LyXServerSocket * lyxsocket;
92 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
95 map<int, shared_ptr<socket_callback> > socket_callbacks;
108 extern void initEncodings();
111 extern bool lyxX11EventFilter(XEvent * xev);
115 extern bool macEventFilter(EventRef event);
117 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
121 class LQApplication : public QApplication
124 LQApplication(int & argc, char ** argv);
126 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
129 bool macEventFilter(EventRef event);
134 LQApplication::LQApplication(int & argc, char ** argv)
135 : QApplication(argc, argv)
138 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
139 NewAEEventHandlerUPP(handleOpenDocuments),
146 bool LQApplication::macEventFilter(EventRef event)
148 if (GetEventClass(event) == kEventClassAppleEvent) {
149 EventRecord eventrec;
150 ConvertEventRefToEventRecord(event, &eventrec);
151 AEProcessAppleEvent(&eventrec);
165 void exec(int & argc, char * argv[])
167 // Force adding of font path _before_ QApplication is initialized
168 FontLoader::initFontPath();
171 static LQApplication app(argc, argv);
173 LQApplication app(argc, argv);
176 // install translation file for Qt built-in dialogs
177 // These are only installed since Qt 3.2.x
178 QTranslator qt_trans;
179 QString language_name = QString("qt_") + QLocale::system().name();
180 language_name.truncate(5);
181 if (qt_trans.load(language_name,
182 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
184 qApp->installTranslator(&qt_trans);
185 // even if the language calls for RtL, don't do that
186 qApp->setLayoutDirection(Qt::LeftToRight);
188 << "Successfully installed Qt translations for locale "
189 << fromqstr(language_name) << std::endl;
192 << "Could not find Qt translations for locale "
193 << fromqstr(language_name) << std::endl;
196 // These translations are meant to break Qt/Mac menu merging
197 // algorithm on some entries. It lists the menu names that
198 // should not be moved to the LyX menu
199 QTranslator aqua_trans(0);
200 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
202 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
204 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
206 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
209 qApp->installTranslator(&aqua_trans);
212 using namespace lyx::graphics;
214 Image::newImage = boost::bind(&QLImage::newImage);
215 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
217 // needs to be done before reading lyxrc
218 lyxrc.dpi = getDPI();
220 LoaderQueue::setPriority(10,100);
222 LyX::ref().exec2(argc, argv);
230 void start(string const & batch, vector<string> const & files,
231 unsigned int width, unsigned int height, int posx, int posy, bool maximize)
233 // this can't be done before because it needs the Languages object
236 boost::shared_ptr<QtView> view_ptr(new QtView(width, height, maximize));
237 LyX::ref().addLyXView(view_ptr);
239 QtView & view = *view_ptr.get();
241 if (posx != -1 && posy != -1)
242 view.move(QPoint(posx, posy));
247 // FIXME: some code below needs moving
249 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
250 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
251 os::internal_path(package().temp_dir() + "/lyxsocket"));
253 for_each(files.begin(), files.end(),
254 bind(&BufferView::loadLyXFile, view.view(), _1, true));
256 // handle the batch commands the user asked for
257 if (!batch.empty()) {
258 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
270 // This is the ONLY place where processEvents may be called.
271 // During screen update/ redraw, this method is disabled to
272 // prevent keyboard events being handed to the LyX core, where
273 // they could cause re-entrant calls to screen update.
274 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
278 void exit(int status)
282 // we cannot call QApplication::exit(status) - that could return us
283 // into a static dialog return in the lyx code (for example,
284 // load autosave file QMessageBox. We have to just get the hell
291 FuncStatus getStatus(FuncRequest const & ev)
295 case LFUN_DIALOG_SHOW:
296 if (ev.argument == "preamble")
299 case LFUN_TOOLTIPS_TOGGLE:
310 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
312 QColor const & qcol = lcolorcache.get(col);
313 if (!qcol.isValid()) {
319 rgbcol.r = qcol.red();
320 rgbcol.g = qcol.green();
321 rgbcol.b = qcol.blue();
326 string const hexname(LColor_color col)
328 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
332 void update_color(LColor_color)
334 // FIXME: Bleh, can't we just clear them all at once ?
345 bool font_available(LyXFont const & font)
347 return fontloader.available(font);
351 void register_socket_callback(int fd, boost::function<void()> func)
353 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
357 void unregister_socket_callback(int fd)
359 socket_callbacks.erase(fd);
363 string const roman_font_name()
369 font.setStyleHint(QFont::Serif);
370 font.setFamily("serif");
372 return fromqstr(QFontInfo(font).family());
376 string const sans_font_name()
382 font.setStyleHint(QFont::SansSerif);
383 font.setFamily("sans");
385 return fromqstr(QFontInfo(font).family());
389 string const typewriter_font_name()
395 font.setStyleHint(QFont::TypeWriter);
396 font.setFamily("monospace");
398 return fromqstr(QFontInfo(font).family());
401 }; // namespace lyx_gui