3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
19 // FIXME: move this stuff out again
20 #include "bufferlist.h"
21 #include "BufferView.h"
23 #include "funcrequest.h"
26 #include "LyXAction.h"
29 #include "lyxserver.h"
30 #include "lyxsocket.h"
32 #include "graphics/LoaderQueue.h"
34 #include "support/lstrings.h"
35 #include "support/os.h"
36 #include "support/package.h"
39 #include <boost/bind.hpp>
40 #include <boost/shared_ptr.hpp>
43 #include "ColorCache.h"
44 #include "FontLoader.h"
46 #include "qt_helpers.h"
47 #include "socket_callback.h"
48 #include "Application.h"
50 #include <QApplication>
52 #include <QTranslator>
55 #include <QLibraryInfo>
59 using lyx::support::ltrim;
60 using lyx::support::package;
62 using lyx::frontend::GuiView;
63 using lyx::frontend::Application;
65 namespace os = lyx::support::os;
67 using boost::shared_ptr;
69 #ifndef CXX_GLOBAL_CSTD
77 // FIXME: wrong place !
78 LyXServer * lyxserver;
79 LyXServerSocket * lyxsocket;
86 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
89 map<int, shared_ptr<socket_callback> > socket_callbacks;
102 extern void initEncodings();
108 void exec(int & argc, char * argv[])
111 FIXME : Abdel 29/05/2006 (younes.a@free.fr)
112 reorganize this code. In particular make sure that this
113 advice from Qt documentation is respected:
115 Since the QApplication object does so much initialization, it
116 must be created before any other objects related to the user
117 interface are created.
119 Right now this is not the case, I suspect that a number of global variables
120 contains Qt object that are initialized before the passage through
121 parse_init(). This might also explain the message displayed by Qt
122 that caused the hanging:
124 QObject::killTimer: timers cannot be stopped from another thread
127 // Force adding of font path _before_ QApplication is initialized
128 FontLoader::initFontPath();
131 static Application app(argc, argv);
133 Application app(argc, argv);
137 // install translation file for Qt built-in dialogs
138 // These are only installed since Qt 3.2.x
139 QTranslator qt_trans;
140 QString language_name = QString("qt_") + QLocale::system().name();
141 language_name.truncate(5);
142 if (qt_trans.load(language_name,
143 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
145 qApp->installTranslator(&qt_trans);
146 // even if the language calls for RtL, don't do that
147 qApp->setLayoutDirection(Qt::LeftToRight);
149 << "Successfully installed Qt translations for locale "
150 << fromqstr(language_name) << std::endl;
153 << "Could not find Qt translations for locale "
154 << fromqstr(language_name) << std::endl;
157 // These translations are meant to break Qt/Mac menu merging
158 // algorithm on some entries. It lists the menu names that
159 // should not be moved to the LyX menu
160 QTranslator aqua_trans(0);
161 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
163 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
165 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
167 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
170 qApp->installTranslator(&aqua_trans);
173 using namespace lyx::graphics;
175 Image::newImage = boost::bind(&QLImage::newImage);
176 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
178 // needs to be done before reading lyxrc
179 lyxrc.dpi = getDPI();
181 LoaderQueue::setPriority(10,100);
183 LyX::ref().exec2(argc, argv);
191 void start(string const & batch, vector<string> const & files,
192 unsigned int width, unsigned int height, int posx, int posy, bool maximize)
194 // this can't be done before because it needs the Languages object
197 boost::shared_ptr<GuiView> view_ptr(new GuiView);
199 LyX::ref().addLyXView(view_ptr);
201 GuiView & view = *view_ptr.get();
205 // only true when the -geometry option was NOT used
206 if (width != -1 && height != -1) {
207 if (posx != -1 && posy != -1) {
209 // FIXME: use only setGeoemtry when Trolltech has
210 // fixed the qt4/X11 bug
211 view.setGeometry(posx, posy,width, height);
213 view.resize(width, height);
214 view.move(posx, posy);
217 view.resize(width, height);
221 view.setWindowState(Qt::WindowMaximized);
226 // FIXME: some code below needs moving
228 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
229 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
230 os::internal_path(package().temp_dir() + "/lyxsocket"));
232 for_each(files.begin(), files.end(),
233 bind(&BufferView::loadLyXFile, view.view(), _1, true));
235 // handle the batch commands the user asked for
236 if (!batch.empty()) {
237 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
249 // This is the ONLY place where processEvents may be called.
250 // During screen update/ redraw, this method is disabled to
251 // prevent keyboard events being handed to the LyX core, where
252 // they could cause re-entrant calls to screen update.
253 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
257 void exit(int status)
261 // we cannot call QApplication::exit(status) - that could return us
262 // into a static dialog return in the lyx code (for example,
263 // load autosave file QMessageBox. We have to just get the hell
270 FuncStatus getStatus(FuncRequest const & ev)
274 case LFUN_DIALOG_SHOW:
275 if (ev.argument == "preamble")
278 case LFUN_TOOLTIPS_TOGGLE:
289 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
291 QColor const & qcol = lcolorcache.get(col);
292 if (!qcol.isValid()) {
298 rgbcol.r = qcol.red();
299 rgbcol.g = qcol.green();
300 rgbcol.b = qcol.blue();
305 string const hexname(LColor_color col)
307 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
311 void update_color(LColor_color)
313 // FIXME: Bleh, can't we just clear them all at once ?
324 bool font_available(LyXFont const & font)
326 return fontloader.available(font);
330 void register_socket_callback(int fd, boost::function<void()> func)
332 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
336 void unregister_socket_callback(int fd)
338 socket_callbacks.erase(fd);
342 string const roman_font_name()
348 font.setStyleHint(QFont::Serif);
349 font.setFamily("serif");
351 return fromqstr(QFontInfo(font).family());
355 string const sans_font_name()
361 font.setStyleHint(QFont::SansSerif);
362 font.setFamily("sans");
364 return fromqstr(QFontInfo(font).family());
368 string const typewriter_font_name()
374 font.setStyleHint(QFont::TypeWriter);
375 font.setFamily("monospace");
377 return fromqstr(QFontInfo(font).family());
380 }; // namespace lyx_gui