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>
43 #include "GuiImplementation.h"
45 #include "lcolorcache.h"
46 #include "qfont_loader.h"
48 #include "qt_helpers.h"
49 #include "socket_callback.h"
52 #include <Carbon/Carbon.h>
55 #include <qapplication.h>
56 #if QT_VERSION >= 0x030100
57 #include <qeventloop.h>
59 #include <qpaintdevicemetrics.h>
60 #include <qtranslator.h>
61 #include <qtextcodec.h>
63 using lyx::support::ltrim;
64 using lyx::support::package;
66 using lyx::frontend::Gui;
67 using lyx::frontend::GuiImplementation;
68 using lyx::frontend::QtView;
70 namespace os = lyx::support::os;
72 using boost::shared_ptr;
74 #ifndef CXX_GLOBAL_CSTD
83 extern BufferList bufferlist;
85 // FIXME: wrong place !
86 LyXServer * lyxserver;
87 LyXServerSocket * lyxsocket;
94 QPaintDeviceMetrics pdm(&w);
95 return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
98 map<int, shared_ptr<socket_callback> > socket_callbacks;
111 extern void initEncodings();
114 extern bool lyxX11EventFilter(XEvent * xev);
119 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
123 class LQApplication : public QApplication
126 LQApplication(int & argc, char ** argv);
128 Gui & gui() { return gui_; }
131 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
134 bool macEventFilter(EventRef event);
139 GuiImplementation gui_;
143 LQApplication::LQApplication(int & argc, char ** argv)
144 : QApplication(argc, argv)
147 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
148 NewAEEventHandlerUPP(handleOpenDocuments),
155 bool LQApplication::macEventFilter(EventRef event)
157 if (GetEventClass(event) == kEventClassAppleEvent) {
158 EventRecord eventrec;
159 ConvertEventRefToEventRecord(event, &eventrec);
160 AEProcessAppleEvent(&eventrec);
168 LQApplication * theApp;
175 void exec(int & argc, char * argv[])
177 // Force adding of font path _before_ QApplication is initialized
178 FontLoader::initFontPath();
180 LQApplication app(argc, argv);
183 #if QT_VERSION >= 0x030200
184 // install translation file for Qt built-in dialogs
185 // These are only installed since Qt 3.2.x
186 QTranslator qt_trans(0);
187 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
188 qInstallPathTranslations())) {
189 qApp->installTranslator(&qt_trans);
190 // even if the language calls for RtL, don't do that
191 qApp->setReverseLayout(false);
193 << "Successfully installed Qt translations for locale "
194 << QTextCodec::locale() << std::endl;
197 << "Could not find Qt translations for locale "
198 << QTextCodec::locale() << std::endl;
202 // These translations are meant to break Qt/Mac menu merging
203 // algorithm on some entries. It lists the menu names that
204 // should not be moved to the LyX menu
205 QTranslator aqua_trans(0);
206 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
208 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
210 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
212 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
215 qApp->installTranslator(&aqua_trans);
218 using namespace lyx::graphics;
220 Image::newImage = boost::bind(&QLImage::newImage);
221 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
223 // needs to be done before reading lyxrc
224 lyxrc.dpi = getDPI();
226 LoaderQueue::setPriority(10,100);
228 LyX::ref().exec2(argc, argv);
236 void start(string const & batch, vector<string> const & files,
237 unsigned int width, unsigned int height, int posx, int posy, bool maximize)
239 // this can't be done before because it needs the Languages object
242 int view_id = theApp->gui().newView(width, height);
243 QtView & view = static_cast<QtView &> (theApp->gui().view(view_id));
244 theApp->gui().newWorkArea(width, height, 0);
246 LyX::ref().addLyXView(&view);
250 if (width != 0 && height != 0) {
251 view.initFloatingGeometry(QRect(posx, posy, width, height));
252 view.resize(width, height);
253 if (posx != -1 && posy != -1)
254 view.move(posx, posy);
257 view.setWindowState(Qt::WindowMaximized);
261 // FIXME: some code below needs moving
263 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
264 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
265 os::internal_path(package().temp_dir() + "/lyxsocket"));
267 for_each(files.begin(), files.end(),
268 bind(&BufferView::loadLyXFile, view.view(), _1, true));
270 // handle the batch commands the user asked for
271 if (!batch.empty()) {
272 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
284 // This is the ONLY place where processEvents may be called.
285 // During screen update/ redraw, this method is disabled to
286 // prevent keyboard events being handed to the LyX core, where
287 // they could cause re-entrant calls to screen update.
288 #if QT_VERSION >= 0x030100
289 qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
294 void exit(int status)
298 // we cannot call QApplication::exit(status) - that could return us
299 // into a static dialog return in the lyx code (for example,
300 // load autosave file QMessageBox. We have to just get the hell
307 FuncStatus getStatus(FuncRequest const & ev)
311 case LFUN_DIALOG_SHOW:
312 if (ev.argument == "preamble")
315 case LFUN_TOOLTIPS_TOGGLE:
326 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
328 QColor const & qcol = lcolorcache.get(col);
329 if (!qcol.isValid()) {
335 rgbcol.r = qcol.red();
336 rgbcol.g = qcol.green();
337 rgbcol.b = qcol.blue();
342 string const hexname(LColor_color col)
344 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
348 void update_color(LColor_color)
350 // FIXME: Bleh, can't we just clear them all at once ?
361 bool font_available(LyXFont const & font)
363 return fontloader.available(font);
367 void register_socket_callback(int fd, boost::function<void()> func)
369 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
373 void unregister_socket_callback(int fd)
375 socket_callbacks.erase(fd);
379 string const roman_font_name()
385 font.setStyleHint(QFont::Serif);
386 font.setFamily("serif");
388 return fromqstr(QFontInfo(font).family());
392 string const sans_font_name()
398 font.setStyleHint(QFont::SansSerif);
399 font.setFamily("sans");
401 return fromqstr(QFontInfo(font).family());
405 string const typewriter_font_name()
411 font.setStyleHint(QFont::TypeWriter);
412 font.setFamily("monospace");
414 return fromqstr(QFontInfo(font).family());
417 }; // namespace lyx_gui