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::GuiImplementation;
63 using lyx::frontend::GuiView;
64 using lyx::frontend::Application;
66 namespace os = lyx::support::os;
68 using boost::shared_ptr;
70 #ifndef CXX_GLOBAL_CSTD
78 // FIXME: wrong place !
79 LyXServer * lyxserver;
80 LyXServerSocket * lyxsocket;
82 lyx::frontend::Application * theApp;
89 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
92 map<int, shared_ptr<socket_callback> > socket_callbacks;
105 extern void initEncodings();
111 void exec(int & argc, char * argv[])
114 FIXME : Abdel 29/05/2006 (younes.a@free.fr)
115 reorganize this code. In particular make sure that this
116 advice from Qt documentation is respected:
118 Since the QApplication object does so much initialization, it
119 must be created before any other objects related to the user
120 interface are created.
122 Right now this is not the case, I suspect that a number of global variables
123 contains Qt object that are initialized before the passage through
124 parse_init(). This might also explain the message displayed by Qt
125 that caused the hanging:
127 QObject::killTimer: timers cannot be stopped from another thread
130 // Force adding of font path _before_ QApplication is initialized
131 FontLoader::initFontPath();
134 static Application app(argc, argv);
136 Application app(argc, argv);
142 // install translation file for Qt built-in dialogs
143 // These are only installed since Qt 3.2.x
144 QTranslator qt_trans;
145 QString language_name = QString("qt_") + QLocale::system().name();
146 language_name.truncate(5);
147 if (qt_trans.load(language_name,
148 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
150 qApp->installTranslator(&qt_trans);
151 // even if the language calls for RtL, don't do that
152 qApp->setLayoutDirection(Qt::LeftToRight);
154 << "Successfully installed Qt translations for locale "
155 << fromqstr(language_name) << std::endl;
158 << "Could not find Qt translations for locale "
159 << fromqstr(language_name) << std::endl;
162 // These translations are meant to break Qt/Mac menu merging
163 // algorithm on some entries. It lists the menu names that
164 // should not be moved to the LyX menu
165 QTranslator aqua_trans(0);
166 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
168 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
170 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
172 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
175 qApp->installTranslator(&aqua_trans);
178 using namespace lyx::graphics;
180 Image::newImage = boost::bind(&QLImage::newImage);
181 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
183 // needs to be done before reading lyxrc
184 lyxrc.dpi = getDPI();
186 LoaderQueue::setPriority(10,100);
188 LyX::ref().exec2(argc, argv);
196 void start(string const & batch, vector<string> const & files,
197 unsigned int width, unsigned int height, int posx, int posy, bool maximize)
199 // this can't be done before because it needs the Languages object
202 int view_id = theApp->gui().newView(width, height);
203 GuiView & view = static_cast<GuiView &> (theApp->gui().view(view_id));
205 // FIXME: for now we assume that there is only one LyXView with id = 0.
206 int workArea_id_ = theApp->gui().newWorkArea(width, height, 0);
207 //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
209 LyX::ref().addLyXView(&view);
213 // only true when the -geometry option was NOT used
214 if (width != 0 && height != 0) {
215 if (posx != -1 && posy != -1) {
217 // FIXME: use only setGeoemtry when Trolltech has
218 // fixed the qt4/X11 bug
219 view.setGeometry(posx, posy,width, height);
221 view.resize(width, height);
222 view.move(posx, posy);
225 view.resize(width, height);
229 view.setWindowState(Qt::WindowMaximized);
234 // FIXME: some code below needs moving
236 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
237 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
238 os::internal_path(package().temp_dir() + "/lyxsocket"));
240 for_each(files.begin(), files.end(),
241 bind(&BufferView::loadLyXFile, view.view(), _1, true));
243 // handle the batch commands the user asked for
244 if (!batch.empty()) {
245 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
257 // This is the ONLY place where processEvents may be called.
258 // During screen update/ redraw, this method is disabled to
259 // prevent keyboard events being handed to the LyX core, where
260 // they could cause re-entrant calls to screen update.
261 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
265 void exit(int status)
269 // we cannot call QApplication::exit(status) - that could return us
270 // into a static dialog return in the lyx code (for example,
271 // load autosave file QMessageBox. We have to just get the hell
278 FuncStatus getStatus(FuncRequest const & ev)
282 case LFUN_DIALOG_SHOW:
283 if (ev.argument == "preamble")
286 case LFUN_TOOLTIPS_TOGGLE:
297 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
299 QColor const & qcol = lcolorcache.get(col);
300 if (!qcol.isValid()) {
306 rgbcol.r = qcol.red();
307 rgbcol.g = qcol.green();
308 rgbcol.b = qcol.blue();
313 string const hexname(LColor_color col)
315 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
319 void update_color(LColor_color)
321 // FIXME: Bleh, can't we just clear them all at once ?
328 theApp->fontLoader().update();
332 bool font_available(LyXFont const & font)
334 return theApp->fontLoader().available(font);
338 void register_socket_callback(int fd, boost::function<void()> func)
340 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
344 void unregister_socket_callback(int fd)
346 socket_callbacks.erase(fd);
350 string const roman_font_name()
356 font.setStyleHint(QFont::Serif);
357 font.setFamily("serif");
359 return fromqstr(QFontInfo(font).family());
363 string const sans_font_name()
369 font.setStyleHint(QFont::SansSerif);
370 font.setFamily("sans");
372 return fromqstr(QFontInfo(font).family());
376 string const typewriter_font_name()
382 font.setStyleHint(QFont::TypeWriter);
383 font.setFamily("monospace");
385 return fromqstr(QFontInfo(font).family());
388 }; // namespace lyx_gui