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>
42 #include "frontends/WorkArea.h"
44 #include "GuiImplementation.h"
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.h>
57 #if QT_VERSION >= 0x030100
58 #include <qeventloop.h>
60 #include <qpaintdevicemetrics.h>
61 #include <qtranslator.h>
62 #include <qtextcodec.h>
64 using lyx::support::ltrim;
65 using lyx::support::package;
67 using lyx::frontend::Gui;
68 using lyx::frontend::GuiImplementation;
69 using lyx::frontend::QtView;
71 namespace os = lyx::support::os;
73 using boost::shared_ptr;
75 #ifndef CXX_GLOBAL_CSTD
84 extern BufferList bufferlist;
86 // FIXME: wrong place !
87 LyXServer * lyxserver;
88 LyXServerSocket * lyxsocket;
95 QPaintDeviceMetrics pdm(&w);
96 return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
99 map<int, shared_ptr<socket_callback> > socket_callbacks;
112 extern void initEncodings();
115 extern bool lyxX11EventFilter(XEvent * xev);
120 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
124 class LQApplication : public QApplication
127 LQApplication(int & argc, char ** argv);
129 Gui & gui() { return gui_; }
132 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
135 bool macEventFilter(EventRef event);
140 GuiImplementation gui_;
144 LQApplication::LQApplication(int & argc, char ** argv)
145 : QApplication(argc, argv)
148 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
149 NewAEEventHandlerUPP(handleOpenDocuments),
156 bool LQApplication::macEventFilter(EventRef event)
158 if (GetEventClass(event) == kEventClassAppleEvent) {
159 EventRecord eventrec;
160 ConvertEventRefToEventRecord(event, &eventrec);
161 AEProcessAppleEvent(&eventrec);
169 LQApplication * theApp;
176 int exec(int & argc, char * argv[])
178 // Force adding of font path _before_ QApplication is initialized
179 FontLoader::initFontPath();
181 LQApplication app(argc, argv);
184 #if QT_VERSION >= 0x030200
185 // install translation file for Qt built-in dialogs
186 // These are only installed since Qt 3.2.x
187 QTranslator qt_trans(0);
188 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
189 qInstallPathTranslations())) {
190 qApp->installTranslator(&qt_trans);
191 // even if the language calls for RtL, don't do that
192 qApp->setReverseLayout(false);
194 << "Successfully installed Qt translations for locale "
195 << QTextCodec::locale() << std::endl;
198 << "Could not find Qt translations for locale "
199 << QTextCodec::locale() << std::endl;
203 // These translations are meant to break Qt/Mac menu merging
204 // algorithm on some entries. It lists the menu names that
205 // should not be moved to the LyX menu
206 QTranslator aqua_trans(0);
207 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
209 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
211 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
213 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
216 qApp->installTranslator(&aqua_trans);
219 using namespace lyx::graphics;
221 Image::newImage = boost::bind(&QLImage::newImage);
222 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
224 // needs to be done before reading lyxrc
225 lyxrc.dpi = getDPI();
227 LoaderQueue::setPriority(10,100);
229 return LyX::ref().exec2(argc, argv);
237 LyXView* create_view(unsigned int width, unsigned int height, int posx, int posy,
240 // this can't be done before because it needs the Languages object
243 int view_id = theApp->gui().newView(width, height);
244 QtView & view = static_cast<QtView &> (theApp->gui().view(view_id));
245 theApp->gui().newWorkArea(width, height, 0);
247 LyX::ref().addLyXView(&view);
251 if (width != 0 && height != 0) {
252 view.initFloatingGeometry(QRect(posx, posy, width, height));
253 view.resize(width, height);
254 if (posx != -1 && posy != -1)
255 view.move(posx, posy);
258 view.setWindowState(Qt::WindowMaximized);
266 int start(LyXView * view, string const & batch)
268 // FIXME: some code below needs moving
270 lyxserver = new LyXServer(&view->getLyXFunc(), lyxrc.lyxpipes);
271 lyxsocket = new LyXServerSocket(&view->getLyXFunc(),
272 os::internal_path(package().temp_dir() + "/lyxsocket"));
274 // handle the batch commands the user asked for
275 if (!batch.empty()) {
276 view->getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
279 int const status = qApp->exec();
289 // This is the ONLY place where processEvents may be called.
290 // During screen update/ redraw, this method is disabled to
291 // prevent keyboard events being handed to the LyX core, where
292 // they could cause re-entrant calls to screen update.
293 #if QT_VERSION >= 0x030100
294 qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
299 void exit(int status)
302 QApplication::exit(status);
306 FuncStatus getStatus(FuncRequest const & ev)
310 case LFUN_TOOLTIPS_TOGGLE:
321 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
323 QColor const & qcol = lcolorcache.get(col);
324 if (!qcol.isValid()) {
330 rgbcol.r = qcol.red();
331 rgbcol.g = qcol.green();
332 rgbcol.b = qcol.blue();
337 string const hexname(LColor_color col)
339 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
343 void update_color(LColor_color)
345 // FIXME: Bleh, can't we just clear them all at once ?
356 bool font_available(LyXFont const & font)
358 return fontloader.available(font);
362 void register_socket_callback(int fd, boost::function<void()> func)
364 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
368 void unregister_socket_callback(int fd)
370 socket_callbacks.erase(fd);
374 string const roman_font_name()
380 font.setStyleHint(QFont::Serif);
381 font.setFamily("serif");
383 return fromqstr(QFontInfo(font).family());
387 string const sans_font_name()
393 font.setStyleHint(QFont::SansSerif);
394 font.setFamily("sans");
396 return fromqstr(QFontInfo(font).family());
400 string const typewriter_font_name()
406 font.setStyleHint(QFont::TypeWriter);
407 font.setFamily("monospace");
409 return fromqstr(QFontInfo(font).family());
412 }; // namespace lyx_gui