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 int 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 return LyX::ref().exec2(argc, argv);
236 LyXView* create_view(unsigned int width, unsigned int height, int posx, int posy,
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);
265 int start(LyXView * view, string const & batch)
267 // FIXME: some code below needs moving
269 lyxserver = new LyXServer(&view->getLyXFunc(), lyxrc.lyxpipes);
270 lyxsocket = new LyXServerSocket(&view->getLyXFunc(),
271 os::internal_path(package().temp_dir() + "/lyxsocket"));
273 // handle the batch commands the user asked for
274 if (!batch.empty()) {
275 view->getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
278 int const status = qApp->exec();
288 // This is the ONLY place where processEvents may be called.
289 // During screen update/ redraw, this method is disabled to
290 // prevent keyboard events being handed to the LyX core, where
291 // they could cause re-entrant calls to screen update.
292 #if QT_VERSION >= 0x030100
293 qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
298 void exit(int status)
301 QApplication::exit(status);
305 FuncStatus getStatus(FuncRequest const & ev)
309 case LFUN_TOOLTIPS_TOGGLE:
320 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
322 QColor const & qcol = lcolorcache.get(col);
323 if (!qcol.isValid()) {
329 rgbcol.r = qcol.red();
330 rgbcol.g = qcol.green();
331 rgbcol.b = qcol.blue();
336 string const hexname(LColor_color col)
338 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
342 void update_color(LColor_color)
344 // FIXME: Bleh, can't we just clear them all at once ?
355 bool font_available(LyXFont const & font)
357 return fontloader.available(font);
361 void register_socket_callback(int fd, boost::function<void()> func)
363 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
367 void unregister_socket_callback(int fd)
369 socket_callbacks.erase(fd);
373 string const roman_font_name()
379 font.setStyleHint(QFont::Serif);
380 font.setFamily("serif");
382 return fromqstr(QFontInfo(font).family());
386 string const sans_font_name()
392 font.setStyleHint(QFont::SansSerif);
393 font.setFamily("sans");
395 return fromqstr(QFontInfo(font).family());
399 string const typewriter_font_name()
405 font.setStyleHint(QFont::TypeWriter);
406 font.setFamily("monospace");
408 return fromqstr(QFontInfo(font).family());
411 }; // namespace lyx_gui