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.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::QtView;
69 namespace os = lyx::support::os;
71 using boost::shared_ptr;
73 #ifndef CXX_GLOBAL_CSTD
82 extern BufferList bufferlist;
89 QPaintDeviceMetrics pdm(&w);
90 return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
93 map<int, shared_ptr<socket_callback> > socket_callbacks;
97 // FIXME: wrong place !
98 LyXServer * lyxserver;
99 LyXServerSocket * lyxsocket;
102 extern void initEncodings();
105 extern bool lyxX11EventFilter(XEvent * xev);
109 extern bool macEventFilter(EventRef event);
111 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
115 class LQApplication : public QApplication
118 LQApplication(int & argc, char ** argv);
121 bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
124 bool macEventFilter(EventRef event);
129 LQApplication::LQApplication(int & argc, char ** argv)
130 : QApplication(argc, argv)
133 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
134 NewAEEventHandlerUPP(handleOpenDocuments),
140 LQApplication::~LQApplication()
145 bool LQApplication::macEventFilter(EventRef event)
147 if (GetEventClass(event) == kEventClassAppleEvent) {
148 EventRecord eventrec;
149 ConvertEventRefToEventRecord(event, &eventrec);
150 AEProcessAppleEvent(&eventrec);
163 void parse_init(int & argc, char * argv[])
165 // Force adding of font path _before_ QApplication is initialized
166 FontLoader::initFontPath();
168 static LQApplication app(argc, argv);
170 #if QT_VERSION >= 0x030200
171 // install translation file for Qt built-in dialogs
172 // These are only installed since Qt 3.2.x
173 static QTranslator qt_trans(0);
174 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
175 qInstallPathTranslations())) {
176 app.installTranslator(&qt_trans);
177 // even if the language calls for RtL, don't do that
178 app.setReverseLayout(false);
180 << "Successfully installed Qt translations for locale "
181 << QTextCodec::locale() << std::endl;
184 << "Could not find Qt translations for locale "
185 << QTextCodec::locale() << std::endl;
189 // These translations are meant to break Qt/Mac menu merging
190 // algorithm on some entries. It lists the menu names that
191 // should not be moved to the LyX menu
192 static QTranslator aqua_trans(0);
193 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
195 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
197 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
199 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
202 app.installTranslator(&aqua_trans);
205 using namespace lyx::graphics;
207 Image::newImage = boost::bind(&QLImage::newImage);
208 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
210 // needs to be done before reading lyxrc
211 lyxrc.dpi = getDPI();
213 LoaderQueue::setPriority(10,100);
221 void start(string const & batch, vector<string> const & files)
223 // this can't be done before because it needs the Languages object
227 unsigned int width = 690;
228 unsigned int height = 510;
230 if (lyxrc.geometry_width != 0 && lyxrc.geometry_height != 0 ) {
231 width = lyxrc.geometry_width;
232 height = lyxrc.geometry_height;
234 // if lyxrc returns (0,0), then use session info
236 string val = LyX::ref().session().loadSessionInfo("WindowWidth");
238 width = convert<unsigned int>(val);
239 val = LyX::ref().session().loadSessionInfo("WindowHeight");
241 height = convert<unsigned int>(val);
244 boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
245 LyX::ref().addLyXView(view_ptr);
247 QtView & view = *view_ptr.get();
249 // if user wants to restore window position
250 if (lyxrc.geometry_xysaved) {
251 QPoint p = view.pos();
252 string val = LyX::ref().session().loadSessionInfo("WindowPosX");
254 p.setX(convert<unsigned int>(val));
255 val = LyX::ref().session().loadSessionInfo("WindowPosY");
257 p.setY(convert<unsigned int>(val));
263 // FIXME: some code below needs moving
265 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
266 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
267 os::internal_path(package().temp_dir() + "/lyxsocket"));
269 for_each(files.begin(), files.end(),
270 bind(&BufferView::loadLyXFile, view.view(), _1, true));
272 // handle the batch commands the user asked for
273 if (!batch.empty()) {
274 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
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);
304 // we cannot call qApp->exit(0) - that could return us
305 // into a static dialog return in the lyx code (for example,
306 // load autosave file QMessageBox. We have to just get the hell
313 FuncStatus getStatus(FuncRequest const & ev)
317 case LFUN_DIALOG_SHOW:
318 if (ev.argument == "preamble")
321 case LFUN_TOOLTIPS_TOGGLE:
332 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
334 QColor const & qcol = lcolorcache.get(col);
335 if (!qcol.isValid()) {
341 rgbcol.r = qcol.red();
342 rgbcol.g = qcol.green();
343 rgbcol.b = qcol.blue();
348 string const hexname(LColor_color col)
350 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
354 void update_color(LColor_color)
356 // FIXME: Bleh, can't we just clear them all at once ?
367 bool font_available(LyXFont const & font)
369 return fontloader.available(font);
373 void register_socket_callback(int fd, boost::function<void()> func)
375 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
379 void unregister_socket_callback(int fd)
381 socket_callbacks.erase(fd);
385 string const roman_font_name()
391 font.setStyleHint(QFont::Serif);
392 font.setFamily("serif");
394 return fromqstr(QFontInfo(font).family());
398 string const sans_font_name()
404 font.setStyleHint(QFont::SansSerif);
405 font.setFamily("sans");
407 return fromqstr(QFontInfo(font).family());
411 string const typewriter_font_name()
417 font.setStyleHint(QFont::TypeWriter);
418 font.setFamily("monospace");
420 return fromqstr(QFontInfo(font).family());
423 }; // namespace lyx_gui