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;
105 // FIXME: These strings are also used in GuiClipboard.cpp.
106 static char const * const lyx_mime_type = "application/x-lyx";
107 static char const * const pdf_mime_type = "application/pdf";
108 static char const * const emf_mime_type = "image/x-emf";
109 static char const * const wmf_mime_type = "image/x-wmf";
113 frontend::Application * createApplication(int & argc, char * argv[])
115 return new frontend::GuiApplication(argc, argv);
121 class SocketNotifier : public QSocketNotifier
124 /// connect a connection notification from the LyXServerSocket
125 SocketNotifier(QObject * parent, int fd, Application::SocketCallback func)
126 : QSocketNotifier(fd, QSocketNotifier::Read, parent), func_(func)
130 /// The callback function
131 Application::SocketCallback func_;
135 ////////////////////////////////////////////////////////////////////////
136 // Mac specific stuff goes here...
138 class MenuTranslator : public QTranslator
141 MenuTranslator(QObject * parent)
142 : QTranslator(parent)
145 QString translate(const char * /*context*/,
146 const char * sourceText,
147 const char * /*comment*/ = 0)
149 string const s = sourceText;
150 if (s == N_("About %1") || s == N_("Preferences")
151 || s == N_("Reconfigure") || s == N_("Quit %1"))
158 class GlobalMenuBar : public QMenuBar
162 GlobalMenuBar() : QMenuBar(0) {}
165 bool event(QEvent * e)
167 if (e->type() == QEvent::ShortcutOverride) {
168 // && activeWindow() == 0) {
169 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
171 setKeySymbol(&sym, ke);
172 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
181 // QMacPasteboardMimeGraphics can only be compiled on Mac.
183 class QMacPasteboardMimeGraphics : public QMacPasteboardMime
186 QMacPasteboardMimeGraphics()
187 : QMacPasteboardMime(MIME_QT_CONVERTOR|MIME_ALL)
190 QString convertorName() { return "Graphics"; }
192 QString flavorFor(QString const & mime)
194 LYXERR(Debug::ACTION, "flavorFor " << mime);
195 if (mime == QLatin1String(pdf_mime_type))
196 return QLatin1String("com.adobe.pdf");
200 QString mimeFor(QString flav)
202 LYXERR(Debug::ACTION, "mimeFor " << flav);
203 if (flav == QLatin1String("com.adobe.pdf"))
204 return QLatin1String(pdf_mime_type);
208 bool canConvert(QString const & mime, QString flav)
209 { return mimeFor(flav) == mime; }
211 QVariant convertToMime(QString const & mime, QList<QByteArray> data, QString flav)
214 qWarning("QMacPasteboardMimeGraphics: Cannot handle multiple member data");
218 QList<QByteArray> convertFromMime(QString const & mime, QVariant data, QString flav)
220 QList<QByteArray> ret;
221 ret.append(data.toByteArray());
227 ///////////////////////////////////////////////////////////////
228 // You can find more platform specific stuff
229 // at the end of this file...
230 ///////////////////////////////////////////////////////////////
232 ////////////////////////////////////////////////////////////////////////
233 // Windows specific stuff goes here...
236 // QWindowsMimeMetafile can only be compiled on Windows.
238 static FORMATETC cfFromMime(QString const & mimetype)
241 if (mimetype == emf_mime_type) {
242 formatetc.cfFormat = CF_ENHMETAFILE;
243 formatetc.tymed = TYMED_ENHMF;
244 } else if (mimetype == wmf_mime_type) {
245 formatetc.cfFormat = CF_METAFILEPICT;
246 formatetc.tymed = TYMED_MFPICT;
249 formatetc.dwAspect = DVASPECT_CONTENT;
250 formatetc.lindex = -1;
255 class QWindowsMimeMetafile : public QWindowsMime {
257 QWindowsMimeMetafile() {}
259 bool canConvertFromMime(FORMATETC const & formatetc,
260 QMimeData const * mimedata) const
265 bool canConvertToMime(QString const & mimetype,
266 IDataObject * pDataObj) const
268 if (mimetype != emf_mime_type && mimetype != wmf_mime_type)
270 FORMATETC formatetc = cfFromMime(mimetype);
271 return pDataObj->QueryGetData(&formatetc) == S_OK;
274 bool convertFromMime(FORMATETC const & formatetc,
275 const QMimeData * mimedata, STGMEDIUM * pmedium) const
280 QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj,
281 QVariant::Type preferredType) const
284 if (!canConvertToMime(mimetype, pDataObj))
287 FORMATETC formatetc = cfFromMime(mimetype);
289 if (pDataObj->GetData(&formatetc, &s) != S_OK)
293 if (s.tymed == TYMED_ENHMF) {
294 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, 0, 0);
295 data.resize(dataSize);
296 dataSize = GetEnhMetaFileBits(s.hEnhMetaFile, dataSize,
297 (LPBYTE)data.data());
298 } else if (s.tymed == TYMED_MFPICT) {
299 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, 0, 0);
300 data.resize(dataSize);
301 dataSize = GetMetaFileBitsEx((HMETAFILE)s.hMetaFilePict, dataSize,
302 (LPBYTE)data.data());
305 ReleaseStgMedium(&s);
311 QVector<FORMATETC> formatsForMime(QString const & mimeType,
312 QMimeData const * mimeData) const
314 QVector<FORMATETC> formats;
315 formats += cfFromMime(mimeType);
319 QString mimeForFormat(FORMATETC const & formatetc) const
321 switch (formatetc.cfFormat) {
323 return emf_mime_type;
324 case CF_METAFILEPICT:
325 return wmf_mime_type;
333 ////////////////////////////////////////////////////////////////////////
334 // GuiApplication::Private definition and implementation.
335 ////////////////////////////////////////////////////////////////////////
337 struct GuiApplication::Private
340 : language_model_(0), global_menubar_(0)
343 // Create the global default menubar which is shown for the dialogs
344 // and if no GuiView is visible.
345 global_menubar_ = new GlobalMenuBar();
350 QSortFilterProxyModel * language_model_;
352 GuiClipboard clipboard_;
354 GuiSelection selection_;
356 FontLoader font_loader_;
358 ColorCache color_cache_;
360 QTranslator qt_trans_;
362 QHash<int, SocketNotifier *> socket_notifiers_;
365 /// this timer is used for any regular events one wants to
366 /// perform. at present it is used to check if forked processes
368 QTimer general_timer_;
370 /// Multiple views container.
372 * Warning: This must not be a smart pointer as the destruction of the
373 * object is handled by Qt when the view is closed
374 * \sa Qt::WA_DeleteOnClose attribute.
376 QHash<int, GuiView *> views_;
378 /// Only used on mac.
379 GlobalMenuBar * global_menubar_;
382 /// Linkback mime handler for MacOSX.
383 QMacPasteboardMimeGraphics mac_pasteboard_mime_;
387 /// WMF Mime handler for Windows clipboard.
388 // FIXME for Windows Vista and Qt4 (see http://bugzilla.lyx.org/show_bug.cgi?id=4846)
389 // But this makes LyX crash on exit when LyX is compiled in release mode and if there
390 // is something in the clipboard.
391 QWindowsMimeMetafile wmf_mime_;
396 GuiApplication * guiApp;
398 GuiApplication::~GuiApplication()
401 closeAllLinkBackLinks();
407 GuiApplication::GuiApplication(int & argc, char ** argv)
408 : QApplication(argc, argv), current_view_(0), d(new GuiApplication::Private)
410 QString app_name = "LyX";
411 QCoreApplication::setOrganizationName(app_name);
412 QCoreApplication::setOrganizationDomain("lyx.org");
413 QCoreApplication::setApplicationName(app_name + "-" + lyx_version);
415 // FIXME: quitOnLastWindowClosed is true by default. We should have a
416 // lyxrc setting for this in order to let the application stay resident.
417 // But then we need some kind of dock icon, at least on Windows.
419 if (lyxrc.quit_on_last_window_closed)
420 setQuitOnLastWindowClosed(false);
423 // FIXME: Do we need a lyxrc setting for this on Mac? This behaviour
424 // seems to be the default case for applications like LyX.
425 setQuitOnLastWindowClosed(false);
429 // doubleClickInterval() is 400 ms on X11 which is just too long.
430 // On Windows and Mac OS X, the operating system's value is used.
431 // On Microsoft Windows, calling this function sets the double
432 // click interval for all applications. So we don't!
433 QApplication::setDoubleClickInterval(300);
436 // install translation file for Qt built-in dialogs
437 QString language_name = QString("qt_") + QLocale::system().name();
439 // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN).
440 // Short-named translator can be loaded from a long name, but not the
441 // opposite. Therefore, long name should be used without truncation.
442 // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
443 if (d->qt_trans_.load(language_name,
444 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
446 installTranslator(&d->qt_trans_);
447 // even if the language calls for RtL, don't do that
448 setLayoutDirection(Qt::LeftToRight);
449 LYXERR(Debug::GUI, "Successfully installed Qt translations for locale "
452 LYXERR(Debug::GUI, "Could not find Qt translations for locale "
456 // This allows to translate the strings that appear in the LyX menu.
457 /// A translator suitable for the entries in the LyX menu.
458 /// Only needed with Qt/Mac.
459 installTranslator(new MenuTranslator(this));
461 connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onLastWindowClosed()));
463 using namespace lyx::graphics;
465 Image::newImage = boost::bind(&GuiImage::newImage);
466 Image::loadableFormats = boost::bind(&GuiImage::loadableFormats);
468 // needs to be done before reading lyxrc
470 lyxrc.dpi = (w.logicalDpiX() + w.logicalDpiY()) / 2;
474 // Set the cache to 5120 kilobytes which corresponds to screen size of
475 // 1280 by 1024 pixels with a color depth of 32 bits.
476 QPixmapCache::setCacheLimit(5120);
478 // Initialize RC Fonts
479 if (lyxrc.roman_font_name.empty())
480 lyxrc.roman_font_name = fromqstr(romanFontName());
482 if (lyxrc.sans_font_name.empty())
483 lyxrc.sans_font_name = fromqstr(sansFontName());
485 if (lyxrc.typewriter_font_name.empty())
486 lyxrc.typewriter_font_name = fromqstr(typewriterFontName());
488 d->general_timer_.setInterval(500);
489 connect(&d->general_timer_, SIGNAL(timeout()),
490 this, SLOT(handleRegularEvents()));
491 d->general_timer_.start();
495 bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
501 case LFUN_WINDOW_CLOSE:
502 enable = d->views_.size() > 0;
505 case LFUN_BUFFER_NEW:
506 case LFUN_BUFFER_NEW_TEMPLATE:
508 case LFUN_SCREEN_FONT_UPDATE:
510 case LFUN_WINDOW_NEW:
526 bool GuiApplication::dispatch(FuncRequest const & cmd)
528 switch (cmd.action) {
530 case LFUN_WINDOW_NEW:
531 createView(toqstr(cmd.argument()));
534 case LFUN_WINDOW_CLOSE:
535 // update bookmark pit of the current buffer before window close
536 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
537 theLyXFunc().gotoBookmark(i+1, false, false);
538 current_view_->close();
542 // quitting is triggered by the gui code
543 // (leaving the event loop).
545 current_view_->message(from_utf8(N_("Exiting.")));
550 case LFUN_SCREEN_FONT_UPDATE: {
551 // handle the screen font changes.
552 d->font_loader_.update();
553 // Backup current_view_
554 GuiView * view = current_view_;
555 // Set current_view_ to zero to forbid GuiWorkArea::redraw()
556 // to skip the refresh.
558 BufferList::iterator it = theBufferList().begin();
559 BufferList::iterator const end = theBufferList().end();
560 for (; it != end; ++it)
562 // Restore current_view_
563 current_view_ = view;
567 case LFUN_BUFFER_NEW:
568 if (d->views_.empty()
569 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
570 createView(QString(), false); // keep hidden
571 current_view_->newDocument(to_utf8(cmd.argument()), false);
572 current_view_->show();
573 setActiveWindow(current_view_);
575 current_view_->newDocument(to_utf8(cmd.argument()), false);
579 case LFUN_BUFFER_NEW_TEMPLATE:
580 if (d->views_.empty()
581 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
583 current_view_->newDocument(to_utf8(cmd.argument()), true);
584 if (!current_view_->buffer())
585 current_view_->close();
587 current_view_->newDocument(to_utf8(cmd.argument()), true);
592 if (d->views_.empty()
593 || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
595 current_view_->openDocument(to_utf8(cmd.argument()));
596 if (!current_view_->buffer())
597 current_view_->close();
599 current_view_->openDocument(to_utf8(cmd.argument()));
602 case LFUN_SET_COLOR: {
604 string const x11_name = split(to_utf8(cmd.argument()), lyx_name, ' ');
605 if (lyx_name.empty() || x11_name.empty()) {
606 current_view_->message(
607 _("Syntax: set-color <lyx_name> <x11_name>"));
611 string const graphicsbg = lcolor.getLyXName(Color_graphicsbg);
612 bool const graphicsbg_changed = lyx_name == graphicsbg
613 && x11_name != graphicsbg;
614 if (graphicsbg_changed) {
615 // FIXME: The graphics cache no longer has a changeDisplay method.
617 graphics::GCache::get().changeDisplay(true);
621 if (!lcolor.setColor(lyx_name, x11_name)) {
622 current_view_->message(
623 bformat(_("Set-color \"%1$s\" failed "
624 "- color is undefined or "
625 "may not be redefined"),
626 from_utf8(lyx_name)));
629 // Make sure we don't keep old colors in cache.
630 d->color_cache_.clear();
635 // Notify the caller that the action has not been dispatched.
639 // The action has been dispatched.
644 void GuiApplication::resetGui()
646 QHash<int, GuiView *>::iterator it;
647 for (it = d->views_.begin(); it != d->views_.end(); ++it)
648 (*it)->resetDialogs();
650 dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
654 void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
656 // release the keyboard which might have been grabed by the global
657 // menubar on Mac to catch shortcuts even without any GuiView.
658 if (d->global_menubar_)
659 d->global_menubar_->releaseKeyboard();
663 while (d->views_.find(id) != d->views_.end())
665 GuiView * view = new GuiView(id);
667 // copy the icon size from old view
669 view->setIconSize(current_view_->iconSize());
672 d->views_[id] = view;
676 setActiveWindow(view);
679 if (!geometry_arg.isEmpty()) {
683 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
684 re.indexIn(geometry_arg);
685 w = re.cap(1).toInt();
686 h = re.cap(2).toInt();
687 x = re.cap(3).toInt();
688 y = re.cap(4).toInt();
689 view->setGeometry(x, y, w, h);
693 setCurrentView(view);
697 Clipboard & GuiApplication::clipboard()
699 return d->clipboard_;
703 Selection & GuiApplication::selection()
705 return d->selection_;
709 FontLoader & GuiApplication::fontLoader()
711 return d->font_loader_;
715 Menus const & GuiApplication::menus() const
721 Menus & GuiApplication::menus()
727 QList<int> GuiApplication::viewIds() const
729 return d->views_.keys();
733 ColorCache & GuiApplication::colorCache()
735 return d->color_cache_;
739 int GuiApplication::exec()
741 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
742 return QApplication::exec();
746 void GuiApplication::exit(int status)
748 QApplication::exit(status);
752 void GuiApplication::execBatchCommands()
755 if (!readUIFile(lyxrc.ui_file))
756 // Gives some error box here.
759 // init the global menubar on Mac. This must be done after the session
760 // was recovered to know the "last files".
761 if (d->global_menubar_)
762 d->menus_.fillMenuBar(d->global_menubar_, 0, true);
764 LyX::ref().execBatchCommands();
767 QAbstractItemModel * GuiApplication::languageModel()
769 if (d->language_model_)
770 return d->language_model_;
772 QStandardItemModel * lang_model = new QStandardItemModel(this);
773 lang_model->insertColumns(0, 1);
775 Languages::const_iterator it = languages.begin();
776 Languages::const_iterator end = languages.end();
777 for (; it != end; ++it) {
778 current_row = lang_model->rowCount();
779 lang_model->insertRows(current_row, 1);
780 QModelIndex item = lang_model->index(current_row, 0);
781 lang_model->setData(item, qt_(it->second.display()), Qt::DisplayRole);
782 lang_model->setData(item, toqstr(it->second.lang()), Qt::UserRole);
784 d->language_model_ = new QSortFilterProxyModel(this);
785 d->language_model_->setSourceModel(lang_model);
786 #if QT_VERSION >= 0x040300
787 d->language_model_->setSortLocaleAware(true);
789 return d->language_model_;
793 void GuiApplication::restoreGuiSession()
795 if (!lyxrc.load_session)
798 Session & session = LyX::ref().session();
799 vector<FileName> const & lastopened = session.lastOpened().getfiles();
800 // do not add to the lastfile list since these files are restored from
801 // last session, and should be already there (regular files), or should
802 // not be added at all (help files).
803 for_each(lastopened.begin(), lastopened.end(),
804 bind(&GuiView::loadDocument, current_view_, _1, false));
806 // clear this list to save a few bytes of RAM
807 session.lastOpened().clear();
811 QString const GuiApplication::romanFontName()
814 font.setKerning(false);
815 font.setStyleHint(QFont::Serif);
816 font.setFamily("serif");
818 return QFontInfo(font).family();
822 QString const GuiApplication::sansFontName()
825 font.setKerning(false);
826 font.setStyleHint(QFont::SansSerif);
827 font.setFamily("sans");
829 return QFontInfo(font).family();
833 QString const GuiApplication::typewriterFontName()
836 font.setKerning(false);
837 font.setStyleHint(QFont::TypeWriter);
838 font.setFamily("monospace");
840 return QFontInfo(font).family();
844 void GuiApplication::handleRegularEvents()
846 ForkedCallsController::handleCompletedProcesses();
850 bool GuiApplication::event(QEvent * e)
853 case QEvent::FileOpen: {
854 // Open a file; this happens only on Mac OS X for now
855 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
857 if (!current_view_ || !current_view_->view())
858 // The application is not properly initialized yet.
859 // So we acknowledge the event and delay the file opening
860 // until LyX is ready.
861 // FIXME UNICODE: FileName accept an utf8 encoded string.
862 LyX::ref().addFileToLoad(fromqstr(foe->file()));
864 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
865 qstring_to_ucs4(foe->file())));
871 return QApplication::event(e);
876 bool GuiApplication::notify(QObject * receiver, QEvent * event)
879 return QApplication::notify(receiver, event);
881 catch (ExceptionMessage const & e) {
884 LyX::cref().emergencyCleanup();
885 setQuitOnLastWindowClosed(false);
887 Alert::error(e.title_, e.details_);
889 // Properly crash in debug mode in order to get a useful backtrace.
892 // In release mode, try to exit gracefully.
895 case BufferException: {
896 Buffer * buf = current_view_->buffer();
897 docstring details = e.details_ + '\n';
898 details += theBufferList().emergencyWrite(buf);
899 theBufferList().release(buf);
900 details += "\n" + _("The current document was closed.");
901 Alert::error(e.title_, details);
904 case WarningException:
905 Alert::warning(e.title_, e.details_);
909 catch (exception const & e) {
910 docstring s = _("LyX has caught an exception, it will now "
911 "attempt to save all unsaved documents and exit."
913 s += from_ascii(e.what());
914 Alert::error(_("Software exception Detected"), s);
918 docstring s = _("LyX has caught some really weird exception, it will "
919 "now attempt to save all unsaved documents and exit.");
920 Alert::error(_("Software exception Detected"), s);
928 bool GuiApplication::getRgbColor(ColorCode col, RGBColor & rgbcol)
930 QColor const & qcol = d->color_cache_.get(col);
931 if (!qcol.isValid()) {
937 rgbcol.r = qcol.red();
938 rgbcol.g = qcol.green();
939 rgbcol.b = qcol.blue();
944 string const GuiApplication::hexName(ColorCode col)
946 return ltrim(fromqstr(d->color_cache_.get(col).name()), "#");
950 void GuiApplication::registerSocketCallback(int fd, SocketCallback func)
952 SocketNotifier * sn = new SocketNotifier(this, fd, func);
953 d->socket_notifiers_[fd] = sn;
954 connect(sn, SIGNAL(activated(int)), this, SLOT(socketDataReceived(int)));
958 void GuiApplication::socketDataReceived(int fd)
960 d->socket_notifiers_[fd]->func_();
964 void GuiApplication::unregisterSocketCallback(int fd)
966 d->socket_notifiers_.take(fd)->setEnabled(false);
970 void GuiApplication::commitData(QSessionManager & sm)
972 /// The implementation is required to avoid an application exit
973 /// when session state save is triggered by session manager.
974 /// The default implementation sends a close event to all
975 /// visible top level widgets when session managment allows
977 /// We are changing that to close all wiew one by one.
978 /// FIXME: verify if the default implementation is enough now.
979 if (sm.allowsInteraction() && !closeAllViews())
984 void GuiApplication::unregisterView(GuiView * gv)
986 LASSERT(d->views_[gv->id()] == gv, /**/);
987 d->views_.remove(gv->id());
988 if (current_view_ == gv) {
990 theLyXFunc().setLyXView(0);
995 bool GuiApplication::closeAllViews()
997 if (d->views_.empty())
1000 QList<GuiView *> views = d->views_.values();
1001 foreach (GuiView * view, views) {
1011 GuiView & GuiApplication::view(int id) const
1013 LASSERT(d->views_.contains(id), /**/);
1014 return *d->views_.value(id);
1018 void GuiApplication::hideDialogs(string const & name, Inset * inset) const
1020 QList<GuiView *> views = d->views_.values();
1021 foreach (GuiView * view, views)
1022 view->hideDialog(name, inset);
1026 Buffer const * GuiApplication::updateInset(Inset const * inset) const
1028 Buffer const * buffer_ = 0;
1029 QHash<int, GuiView *>::iterator end = d->views_.end();
1030 for (QHash<int, GuiView *>::iterator it = d->views_.begin(); it != end; ++it) {
1031 if (Buffer const * ptr = (*it)->updateInset(inset))
1038 void GuiApplication::readMenus(Lexer & lex)
1044 bool GuiApplication::searchMenu(FuncRequest const & func,
1045 vector<docstring> & names) const
1047 return menus().searchMenu(func, names);
1051 bool GuiApplication::readUIFile(string const & name, bool include)
1061 LexerKeyword uitags[] = {
1062 { "include", ui_include },
1063 { "menuset", ui_menuset },
1064 { "toolbars", ui_toolbars },
1065 { "toolbarset", ui_toolbarset }
1068 // Ensure that a file is read only once (prevents include loops)
1069 static list<string> uifiles;
1070 list<string>::const_iterator it = uifiles.begin();
1071 list<string>::const_iterator end = uifiles.end();
1072 it = find(it, end, name);
1074 LYXERR(Debug::INIT, "UI file '" << name << "' has been read already. "
1075 << "Is this an include loop?");
1079 LYXERR(Debug::INIT, "About to read " << name << "...");
1084 ui_path = libFileSearch("ui", toqstr(name), "inc");
1085 if (ui_path.empty())
1086 ui_path = libFileSearch("ui",
1087 changeExtension(toqstr(name), "inc"));
1090 ui_path = libFileSearch("ui", toqstr(name), "ui");
1092 if (ui_path.empty()) {
1093 LYXERR(Debug::INIT, "Could not find " << name);
1094 Alert::warning(_("Could not find UI defintion file"),
1095 bformat(_("Error while reading the configuration file\n%1$s.\n"
1096 "Please check your installation."), from_utf8(name)));
1100 uifiles.push_back(name);
1102 LYXERR(Debug::INIT, "Found " << name << " in " << ui_path);
1104 lex.setFile(ui_path);
1106 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
1110 if (lyxerr.debugging(Debug::PARSER))
1111 lex.printTable(lyxerr);
1113 while (lex.isOK()) {
1114 switch (lex.lex()) {
1117 string const file = lex.getString();
1118 if (!readUIFile(file, true))
1127 toolbarbackend.readToolbars(lex);
1131 toolbarbackend.readToolbarSettings(lex);
1135 if (!rtrim(lex.getString()).empty())
1136 lex.printError("LyX::ReadUIFile: "
1137 "Unknown menu tag: `$$Token'");
1145 void GuiApplication::onLastWindowClosed()
1147 if (d->global_menubar_)
1148 d->global_menubar_->grabKeyboard();
1152 ////////////////////////////////////////////////////////////////////////
1154 // X11 specific stuff goes here...
1157 bool GuiApplication::x11EventFilter(XEvent * xev)
1162 switch (xev->type) {
1163 case SelectionRequest: {
1164 if (xev->xselectionrequest.selection != XA_PRIMARY)
1166 LYXERR(Debug::GUI, "X requested selection.");
1167 BufferView * bv = current_view_->view();
1169 docstring const sel = bv->requestSelection();
1171 d->selection_.put(sel);
1175 case SelectionClear: {
1176 if (xev->xselectionclear.selection != XA_PRIMARY)
1178 LYXERR(Debug::GUI, "Lost selection.");
1179 BufferView * bv = current_view_->view();
1181 bv->clearSelection();
1189 } // namespace frontend
1192 void hideDialogs(std::string const & name, Inset * inset)
1195 theApp()->hideDialogs(name, inset);
1199 ////////////////////////////////////////////////////////////////////
1203 ////////////////////////////////////////////////////////////////////
1205 frontend::FontLoader & theFontLoader()
1207 LASSERT(frontend::guiApp, /**/);
1208 return frontend::guiApp->fontLoader();
1212 frontend::FontMetrics const & theFontMetrics(Font const & f)
1214 return theFontMetrics(f.fontInfo());
1218 frontend::FontMetrics const & theFontMetrics(FontInfo const & f)
1220 LASSERT(frontend::guiApp, /**/);
1221 return frontend::guiApp->fontLoader().metrics(f);
1225 ////////////////////////////////////////////////////////////////////
1229 ////////////////////////////////////////////////////////////////////
1231 frontend::Clipboard & theClipboard()
1233 LASSERT(frontend::guiApp, /**/);
1234 return frontend::guiApp->clipboard();
1238 frontend::Selection & theSelection()
1240 LASSERT(frontend::guiApp, /**/);
1241 return frontend::guiApp->selection();
1246 #include "GuiApplication_moc.cpp"