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 int 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 return LyX::ref().exec2(argc, argv);
196 int start(string const & batch, vector<string> const & files,
197 unsigned int width, unsigned int height, int posx, int posy,
200 // this can't be done before because it needs the Languages object
203 int view_id = theApp->gui().newView(width, height);
204 GuiView & view = static_cast<GuiView &> (theApp->gui().view(view_id));
206 // FIXME: for now we assume that there is only one LyXView with id = 0.
207 int workArea_id_ = theApp->gui().newWorkArea(width, height, 0);
208 //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
210 LyX::ref().addLyXView(&view);
214 // only true when the -geometry option was NOT used
215 if (width != 0 && height != 0) {
216 if (posx != -1 && posy != -1) {
218 // FIXME: use only setGeoemtry when Trolltech has
219 // fixed the qt4/X11 bug
220 view.setGeometry(posx, posy,width, height);
222 view.resize(width, height);
223 view.move(posx, posy);
226 view.resize(width, height);
230 view.setWindowState(Qt::WindowMaximized);
235 // FIXME: some code below needs moving
237 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
238 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
239 os::internal_path(package().temp_dir() + "/lyxsocket"));
241 for_each(files.begin(), files.end(),
242 bind(&BufferView::loadLyXFile, view.view(), _1, true));
244 // handle the batch commands the user asked for
245 if (!batch.empty()) {
246 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
249 int const status = qApp->exec();
259 // This is the ONLY place where processEvents may be called.
260 // During screen update/ redraw, this method is disabled to
261 // prevent keyboard events being handed to the LyX core, where
262 // they could cause re-entrant calls to screen update.
263 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
267 void exit(int status)
270 QApplication::exit(status);
274 FuncStatus getStatus(FuncRequest const & ev)
278 case LFUN_DIALOG_SHOW:
279 if (ev.argument == "preamble")
282 case LFUN_TOOLTIPS_TOGGLE:
293 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
295 QColor const & qcol = lcolorcache.get(col);
296 if (!qcol.isValid()) {
302 rgbcol.r = qcol.red();
303 rgbcol.g = qcol.green();
304 rgbcol.b = qcol.blue();
309 string const hexname(LColor_color col)
311 return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
315 void update_color(LColor_color)
317 // FIXME: Bleh, can't we just clear them all at once ?
324 theApp->fontLoader().update();
328 bool font_available(LyXFont const & font)
330 return theApp->fontLoader().available(font);
334 void register_socket_callback(int fd, boost::function<void()> func)
336 socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
340 void unregister_socket_callback(int fd)
342 socket_callbacks.erase(fd);
346 string const roman_font_name()
352 font.setStyleHint(QFont::Serif);
353 font.setFamily("serif");
355 return fromqstr(QFontInfo(font).family());
359 string const sans_font_name()
365 font.setStyleHint(QFont::SansSerif);
366 font.setFamily("sans");
368 return fromqstr(QFontInfo(font).family());
372 string const typewriter_font_name()
378 font.setStyleHint(QFont::TypeWriter);
379 font.setFamily("monospace");
381 return fromqstr(QFontInfo(font).family());
384 }; // namespace lyx_gui