2 * \file GuiApplication.cpp
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.
15 #include "GuiApplication.h"
17 #include "ColorCache.h"
18 #include "GuiClipboard.h"
20 #include "GuiKeySymbol.h"
21 #include "GuiSelection.h"
24 #include "qt_helpers.h"
25 #include "ToolbarBackend.h"
27 #include "frontends/alert.h"
28 #include "frontends/Application.h"
29 #include "frontends/FontLoader.h"
30 #include "frontends/FontMetrics.h"
33 #include "BufferList.h"
34 #include "BufferView.h"
37 #include "FuncRequest.h"
38 #include "FuncStatus.h"
47 #include "support/lassert.h"
48 #include "support/debug.h"
49 #include "support/ExceptionMessage.h"
50 #include "support/FileName.h"
51 #include "support/foreach.h"
52 #include "support/ForkedCalls.h"
53 #include "support/gettext.h"
54 #include "support/lstrings.h"
55 #include "support/os.h"
56 #include "support/Package.h"
59 #include "support/linkback/LinkBackProxy.h"
64 #include <QFileOpenEvent>
67 #include <QLibraryInfo>
68 #include <QMacPasteboardMime>
71 #include <QPixmapCache>
73 #include <QSessionManager>
74 #include <QSocketNotifier>
75 #include <QSortFilterProxyModel>
76 #include <QStandardItemModel>
79 #include <QTranslator>
83 #include <X11/Xatom.h>
91 #include <QWindowsMime>
92 #if defined(Q_CYGWIN_WIN) || defined(Q_CC_MINGW)
98 #include <boost/bind.hpp>
103 using namespace lyx::support;
108 frontend::Application * createApplication(int & argc, char * argv[])
110 return new frontend::GuiApplication(argc, argv);
115 class SocketNotifier : public QSocketNotifier
118 /// connect a connection notification from the LyXServerSocket
119 SocketNotifier(QObject * parent, int fd, Application::SocketCallback func)
120 : QSocketNotifier(fd, QSocketNotifier::Read, parent), func_(func)
124 /// The callback function
125 Application::SocketCallback func_;
129 ////////////////////////////////////////////////////////////////////////
130 // Mac specific stuff goes here...
132 class MenuTranslator : public QTranslator
135 MenuTranslator(QObject * parent)
136 : QTranslator(parent)
139 QString translate(const char * /*context*/,
140 const char * sourceText,
141 const char * /*comment*/ = 0)
143 string const s = sourceText;
144 if (s == N_("About %1") || s == N_("Preferences")
145 || s == N_("Reconfigure") || s == N_("Quit %1"))
152 class GlobalMenuBar : public QMenuBar
156 GlobalMenuBar() : QMenuBar(0) {}
159 bool event(QEvent * e)
161 if (e->type() == QEvent::ShortcutOverride) {
162 // && activeWindow() == 0) {
163 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
165 setKeySymbol(&sym, ke);
166 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
175 // QMacPasteboardMimeGraphics can only be compiled on Mac.
177 class QMacPasteboardMimeGraphics : public QMacPasteboardMime
180 QMacPasteboardMimeGraphics()
181 : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
184 QString convertorName() { return "Graphics"; }
186 QString flavorFor(QString const & mime)
188 LYXERR(Debug::ACTION, "flavorFor " << mime);
189 if (mime == QLatin1String(pdf_mime_type))
190 return QLatin1String("com.adobe.pdf");
194 QString mimeFor(QString flav)
196 LYXERR(Debug::ACTION, "mimeFor " << flav);
197 if (flav == QLatin1String("com.adobe.pdf"))
198 return QLatin1String(pdf_mime_type);
202 bool canConvert(QString const & mime, QString flav)
203 { return mimeFor(flav) == mime; }
205 QVariant convertToMime(QString const & mime, QList<QByteArray> data, QString flav)
208 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
212 QList<QByteArray> convertFromMime(QString const & mime, QVariant data, QString flav)
214 QList<QByteArray> ret;
215 ret.append(data.toByteArray());
221 ///////////////////////////////////////////////////////////////
222 // You can find more platform specific stuff
223 // at the end of this file...
224 ///////////////////////////////////////////////////////////////
226 ////////////////////////////////////////////////////////////////////////
227 // Windows specific stuff goes here...
230 // QWindowsMimeMetafile can only be compiled on Windows.
232 static FORMATETC cfFromMime(QString const & mimetype)
235 if (mimetype == emf_mime_type) {
236 formatetc.cfFormat = CF_ENHMETAFILE;
237 formatetc.tymed = TYMED_ENHMF;
238 } else if (mimetype == wmf_mime_type) {
239 formatetc.cfFormat = CF_METAFILEPICT;
240 formatetc.tymed = TYMED_MFPICT;
243 formatetc.dwAspect = DVASPECT_CONTENT;
244 formatetc.lindex = -1;
249 class QWindowsMimeMetafile : public QWindowsMime {
251 QWindowsMimeMetafile() {}
253 bool canConvertFromMime(FORMATETC const & formatetc,
254 QMimeData const * mimedata) const
259 bool canConvertToMime(QString const & mimetype,
260 IDataObject * pDataObj) const
262 if (mimetype != emf_mime_type && mimetype != wmf_mime_type)
264 FORMATETC formatetc = cfFromMime(mimetype);
265 return pDataObj->QueryGetData(&formatetc) == S_OK;
268 bool convertFromMime(FORMATETC const & formatetc,
269 const QMimeData * mimedata, STGMEDIUM * pmedium) const
274 QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj,
275 QVariant::Type preferredType) const
278 if (!canConvertToMime(mimetype, pDataObj))
281 FORMATETC formatetc = cfFromMime(mimetype);
283 if (pDataObj->GetData(&formatetc, &s) != S_OK)
287 if (s.tymed == TYMED_ENHMF) {
288 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, 0, 0);
289 data.resize(dataSize);
290 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, dataSize,
291 (LPBYTE)data.data());
292 } else if (s.tymed == TYMED_MFPICT) {
293 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, 0, 0);
294 data.resize(dataSize);
295 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, dataSize,
296 (LPBYTE)data.data());
299 ReleaseStgMedium(&s);
305 QVector<FORMATETC> formatsForMime(QString const & mimeType,
306 QMimeData const * mimeData) const
308 QVector<FORMATETC> formats;
309 formats += cfFromMime(mimeType);
313 QString mimeForFormat(FORMATETC const & formatetc) const
315 switch (formatetc.cfFormat) {
317 return emf_mime_type;
318 case CF_METAFILEPICT:
319 return wmf_mime_type;
327 ////////////////////////////////////////////////////////////////////////
328 // GuiApplication::Private definition and implementation.
329 ////////////////////////////////////////////////////////////////////////
331 struct GuiApplication::Private
334 : language_model_(0), global_menubar_(0)
337 // Create the global default menubar which is shown for the dialogs
338 // and if no GuiView is visible.
339 global_menubar_ = new GlobalMenuBar();
344 QSortFilterProxyModel * language_model_;
346 GuiClipboard clipboard_;
348 GuiSelection selection_;
350 FontLoader font_loader_;
352 ColorCache color_cache_;
354 QTranslator qt_trans_;
356 QHash<int, SocketNotifier *> socket_notifiers_;
359 /// this timer is used for any regular events one wants to
360 /// perform. at present it is used to check if forked processes
362 QTimer general_timer_;
364 /// Multiple views container.
366 * Warning: This must not be a smart pointer as the destruction of the
367 * object is handled by Qt when the view is closed
368 * \sa Qt::WA_DeleteOnClose attribute.
370 QHash<int, GuiView *> views_;
372 /// Only used on mac.
373 GlobalMenuBar * global_menubar_;
376 /// Linkback mime handler for MacOSX.
377 QMacPasteboardMimeGraphics mac_pasteboard_mime_;
381 /// WMF Mime handler for Windows clipboard.
382 // FIXME for Windows Vista and Qt4 (see http://bugzilla.lyx.org/show_bug.cgi?id=4846)
383 // But this makes LyX crash on exit when LyX is compiled in release mode and if there
384 // is something in the clipboard.
385 QWindowsMimeMetafile wmf_mime_;
390 GuiApplication * guiApp;
392 GuiApplication::~GuiApplication()
395 closeAllLinkBackLinks();
401 GuiApplication::GuiApplication(int & argc, char ** argv)
402 : QApplication(argc, argv), current_view_(0), d(new GuiApplication::Private)
404 QString app_name = "LyX";
405 QCoreApplication::setOrganizationName(app_name);
406 QCoreApplication::setOrganizationDomain("lyx.org");
407 QCoreApplication::setApplicationName(app_name + "-" + lyx_version);
409 // FIXME: quitOnLastWindowClosed is true by default. We should have a
410 // lyxrc setting for this in order to let the application stay resident.
411 // But then we need some kind of dock icon, at least on Windows.
413 if (lyxrc.quit_on_last_window_closed)
414 setQuitOnLastWindowClosed(false);
417 // FIXME: Do we need a lyxrc setting for this on Mac? This behaviour
418 // seems to be the default case for applications like LyX.
419 setQuitOnLastWindowClosed(false);
423 // doubleClickInterval() is 400 ms on X11 which is just too long.
424 // On Windows and Mac OS X, the operating system's value is used.
425 // On Microsoft Windows, calling this function sets the double
426 // click interval for all applications. So we don't!
427 QApplication::setDoubleClickInterval(300);
430 // install translation file for Qt built-in dialogs
431 QString language_name = QString("qt_") + QLocale::system().name();
433 // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN).
434 // Short-named translator can be loaded from a long name, but not the
435 // opposite. Therefore, long name should be used without truncation.
436 // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
437 if (d->qt_trans_.load(language_name,
438 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
440 installTranslator(&d->qt_trans_);
441 // even if the language calls for RtL, don't do that
442 setLayoutDirection(Qt::LeftToRight);
443 LYXERR(Debug::GUI, "Successfully installed Qt translations for locale "
446 LYXERR(Debug::GUI, "Could not find Qt translations for locale "
450 // This allows to translate the strings that appear in the LyX menu.
451 /// A translator suitable for the entries in the LyX menu.
452 /// Only needed with Qt/Mac.
453 installTranslator(new MenuTranslator(this));
455 connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onLastWindowClosed()));
457 using namespace lyx::graphics;
459 Image::newImage = boost::bind(&GuiImage::newImage);
460 Image::loadableFormats = boost::bind(&GuiImage::loadableFormats);
462 // needs to be done before reading lyxrc
464 lyxrc.dpi = (w.logicalDpiX() + w.logicalDpiY()) / 2;
468 // Set the cache to 5120 kilobytes which corresponds to screen size of
469 // 1280 by 1024 pixels with a color depth of 32 bits.
470 QPixmapCache::setCacheLimit(5120);
472 // Initialize RC Fonts
473 if (lyxrc.roman_font_name.empty())
474 lyxrc.roman_font_name = fromqstr(romanFontName());
476 if (lyxrc.sans_font_name.empty())
477 lyxrc.sans_font_name = fromqstr(sansFontName());
479 if (lyxrc.typewriter_font_name.empty())
480 lyxrc.typewriter_font_name = fromqstr(typewriterFontName());
482 d->general_timer_.setInterval(500);
483 connect(&d->general_timer_, SIGNAL(timeout()),
484 this, SLOT(handleRegularEvents()));
485 d->general_timer_.start();
489 bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
495 case LFUN_WINDOW_CLOSE:
496 enable = d->views_.size() > 0;
499 case LFUN_BUFFER_NEW:
500 case LFUN_BUFFER_NEW_TEMPLATE:
502 case LFUN_SCREEN_FONT_UPDATE:
504 case LFUN_WINDOW_NEW:
520 bool GuiApplication::dispatch(FuncRequest const & cmd)
522 switch (cmd.action) {
524 case LFUN_WINDOW_NEW:
525 createView(toqstr(cmd.argument()));
528 case LFUN_WINDOW_CLOSE:
529 // update bookmark pit of the current buffer before window close
530 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
531 theLyXFunc().gotoBookmark(i+1, false, false);
532 current_view_->close();
536 // quitting is triggered by the gui code
537 // (leaving the event loop).
539 current_view_->message(from_utf8(N_("Exiting.")));
544 case LFUN_SCREEN_FONT_UPDATE: {
545 // handle the screen font changes.
546 d->font_loader_.update();
547 // Backup current_view_
548 GuiView * view = current_view_;
549 // Set current_view_ to zero to forbid GuiWorkArea::redraw()
550 // to skip the refresh.
552 BufferList::iterator it = theBufferList().begin();
553 BufferList::iterator const end = theBufferList().end();
554 for (; it != end; ++it)
556 // Restore current_view_
557 current_view_ = view;
561 case LFUN_BUFFER_NEW:
562 if (d->views_.empty()
563 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
564 createView(QString(), false); // keep hidden
565 current_view_->newDocument(to_utf8(cmd.argument()), false);
566 current_view_->show();
567 setActiveWindow(current_view_);
569 current_view_->newDocument(to_utf8(cmd.argument()), false);
573 case LFUN_BUFFER_NEW_TEMPLATE:
574 if (d->views_.empty()
575 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
577 current_view_->newDocument(to_utf8(cmd.argument()), true);
578 if (!current_view_->buffer())
579 current_view_->close();
581 current_view_->newDocument(to_utf8(cmd.argument()), true);
586 if (d->views_.empty()
587 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
589 current_view_->openDocument(to_utf8(cmd.argument()));
590 if (!current_view_->buffer())
591 current_view_->close();
593 current_view_->openDocument(to_utf8(cmd.argument()));
596 case LFUN_SET_COLOR: {
598 string const x11_name = split(to_utf8(cmd.argument()), lyx_name, ' ');
599 if (lyx_name.empty() || x11_name.empty()) {
600 current_view_->message(
601 _("Syntax: set-color <lyx_name> <x11_name>"));
605 string const graphicsbg = lcolor.getLyXName(Color_graphicsbg);
606 bool const graphicsbg_changed = lyx_name == graphicsbg
607 && x11_name != graphicsbg;
608 if (graphicsbg_changed) {
609 // FIXME: The graphics cache no longer has a changeDisplay method.
611 graphics::GCache::get().changeDisplay(true);
615 if (!lcolor.setColor(lyx_name, x11_name)) {
616 current_view_->message(
617 bformat(_("Set-color \"%1$s\" failed "
618 "- color is undefined or "
619 "may not be redefined"),
620 from_utf8(lyx_name)));
623 // Make sure we don't keep old colors in cache.
624 d->color_cache_.clear();
629 // Notify the caller that the action has not been dispatched.
633 // The action has been dispatched.
638 void GuiApplication::resetGui()
640 QHash<int, GuiView *>::iterator it;
641 for (it = d->views_.begin(); it != d->views_.end(); ++it)
642 (*it)->resetDialogs();
644 dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
648 void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
650 // release the keyboard which might have been grabed by the global
651 // menubar on Mac to catch shortcuts even without any GuiView.
652 if (d->global_menubar_)
653 d->global_menubar_->releaseKeyboard();
657 while (d->views_.find(id) != d->views_.end())
659 GuiView * view = new GuiView(id);
661 // copy the icon size from old view
663 view->setIconSize(current_view_->iconSize());
666 d->views_[id] = view;
670 setActiveWindow(view);
673 if (!geometry_arg.isEmpty()) {
677 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
678 re.indexIn(geometry_arg);
679 w = re.cap(1).toInt();
680 h = re.cap(2).toInt();
681 x = re.cap(3).toInt();
682 y = re.cap(4).toInt();
683 view->setGeometry(x, y, w, h);
687 setCurrentView(view);
691 Clipboard & GuiApplication::clipboard()
693 return d->clipboard_;
697 Selection & GuiApplication::selection()
699 return d->selection_;
703 FontLoader & GuiApplication::fontLoader()
705 return d->font_loader_;
709 Menus const & GuiApplication::menus() const
715 Menus & GuiApplication::menus()
721 QList<int> GuiApplication::viewIds() const
723 return d->views_.keys();
727 ColorCache & GuiApplication::colorCache()
729 return d->color_cache_;
733 int GuiApplication::exec()
735 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
736 return QApplication::exec();
740 void GuiApplication::exit(int status)
742 QApplication::exit(status);
746 void GuiApplication::execBatchCommands()
749 if (!readUIFile(toqstr(lyxrc.ui_file)))
750 // Gives some error box here.
753 // init the global menubar on Mac. This must be done after the session
754 // was recovered to know the "last files".
755 if (d->global_menubar_)
756 d->menus_.fillMenuBar(d->global_menubar_, 0, true);
758 LyX::ref().execBatchCommands();
761 QAbstractItemModel * GuiApplication::languageModel()
763 if (d->language_model_)
764 return d->language_model_;
766 QStandardItemModel * lang_model = new QStandardItemModel(this);
767 lang_model->insertColumns(0, 1);
769 Languages::const_iterator it = languages.begin();
770 Languages::const_iterator end = languages.end();
771 for (; it != end; ++it) {
772 current_row = lang_model->rowCount();
773 lang_model->insertRows(current_row, 1);
774 QModelIndex item = lang_model->index(current_row, 0);
775 lang_model->setData(item, qt_(it->second.display()), Qt::DisplayRole);
776 lang_model->setData(item, toqstr(it->second.lang()), Qt::UserRole);
778 d->language_model_ = new QSortFilterProxyModel(this);
779 d->language_model_->setSourceModel(lang_model);
780 #if QT_VERSION >= 0x040300
781 d->language_model_->setSortLocaleAware(true);
783 return d->language_model_;
787 void GuiApplication::restoreGuiSession()
789 if (!lyxrc.load_session)
792 Session & session = LyX::ref().session();
793 vector<FileName> const & lastopened = session.lastOpened().getfiles();
794 // do not add to the lastfile list since these files are restored from
795 // last session, and should be already there (regular files), or should
796 // not be added at all (help files).
797 for_each(lastopened.begin(), lastopened.end(),
798 bind(&GuiView::loadDocument, current_view_, _1, false));
800 // clear this list to save a few bytes of RAM
801 session.lastOpened().clear();
805 QString const GuiApplication::romanFontName()
808 font.setKerning(false);
809 font.setStyleHint(QFont::Serif);
810 font.setFamily("serif");
812 return QFontInfo(font).family();
816 QString const GuiApplication::sansFontName()
819 font.setKerning(false);
820 font.setStyleHint(QFont::SansSerif);
821 font.setFamily("sans");
823 return QFontInfo(font).family();
827 QString const GuiApplication::typewriterFontName()
830 font.setKerning(false);
831 font.setStyleHint(QFont::TypeWriter);
832 font.setFamily("monospace");
834 return QFontInfo(font).family();
838 void GuiApplication::handleRegularEvents()
840 ForkedCallsController::handleCompletedProcesses();
844 bool GuiApplication::event(QEvent * e)
847 case QEvent::FileOpen: {
848 // Open a file; this happens only on Mac OS X for now
849 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
851 if (!current_view_ || !current_view_->view())
852 // The application is not properly initialized yet.
853 // So we acknowledge the event and delay the file opening
854 // until LyX is ready.
855 // FIXME UNICODE: FileName accept an utf8 encoded string.
856 LyX::ref().addFileToLoad(fromqstr(foe->file()));
858 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
859 qstring_to_ucs4(foe->file())));
865 return QApplication::event(e);
870 bool GuiApplication::notify(QObject * receiver, QEvent * event)
873 return QApplication::notify(receiver, event);
875 catch (ExceptionMessage const & e) {
878 LyX::cref().emergencyCleanup();
879 setQuitOnLastWindowClosed(false);
881 Alert::error(e.title_, e.details_);
883 // Properly crash in debug mode in order to get a useful backtrace.
886 // In release mode, try to exit gracefully.
889 case BufferException: {
890 Buffer * buf = current_view_->buffer();
891 docstring details = e.details_ + '\n';
892 details += theBufferList().emergencyWrite(buf);
893 theBufferList().release(buf);
894 details += "\n" + _("The current document was closed.");
895 Alert::error(e.title_, details);
898 case WarningException:
899 Alert::warning(e.title_, e.details_);
903 catch (exception const & e) {
904 docstring s = _("LyX has caught an exception, it will now "
905 "attempt to save all unsaved documents and exit."
907 s += from_ascii(e.what());
908 Alert::error(_("Software exception Detected"), s);
912 docstring s = _("LyX has caught some really weird exception, it will "
913 "now attempt to save all unsaved documents and exit.");
914 Alert::error(_("Software exception Detected"), s);
922 bool GuiApplication::getRgbColor(ColorCode col, RGBColor & rgbcol)
924 QColor const & qcol = d->color_cache_.get(col);
925 if (!qcol.isValid()) {
931 rgbcol.r = qcol.red();
932 rgbcol.g = qcol.green();
933 rgbcol.b = qcol.blue();
938 string const GuiApplication::hexName(ColorCode col)
940 return ltrim(fromqstr(d->color_cache_.get(col).name()), "#");
944 void GuiApplication::registerSocketCallback(int fd, SocketCallback func)
946 SocketNotifier * sn = new SocketNotifier(this, fd, func);
947 d->socket_notifiers_[fd] = sn;
948 connect(sn, SIGNAL(activated(int)), this, SLOT(socketDataReceived(int)));
952 void GuiApplication::socketDataReceived(int fd)
954 d->socket_notifiers_[fd]->func_();
958 void GuiApplication::unregisterSocketCallback(int fd)
960 d->socket_notifiers_.take(fd)->setEnabled(false);
964 void GuiApplication::commitData(QSessionManager & sm)
966 /// The implementation is required to avoid an application exit
967 /// when session state save is triggered by session manager.
968 /// The default implementation sends a close event to all
969 /// visible top level widgets when session managment allows
971 /// We are changing that to close all wiew one by one.
972 /// FIXME: verify if the default implementation is enough now.
973 if (sm.allowsInteraction() && !closeAllViews())
978 void GuiApplication::unregisterView(GuiView * gv)
980 LASSERT(d->views_[gv->id()] == gv, /**/);
981 d->views_.remove(gv->id());
982 if (current_view_ == gv) {
984 theLyXFunc().setLyXView(0);
989 bool GuiApplication::closeAllViews()
991 if (d->views_.empty())
994 QList<GuiView *> views = d->views_.values();
995 foreach (GuiView * view, views) {
1005 GuiView & GuiApplication::view(int id) const
1007 LASSERT(d->views_.contains(id), /**/);
1008 return *d->views_.value(id);
1012 void GuiApplication::hideDialogs(string const & name, Inset * inset) const
1014 QList<GuiView *> views = d->views_.values();
1015 foreach (GuiView * view, views)
1016 view->hideDialog(name, inset);
1020 Buffer const * GuiApplication::updateInset(Inset const * inset) const
1022 Buffer const * buffer_ = 0;
1023 QHash<int, GuiView *>::iterator end = d->views_.end();
1024 for (QHash<int, GuiView *>::iterator it = d->views_.begin(); it != end; ++it) {
1025 if (Buffer const * ptr = (*it)->updateInset(inset))
1032 bool GuiApplication::searchMenu(FuncRequest const & func,
1033 docstring_list & names) const
1035 return d->menus_.searchMenu(func, names);
1039 bool GuiApplication::readUIFile(QString const & name, bool include)
1049 LexerKeyword uitags[] = {
1050 { "include", ui_include },
1051 { "menuset", ui_menuset },
1052 { "toolbars", ui_toolbars },
1053 { "toolbarset", ui_toolbarset }
1056 // Ensure that a file is read only once (prevents include loops)
1057 static QStringList uifiles;
1058 if (uifiles.contains(name)) {
1059 LYXERR(Debug::INIT, "UI file '" << name << "' has been read already. "
1060 << "Is this an include loop?");
1064 LYXERR(Debug::INIT, "About to read " << name << "...");
1068 ui_path = libFileSearch("ui", name, "inc");
1069 if (ui_path.empty())
1070 ui_path = libFileSearch("ui", changeExtension(name, "inc"));
1072 ui_path = libFileSearch("ui", name, "ui");
1075 if (ui_path.empty()) {
1076 LYXERR(Debug::INIT, "Could not find " << name);
1077 Alert::warning(_("Could not find UI defintion file"),
1078 bformat(_("Error while reading the configuration file\n%1$s.\n"
1079 "Please check your installation."), qstring_to_ucs4(name)));
1083 uifiles.push_back(name);
1085 LYXERR(Debug::INIT, "Found " << name << " in " << ui_path);
1087 lex.setFile(ui_path);
1089 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
1093 if (lyxerr.debugging(Debug::PARSER))
1094 lex.printTable(lyxerr);
1096 while (lex.isOK()) {
1097 switch (lex.lex()) {
1100 QString const file = toqstr(lex.getString());
1101 if (!readUIFile(file, true))
1106 d->menus_.read(lex);
1110 toolbarbackend.readToolbars(lex);
1114 toolbarbackend.readToolbarSettings(lex);
1118 if (!rtrim(lex.getString()).empty())
1119 lex.printError("LyX::ReadUIFile: "
1120 "Unknown menu tag: `$$Token'");
1128 void GuiApplication::onLastWindowClosed()
1130 if (d->global_menubar_)
1131 d->global_menubar_->grabKeyboard();
1135 ////////////////////////////////////////////////////////////////////////
1137 // X11 specific stuff goes here...
1140 bool GuiApplication::x11EventFilter(XEvent * xev)
1145 switch (xev->type) {
1146 case SelectionRequest: {
1147 if (xev->xselectionrequest.selection != XA_PRIMARY)
1149 LYXERR(Debug::GUI, "X requested selection.");
1150 BufferView * bv = current_view_->view();
1152 docstring const sel = bv->requestSelection();
1154 d->selection_.put(sel);
1158 case SelectionClear: {
1159 if (xev->xselectionclear.selection != XA_PRIMARY)
1161 LYXERR(Debug::GUI, "Lost selection.");
1162 BufferView * bv = current_view_->view();
1164 bv->clearSelection();
1172 } // namespace frontend
1175 void hideDialogs(std::string const & name, Inset * inset)
1178 theApp()->hideDialogs(name, inset);
1182 ////////////////////////////////////////////////////////////////////
1186 ////////////////////////////////////////////////////////////////////
1188 frontend::FontLoader & theFontLoader()
1190 LASSERT(frontend::guiApp, /**/);
1191 return frontend::guiApp->fontLoader();
1195 frontend::FontMetrics const & theFontMetrics(Font const & f)
1197 return theFontMetrics(f.fontInfo());
1201 frontend::FontMetrics const & theFontMetrics(FontInfo const & f)
1203 LASSERT(frontend::guiApp, /**/);
1204 return frontend::guiApp->fontLoader().metrics(f);
1208 ////////////////////////////////////////////////////////////////////
1212 ////////////////////////////////////////////////////////////////////
1214 frontend::Clipboard & theClipboard()
1216 LASSERT(frontend::guiApp, /**/);
1217 return frontend::guiApp->clipboard();
1221 frontend::Selection & theSelection()
1223 LASSERT(frontend::guiApp, /**/);
1224 return frontend::guiApp->selection();
1229 #include "GuiApplication_moc.cpp"