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(width, height));
198 LyX::ref().addLyXView(view_ptr);
200 GuiView & view = *view_ptr.get();
204 if (posx != -1 && posy != -1) {
205 view.setGeometry(posx, posy, width, height);
207 view.setWindowState(Qt::WindowMaximized);
212 // FIXME: some code below needs moving
214 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
215 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
216 os::internal_path(package().temp_dir() + "/lyxsocket"));
218 for_each(files.begin(), files.end(),
219 bind(&BufferView::loadLyXFile, view.view(), _1, true));
221 // handle the batch commands the user asked for
222 if (!batch.empty()) {
223 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
235 // This is the ONLY place where processEvents may be called.
236 // During screen update/ redraw, this method is disabled to
237 // prevent keyboard events being handed to the LyX core, where
238 // they could cause re-entrant calls to screen update.
239 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
243 void exit(int status)
247 // we cannot call QApplication::exit(status) - that could return us
248 // into a static dialog return in the lyx code (for example,
249 // load autosave file QMessageBox. We have to just get the hell
256 FuncStatus getStatus(FuncRequest const & ev)
260 case LFUN_DIALOG_SHOW:
261 if (ev.argument == "preamble")
264 case LFUN_TOOLTIPS_TOGGLE:
275 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
277 QColor const & qcol = lcolorcache.get(col);
278 if (!qcol.isValid()) {
284 rgbcol.r = qcol.red();
285 rgbcol.g = qcol.green();
286 rgbcol.b = qcol.blue();
291 string const hexname(LColor_color col)
293 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
297 void update_color(LColor_color)
299 // FIXME: Bleh, can't we just clear them all at once ?
310 bool font_available(LyXFont const & font)
312 return fontloader.available(font);
316 void register_socket_callback(int fd, boost::function<void()> func)
318 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
322 void unregister_socket_callback(int fd)
324 socket_callbacks.erase(fd);
328 string const roman_font_name()
334 font.setStyleHint(QFont::Serif);
335 font.setFamily("serif");
337 return fromqstr(QFontInfo(font).family());
341 string const sans_font_name()
347 font.setStyleHint(QFont::SansSerif);
348 font.setFamily("sans");
350 return fromqstr(QFontInfo(font).family());
354 string const typewriter_font_name()
360 font.setStyleHint(QFont::TypeWriter);
361 font.setFamily("monospace");
363 return fromqstr(QFontInfo(font).family());
366 }; // namespace lyx_gui