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"
33 #include "graphics/LoaderQueue.h"
35 #include "support/lstrings.h"
36 #include "support/os.h"
37 #include "support/package.h"
42 #include "ColorCache.h"
43 #include "FontLoader.h"
45 #include "qt_helpers.h"
46 #include "socket_callback.h"
47 #include "Application.h"
49 #include <QApplication>
51 #include <QTranslator>
54 #include <QLibraryInfo>
56 #include <boost/bind.hpp>
57 #include <boost/shared_ptr.hpp>
60 using lyx::support::ltrim;
61 using lyx::support::package;
63 using lyx::frontend::GuiImplementation;
64 using lyx::frontend::GuiView;
65 using lyx::frontend::Application;
67 namespace os = lyx::support::os;
69 using boost::shared_ptr;
71 #ifndef CXX_GLOBAL_CSTD
79 // FIXME: wrong place !
80 LyXServer * lyxserver;
81 LyXServerSocket * lyxsocket;
83 lyx::frontend::Application * theApp;
90 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
93 map<int, shared_ptr<socket_callback> > socket_callbacks;
106 extern void initEncodings();
112 int exec(int & argc, char * argv[])
115 FIXME : Abdel 29/05/2006 (younes.a@free.fr)
116 reorganize this code. In particular make sure that this
117 advice from Qt documentation is respected:
119 Since the QApplication object does so much initialization, it
120 must be created before any other objects related to the user
121 interface are created.
123 Right now this is not the case, I suspect that a number of global variables
124 contains Qt object that are initialized before the passage through
125 parse_init(). This might also explain the message displayed by Qt
126 that caused the hanging:
128 QObject::killTimer: timers cannot be stopped from another thread
131 // Force adding of font path _before_ QApplication is initialized
132 FontLoader::initFontPath();
135 static Application app(argc, argv);
137 Application app(argc, argv);
143 // install translation file for Qt built-in dialogs
144 // These are only installed since Qt 3.2.x
145 QTranslator qt_trans;
146 QString language_name = QString("qt_") + QLocale::system().name();
147 language_name.truncate(5);
148 if (qt_trans.load(language_name,
149 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
151 qApp->installTranslator(&qt_trans);
152 // even if the language calls for RtL, don't do that
153 qApp->setLayoutDirection(Qt::LeftToRight);
155 << "Successfully installed Qt translations for locale "
156 << fromqstr(language_name) << std::endl;
159 << "Could not find Qt translations for locale "
160 << fromqstr(language_name) << std::endl;
163 // These translations are meant to break Qt/Mac menu merging
164 // algorithm on some entries. It lists the menu names that
165 // should not be moved to the LyX menu
166 QTranslator aqua_trans(0);
167 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
169 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
171 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
173 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
176 qApp->installTranslator(&aqua_trans);
179 using namespace lyx::graphics;
181 Image::newImage = boost::bind(&QLImage::newImage);
182 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
184 // needs to be done before reading lyxrc
185 lyxrc.dpi = getDPI();
187 LoaderQueue::setPriority(10,100);
189 return LyX::ref().exec2(argc, argv);
197 int start(string const & batch, vector<string> const & files,
198 unsigned int width, unsigned int height, int posx, int posy,
201 // this can't be done before because it needs the Languages object
204 int view_id = theApp->gui().newView(width, height);
205 GuiView & view = static_cast<GuiView &> (theApp->gui().view(view_id));
207 // FIXME: for now we assume that there is only one LyXView with id = 0.
208 /*int workArea_id_ =*/ theApp->gui().newWorkArea(width, height, 0);
209 //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
211 LyX::ref().addLyXView(&view);
215 // only true when the -geometry option was NOT used
216 if (width != 0 && height != 0) {
217 if (posx != -1 && posy != -1) {
219 // FIXME: use only setGeoemtry when Trolltech has
220 // fixed the qt4/X11 bug
221 view.setGeometry(posx, posy,width, height);
223 view.resize(width, height);
224 view.move(posx, posy);
227 view.resize(width, height);
231 view.setWindowState(Qt::WindowMaximized);
236 // FIXME: some code below needs moving
238 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
239 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
240 os::internal_path(package().temp_dir() + "/lyxsocket"));
242 for_each(files.begin(), files.end(),
243 bind(&LyXView::loadLyXFile, &view, _1, true));
245 // handle the batch commands the user asked for
246 if (!batch.empty()) {
247 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
250 int const status = qApp->exec();
260 // This is the ONLY place where processEvents may be called.
261 // During screen update/ redraw, this method is disabled to
262 // prevent keyboard events being handed to the LyX core, where
263 // they could cause re-entrant calls to screen update.
264 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
268 void exit(int status)
271 QApplication::exit(status);
275 FuncStatus getStatus(FuncRequest const & ev)
279 case LFUN_TOOLTIPS_TOGGLE:
290 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
292 QColor const & qcol = lcolorcache.get(col);
293 if (!qcol.isValid()) {
299 rgbcol.r = qcol.red();
300 rgbcol.g = qcol.green();
301 rgbcol.b = qcol.blue();
306 string const hexname(LColor_color col)
308 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
312 void update_color(LColor_color)
314 // FIXME: Bleh, can't we just clear them all at once ?
321 theApp->fontLoader().update();
325 bool font_available(LyXFont const & font)
327 return theApp->fontLoader().available(font);
331 void register_socket_callback(int fd, boost::function<void()> func)
333 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
337 void unregister_socket_callback(int fd)
339 socket_callbacks.erase(fd);
343 string const roman_font_name()
349 font.setStyleHint(QFont::Serif);
350 font.setFamily("serif");
352 return fromqstr(QFontInfo(font).family());
356 string const sans_font_name()
362 font.setStyleHint(QFont::SansSerif);
363 font.setFamily("sans");
365 return fromqstr(QFontInfo(font).family());
369 string const typewriter_font_name()
375 font.setStyleHint(QFont::TypeWriter);
376 font.setFamily("monospace");
378 return fromqstr(QFontInfo(font).family());
381 }; // namespace lyx_gui