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"
30 #include "graphics/LoaderQueue.h"
32 #include "support/lstrings.h"
33 #include "support/convert.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 using lyx::support::ltrim;
62 using lyx::support::package;
64 using lyx::frontend::QtView;
66 namespace os = lyx::support::os;
68 using boost::shared_ptr;
70 #ifndef CXX_GLOBAL_CSTD
79 extern BufferList bufferlist;
86 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
89 map<int, shared_ptr<socket_callback> > socket_callbacks;
93 // FIXME: wrong place !
94 LyXServer * lyxserver;
95 LyXServerSocket * lyxsocket;
98 extern void initEncodings();
101 extern bool lyxX11EventFilter(XEvent * xev);
105 extern bool macEventFilter(EventRef event);
107 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
111 class LQApplication : public QApplication
114 LQApplication(int & argc, char ** argv);
117 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
120 bool macEventFilter(EventRef event);
125 LQApplication::LQApplication(int & argc, char ** argv)
126 : QApplication(argc, argv)
129 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
130 NewAEEventHandlerUPP(handleOpenDocuments),
136 LQApplication::~LQApplication()
141 bool LQApplication::macEventFilter(EventRef event)
143 if (GetEventClass(event) == kEventClassAppleEvent) {
144 EventRecord eventrec;
145 ConvertEventRefToEventRecord(event, &eventrec);
146 AEProcessAppleEvent(&eventrec);
157 LQApplication * app = 0;
166 void parse_init(int & argc, char * argv[])
168 // Force adding of font path _before_ QApplication is initialized
169 FontLoader::initFontPath();
173 static QApplication win_app(argc, argv);
175 app = new LQApplication(argc, argv);
178 // install translation file for Qt built-in dialogs
179 // These are only installed since Qt 3.2.x
180 static QTranslator qt_trans(0);
181 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
182 qInstallPathTranslations())) {
183 qApp->installTranslator(&qt_trans);
184 // even if the language calls for RtL, don't do that
185 qApp->setReverseLayout(false);
187 << "Successfully installed Qt translations for locale "
188 << QTextCodec::locale() << std::endl;
191 << "Could not find Qt translations for locale "
192 << QTextCodec::locale() << std::endl;
195 // These translations are meant to break Qt/Mac menu merging
196 // algorithm on some entries. It lists the menu names that
197 // should not be moved to the LyX menu
198 static QTranslator aqua_trans(0);
199 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
201 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
203 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
205 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
208 app.installTranslator(&aqua_trans);
211 using namespace lyx::graphics;
213 Image::newImage = boost::bind(&QLImage::newImage);
214 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
216 // needs to be done before reading lyxrc
217 lyxrc.dpi = getDPI();
219 LoaderQueue::setPriority(10,100);
227 void start(string const & batch, vector<string> const & files)
229 // this can't be done before because it needs the Languages object
233 unsigned int width = 690;
234 unsigned int height = 510;
236 if (lyxrc.geometry_width != 0 && lyxrc.geometry_height != 0 ) {
237 width = lyxrc.geometry_width;
238 height = lyxrc.geometry_height;
240 // if lyxrc returns (0,0), then use session info
242 string val = LyX::ref().session().loadSessionInfo("WindowWidth");
244 width = convert<unsigned int>(val);
245 val = LyX::ref().session().loadSessionInfo("WindowHeight");
247 height = convert<unsigned int>(val);
250 boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
251 LyX::ref().addLyXView(view_ptr);
253 QtView & view = *view_ptr.get();
255 // if user wants to restore window position
256 if (lyxrc.geometry_xysaved) {
257 QPoint p = view.pos();
258 string val = LyX::ref().session().loadSessionInfo("WindowPosX");
260 p.setX(convert<unsigned int>(val));
261 val = LyX::ref().session().loadSessionInfo("WindowPosY");
263 p.setY(convert<unsigned int>(val));
269 // FIXME: some code below needs moving
271 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
272 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
273 os::internal_path(package().temp_dir() + "/lyxsocket"));
275 for_each(files.begin(), files.end(),
276 bind(&BufferView::loadLyXFile, view.view(), _1, true));
278 // handle the batch commands the user asked for
279 if (!batch.empty()) {
280 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
295 // This is the ONLY place where processEvents may be called.
296 // During screen update/ redraw, this method is disabled to
297 // prevent keyboard events being handed to the LyX core, where
298 // they could cause re-entrant calls to screen update.
299 #if QT_VERSION >= 0x030100
300 qApp->processEvents(QEventLoop::ExcludeUserInput);
311 // we cannot call qApp->exit(0) - that could return us
312 // into a static dialog return in the lyx code (for example,
313 // load autosave file QMessageBox. We have to just get the hell
320 FuncStatus getStatus(FuncRequest const & ev)
324 case LFUN_DIALOG_SHOW:
325 if (ev.argument == "preamble")
328 case LFUN_TOOLTIPS_TOGGLE:
339 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
341 QColor const & qcol = lcolorcache.get(col);
342 if (!qcol.isValid()) {
348 rgbcol.r = qcol.red();
349 rgbcol.g = qcol.green();
350 rgbcol.b = qcol.blue();
355 string const hexname(LColor_color col)
357 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
361 void update_color(LColor_color)
363 // FIXME: Bleh, can't we just clear them all at once ?
374 bool font_available(LyXFont const & font)
376 return fontloader.available(font);
380 void register_socket_callback(int fd, boost::function<void()> func)
382 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
386 void unregister_socket_callback(int fd)
388 socket_callbacks.erase(fd);
392 string const roman_font_name()
398 font.setStyleHint(QFont::Serif);
399 font.setFamily("serif");
401 return fromqstr(QFontInfo(font).family());
405 string const sans_font_name()
411 font.setStyleHint(QFont::SansSerif);
412 font.setFamily("sans");
414 return fromqstr(QFontInfo(font).family());
418 string const typewriter_font_name()
424 font.setStyleHint(QFont::TypeWriter);
425 font.setFamily("monospace");
427 return fromqstr(QFontInfo(font).family());
430 }; // namespace lyx_gui