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"
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 == pdfMimeType())
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 pdfMimeType();
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 == emfMimeType()) {
236 formatetc.cfFormat = CF_ENHMETAFILE;
237 formatetc.tymed = TYMED_ENHMF;
238 } else if (mimetype == wmfMimeType()) {
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 != emfMimeType() && mimetype != wmfMimeType())
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 emfMimeType();
318 case CF_METAFILEPICT:
319 return wmfMimeType();
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_;
360 /// The global instance
363 /// this timer is used for any regular events one wants to
364 /// perform. at present it is used to check if forked processes
366 QTimer general_timer_;
368 /// Multiple views container.
370 * Warning: This must not be a smart pointer as the destruction of the
371 * object is handled by Qt when the view is closed
372 * \sa Qt::WA_DeleteOnClose attribute.
374 QHash<int, GuiView *> views_;
376 /// Only used on mac.
377 GlobalMenuBar * global_menubar_;
380 /// Linkback mime handler for MacOSX.
381 QMacPasteboardMimeGraphics mac_pasteboard_mime_;
385 /// WMF Mime handler for Windows clipboard.
386 // FIXME for Windows Vista and Qt4 (see http://bugzilla.lyx.org/show_bug.cgi?id=4846)
387 // But this makes LyX crash on exit when LyX is compiled in release mode and if there
388 // is something in the clipboard.
389 QWindowsMimeMetafile wmf_mime_;
394 GuiApplication * guiApp;
396 GuiApplication::~GuiApplication()
399 closeAllLinkBackLinks();
405 GuiApplication::GuiApplication(int & argc, char ** argv)
406 : QApplication(argc, argv), current_view_(0), d(new GuiApplication::Private)
408 QString app_name = "LyX";
409 QCoreApplication::setOrganizationName(app_name);
410 QCoreApplication::setOrganizationDomain("lyx.org");
411 QCoreApplication::setApplicationName(app_name + "-" + lyx_version);
413 // FIXME: quitOnLastWindowClosed is true by default. We should have a
414 // lyxrc setting for this in order to let the application stay resident.
415 // But then we need some kind of dock icon, at least on Windows.
417 if (lyxrc.quit_on_last_window_closed)
418 setQuitOnLastWindowClosed(false);
421 // FIXME: Do we need a lyxrc setting for this on Mac? This behaviour
422 // seems to be the default case for applications like LyX.
423 setQuitOnLastWindowClosed(false);
427 // doubleClickInterval() is 400 ms on X11 which is just too long.
428 // On Windows and Mac OS X, the operating system's value is used.
429 // On Microsoft Windows, calling this function sets the double
430 // click interval for all applications. So we don't!
431 QApplication::setDoubleClickInterval(300);
434 // install translation file for Qt built-in dialogs
435 QString language_name = QString("qt_") + QLocale::system().name();
437 // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN).
438 // Short-named translator can be loaded from a long name, but not the
439 // opposite. Therefore, long name should be used without truncation.
440 // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
441 if (d->qt_trans_.load(language_name,
442 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
444 installTranslator(&d->qt_trans_);
445 // even if the language calls for RtL, don't do that
446 setLayoutDirection(Qt::LeftToRight);
447 LYXERR(Debug::GUI, "Successfully installed Qt translations for locale "
450 LYXERR(Debug::GUI, "Could not find Qt translations for locale "
454 // This allows to translate the strings that appear in the LyX menu.
455 /// A translator suitable for the entries in the LyX menu.
456 /// Only needed with Qt/Mac.
457 installTranslator(new MenuTranslator(this));
459 connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onLastWindowClosed()));
461 using namespace lyx::graphics;
463 Image::newImage = boost::bind(&GuiImage::newImage);
464 Image::loadableFormats = boost::bind(&GuiImage::loadableFormats);
466 // needs to be done before reading lyxrc
468 lyxrc.dpi = (w.logicalDpiX() + w.logicalDpiY()) / 2;
472 // Set the cache to 5120 kilobytes which corresponds to screen size of
473 // 1280 by 1024 pixels with a color depth of 32 bits.
474 QPixmapCache::setCacheLimit(5120);
476 // Initialize RC Fonts
477 if (lyxrc.roman_font_name.empty())
478 lyxrc.roman_font_name = fromqstr(romanFontName());
480 if (lyxrc.sans_font_name.empty())
481 lyxrc.sans_font_name = fromqstr(sansFontName());
483 if (lyxrc.typewriter_font_name.empty())
484 lyxrc.typewriter_font_name = fromqstr(typewriterFontName());
486 d->general_timer_.setInterval(500);
487 connect(&d->general_timer_, SIGNAL(timeout()),
488 this, SLOT(handleRegularEvents()));
489 d->general_timer_.start();
493 bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
499 case LFUN_WINDOW_CLOSE:
500 enable = d->views_.size() > 0;
503 case LFUN_BUFFER_NEW:
504 case LFUN_BUFFER_NEW_TEMPLATE:
506 case LFUN_SCREEN_FONT_UPDATE:
508 case LFUN_WINDOW_NEW:
524 bool GuiApplication::dispatch(FuncRequest const & cmd)
526 switch (cmd.action) {
528 case LFUN_WINDOW_NEW:
529 createView(toqstr(cmd.argument()));
532 case LFUN_WINDOW_CLOSE:
533 // update bookmark pit of the current buffer before window close
534 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
535 theLyXFunc().gotoBookmark(i+1, false, false);
536 current_view_->close();
540 // quitting is triggered by the gui code
541 // (leaving the event loop).
543 current_view_->message(from_utf8(N_("Exiting.")));
548 case LFUN_SCREEN_FONT_UPDATE: {
549 // handle the screen font changes.
550 d->font_loader_.update();
551 // Backup current_view_
552 GuiView * view = current_view_;
553 // Set current_view_ to zero to forbid GuiWorkArea::redraw()
554 // to skip the refresh.
556 BufferList::iterator it = theBufferList().begin();
557 BufferList::iterator const end = theBufferList().end();
558 for (; it != end; ++it)
560 // Restore current_view_
561 current_view_ = view;
565 case LFUN_BUFFER_NEW:
566 if (d->views_.empty()
567 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
568 createView(QString(), false); // keep hidden
569 current_view_->newDocument(to_utf8(cmd.argument()), false);
570 current_view_->show();
571 setActiveWindow(current_view_);
573 current_view_->newDocument(to_utf8(cmd.argument()), false);
577 case LFUN_BUFFER_NEW_TEMPLATE:
578 if (d->views_.empty()
579 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
581 current_view_->newDocument(to_utf8(cmd.argument()), true);
582 if (!current_view_->buffer())
583 current_view_->close();
585 current_view_->newDocument(to_utf8(cmd.argument()), true);
590 if (d->views_.empty()
591 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
593 current_view_->openDocument(to_utf8(cmd.argument()));
594 if (!current_view_->buffer())
595 current_view_->close();
597 current_view_->openDocument(to_utf8(cmd.argument()));
600 case LFUN_SET_COLOR: {
602 string const x11_name = split(to_utf8(cmd.argument()), lyx_name, ' ');
603 if (lyx_name.empty() || x11_name.empty()) {
604 current_view_->message(
605 _("Syntax: set-color <lyx_name> <x11_name>"));
609 string const graphicsbg = lcolor.getLyXName(Color_graphicsbg);
610 bool const graphicsbg_changed = lyx_name == graphicsbg
611 && x11_name != graphicsbg;
612 if (graphicsbg_changed) {
613 // FIXME: The graphics cache no longer has a changeDisplay method.
615 graphics::GCache::get().changeDisplay(true);
619 if (!lcolor.setColor(lyx_name, x11_name)) {
620 current_view_->message(
621 bformat(_("Set-color \"%1$s\" failed "
622 "- color is undefined or "
623 "may not be redefined"),
624 from_utf8(lyx_name)));
627 // Make sure we don't keep old colors in cache.
628 d->color_cache_.clear();
633 // Notify the caller that the action has not been dispatched.
637 // The action has been dispatched.
642 void GuiApplication::resetGui()
644 QHash<int, GuiView *>::iterator it;
645 for (it = d->views_.begin(); it != d->views_.end(); ++it)
646 (*it)->resetDialogs();
648 dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
652 void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
654 // release the keyboard which might have been grabed by the global
655 // menubar on Mac to catch shortcuts even without any GuiView.
656 if (d->global_menubar_)
657 d->global_menubar_->releaseKeyboard();
661 while (d->views_.find(id) != d->views_.end())
663 GuiView * view = new GuiView(id);
665 // copy the icon size from old view
667 view->setIconSize(current_view_->iconSize());
670 d->views_[id] = view;
674 setActiveWindow(view);
677 if (!geometry_arg.isEmpty()) {
681 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
682 re.indexIn(geometry_arg);
683 w = re.cap(1).toInt();
684 h = re.cap(2).toInt();
685 x = re.cap(3).toInt();
686 y = re.cap(4).toInt();
687 view->setGeometry(x, y, w, h);
691 setCurrentView(view);
695 Clipboard & GuiApplication::clipboard()
697 return d->clipboard_;
701 Selection & GuiApplication::selection()
703 return d->selection_;
707 FontLoader & GuiApplication::fontLoader()
709 return d->font_loader_;
713 Toolbars const & GuiApplication::toolbars() const
719 Toolbars & GuiApplication::toolbars()
725 Menus const & GuiApplication::menus() const
731 Menus & GuiApplication::menus()
737 QList<int> GuiApplication::viewIds() const
739 return d->views_.keys();
743 ColorCache & GuiApplication::colorCache()
745 return d->color_cache_;
749 int GuiApplication::exec()
751 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
752 return QApplication::exec();
756 void GuiApplication::exit(int status)
758 QApplication::exit(status);
762 void GuiApplication::execBatchCommands()
765 if (!readUIFile(toqstr(lyxrc.ui_file)))
766 // Gives some error box here.
769 // init the global menubar on Mac. This must be done after the session
770 // was recovered to know the "last files".
771 if (d->global_menubar_)
772 d->menus_.fillMenuBar(d->global_menubar_, 0, true);
774 LyX::ref().execBatchCommands();
777 QAbstractItemModel * GuiApplication::languageModel()
779 if (d->language_model_)
780 return d->language_model_;
782 QStandardItemModel * lang_model = new QStandardItemModel(this);
783 lang_model->insertColumns(0, 1);
785 Languages::const_iterator it = languages.begin();
786 Languages::const_iterator end = languages.end();
787 for (; it != end; ++it) {
788 current_row = lang_model->rowCount();
789 lang_model->insertRows(current_row, 1);
790 QModelIndex item = lang_model->index(current_row, 0);
791 lang_model->setData(item, qt_(it->second.display()), Qt::DisplayRole);
792 lang_model->setData(item, toqstr(it->second.lang()), Qt::UserRole);
794 d->language_model_ = new QSortFilterProxyModel(this);
795 d->language_model_->setSourceModel(lang_model);
796 #if QT_VERSION >= 0x040300
797 d->language_model_->setSortLocaleAware(true);
799 return d->language_model_;
803 void GuiApplication::restoreGuiSession()
805 if (!lyxrc.load_session)
808 Session & session = LyX::ref().session();
809 vector<FileName> const & lastopened = session.lastOpened().getfiles();
810 // do not add to the lastfile list since these files are restored from
811 // last session, and should be already there (regular files), or should
812 // not be added at all (help files).
813 for_each(lastopened.begin(), lastopened.end(),
814 bind(&GuiView::loadDocument, current_view_, _1, false));
816 // clear this list to save a few bytes of RAM
817 session.lastOpened().clear();
821 QString const GuiApplication::romanFontName()
824 font.setKerning(false);
825 font.setStyleHint(QFont::Serif);
826 font.setFamily("serif");
828 return QFontInfo(font).family();
832 QString const GuiApplication::sansFontName()
835 font.setKerning(false);
836 font.setStyleHint(QFont::SansSerif);
837 font.setFamily("sans");
839 return QFontInfo(font).family();
843 QString const GuiApplication::typewriterFontName()
846 font.setKerning(false);
847 font.setStyleHint(QFont::TypeWriter);
848 font.setFamily("monospace");
850 return QFontInfo(font).family();
854 void GuiApplication::handleRegularEvents()
856 ForkedCallsController::handleCompletedProcesses();
860 bool GuiApplication::event(QEvent * e)
863 case QEvent::FileOpen: {
864 // Open a file; this happens only on Mac OS X for now
865 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
867 if (!current_view_ || !current_view_->view())
868 // The application is not properly initialized yet.
869 // So we acknowledge the event and delay the file opening
870 // until LyX is ready.
871 // FIXME UNICODE: FileName accept an utf8 encoded string.
872 LyX::ref().addFileToLoad(fromqstr(foe->file()));
874 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
875 qstring_to_ucs4(foe->file())));
881 return QApplication::event(e);
886 bool GuiApplication::notify(QObject * receiver, QEvent * event)
889 return QApplication::notify(receiver, event);
891 catch (ExceptionMessage const & e) {
894 LyX::cref().emergencyCleanup();
895 setQuitOnLastWindowClosed(false);
897 Alert::error(e.title_, e.details_);
899 // Properly crash in debug mode in order to get a useful backtrace.
902 // In release mode, try to exit gracefully.
905 case BufferException: {
906 Buffer * buf = current_view_->buffer();
907 docstring details = e.details_ + '\n';
908 details += theBufferList().emergencyWrite(buf);
909 theBufferList().release(buf);
910 details += "\n" + _("The current document was closed.");
911 Alert::error(e.title_, details);
914 case WarningException:
915 Alert::warning(e.title_, e.details_);
919 catch (exception const & e) {
920 docstring s = _("LyX has caught an exception, it will now "
921 "attempt to save all unsaved documents and exit."
923 s += from_ascii(e.what());
924 Alert::error(_("Software exception Detected"), s);
928 docstring s = _("LyX has caught some really weird exception, it will "
929 "now attempt to save all unsaved documents and exit.");
930 Alert::error(_("Software exception Detected"), s);
938 bool GuiApplication::getRgbColor(ColorCode col, RGBColor & rgbcol)
940 QColor const & qcol = d->color_cache_.get(col);
941 if (!qcol.isValid()) {
947 rgbcol.r = qcol.red();
948 rgbcol.g = qcol.green();
949 rgbcol.b = qcol.blue();
954 string const GuiApplication::hexName(ColorCode col)
956 return ltrim(fromqstr(d->color_cache_.get(col).name()), "#");
960 void GuiApplication::registerSocketCallback(int fd, SocketCallback func)
962 SocketNotifier * sn = new SocketNotifier(this, fd, func);
963 d->socket_notifiers_[fd] = sn;
964 connect(sn, SIGNAL(activated(int)), this, SLOT(socketDataReceived(int)));
968 void GuiApplication::socketDataReceived(int fd)
970 d->socket_notifiers_[fd]->func_();
974 void GuiApplication::unregisterSocketCallback(int fd)
976 d->socket_notifiers_.take(fd)->setEnabled(false);
980 void GuiApplication::commitData(QSessionManager & sm)
982 /// The implementation is required to avoid an application exit
983 /// when session state save is triggered by session manager.
984 /// The default implementation sends a close event to all
985 /// visible top level widgets when session managment allows
987 /// We are changing that to close all wiew one by one.
988 /// FIXME: verify if the default implementation is enough now.
989 if (sm.allowsInteraction() && !closeAllViews())
994 void GuiApplication::unregisterView(GuiView * gv)
996 LASSERT(d->views_[gv->id()] == gv, /**/);
997 d->views_.remove(gv->id());
998 if (current_view_ == gv) {
1000 theLyXFunc().setLyXView(0);
1005 bool GuiApplication::closeAllViews()
1007 if (d->views_.empty())
1010 QList<GuiView *> views = d->views_.values();
1011 foreach (GuiView * view, views) {
1021 GuiView & GuiApplication::view(int id) const
1023 LASSERT(d->views_.contains(id), /**/);
1024 return *d->views_.value(id);
1028 void GuiApplication::hideDialogs(string const & name, Inset * inset) const
1030 QList<GuiView *> views = d->views_.values();
1031 foreach (GuiView * view, views)
1032 view->hideDialog(name, inset);
1036 Buffer const * GuiApplication::updateInset(Inset const * inset) const
1038 Buffer const * buffer_ = 0;
1039 QHash<int, GuiView *>::iterator end = d->views_.end();
1040 for (QHash<int, GuiView *>::iterator it = d->views_.begin(); it != end; ++it) {
1041 if (Buffer const * ptr = (*it)->updateInset(inset))
1048 bool GuiApplication::searchMenu(FuncRequest const & func,
1049 docstring_list & names) const
1051 return d->menus_.searchMenu(func, names);
1055 bool GuiApplication::readUIFile(QString const & name, bool include)
1065 LexerKeyword uitags[] = {
1066 { "include", ui_include },
1067 { "menuset", ui_menuset },
1068 { "toolbars", ui_toolbars },
1069 { "toolbarset", ui_toolbarset }
1072 // Ensure that a file is read only once (prevents include loops)
1073 static QStringList uifiles;
1074 if (uifiles.contains(name)) {
1075 LYXERR(Debug::INIT, "UI file '" << name << "' has been read already. "
1076 << "Is this an include loop?");
1080 LYXERR(Debug::INIT, "About to read " << name << "...");
1084 ui_path = libFileSearch("ui", name, "inc");
1085 if (ui_path.empty())
1086 ui_path = libFileSearch("ui", changeExtension(name, "inc"));
1088 ui_path = libFileSearch("ui", name, "ui");
1091 if (ui_path.empty()) {
1092 LYXERR(Debug::INIT, "Could not find " << name);
1093 Alert::warning(_("Could not find UI defintion file"),
1094 bformat(_("Error while reading the configuration file\n%1$s.\n"
1095 "Please check your installation."), qstring_to_ucs4(name)));
1099 uifiles.push_back(name);
1101 LYXERR(Debug::INIT, "Found " << name << " in " << ui_path);
1103 lex.setFile(ui_path);
1105 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
1109 if (lyxerr.debugging(Debug::PARSER))
1110 lex.printTable(lyxerr);
1112 while (lex.isOK()) {
1113 switch (lex.lex()) {
1116 QString const file = toqstr(lex.getString());
1117 if (!readUIFile(file, true))
1122 d->menus_.read(lex);
1126 d->toolbars_.readToolbars(lex);
1130 d->toolbars_.readToolbarSettings(lex);
1134 if (!rtrim(lex.getString()).empty())
1135 lex.printError("LyX::ReadUIFile: "
1136 "Unknown menu tag: `$$Token'");
1144 void GuiApplication::onLastWindowClosed()
1146 if (d->global_menubar_)
1147 d->global_menubar_->grabKeyboard();
1151 ////////////////////////////////////////////////////////////////////////
1153 // X11 specific stuff goes here...
1156 bool GuiApplication::x11EventFilter(XEvent * xev)
1161 switch (xev->type) {
1162 case SelectionRequest: {
1163 if (xev->xselectionrequest.selection != XA_PRIMARY)
1165 LYXERR(Debug::GUI, "X requested selection.");
1166 BufferView * bv = current_view_->view();
1168 docstring const sel = bv->requestSelection();
1170 d->selection_.put(sel);
1174 case SelectionClear: {
1175 if (xev->xselectionclear.selection != XA_PRIMARY)
1177 LYXERR(Debug::GUI, "Lost selection.");
1178 BufferView * bv = current_view_->view();
1180 bv->clearSelection();
1188 } // namespace frontend
1191 void hideDialogs(std::string const & name, Inset * inset)
1194 theApp()->hideDialogs(name, inset);
1198 ////////////////////////////////////////////////////////////////////
1202 ////////////////////////////////////////////////////////////////////
1204 frontend::FontLoader & theFontLoader()
1206 LASSERT(frontend::guiApp, /**/);
1207 return frontend::guiApp->fontLoader();
1211 frontend::FontMetrics const & theFontMetrics(Font const & f)
1213 return theFontMetrics(f.fontInfo());
1217 frontend::FontMetrics const & theFontMetrics(FontInfo const & f)
1219 LASSERT(frontend::guiApp, /**/);
1220 return frontend::guiApp->fontLoader().metrics(f);
1224 ////////////////////////////////////////////////////////////////////
1228 ////////////////////////////////////////////////////////////////////
1230 frontend::Clipboard & theClipboard()
1232 LASSERT(frontend::guiApp, /**/);
1233 return frontend::guiApp->clipboard();
1237 frontend::Selection & theSelection()
1239 LASSERT(frontend::guiApp, /**/);
1240 return frontend::guiApp->selection();
1245 #include "GuiApplication_moc.cpp"