]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.cpp
rework a bit the document dialog so that it works more or less fine without any open...
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
1 /**
2  * \file GuiApplication.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiApplication.h"
16
17 #include "qt_helpers.h"
18 #include "GuiImage.h"
19 #include "GuiKeySymbol.h"
20 #include "GuiView.h"
21
22 #include "frontends/alert.h"
23 #include "frontends/Application.h"
24 #include "frontends/FontLoader.h"
25 #include "frontends/FontMetrics.h"
26
27 #include "Buffer.h"
28 #include "BufferList.h"
29 #include "BufferView.h"
30 #include "Font.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
33 #include "Language.h"
34 #include "LyX.h"
35 #include "LyXFunc.h"
36 #include "LyXRC.h"
37 #include "Session.h"
38 #include "version.h"
39
40 #include "support/lassert.h"
41 #include "support/debug.h"
42 #include "support/ExceptionMessage.h"
43 #include "support/FileName.h"
44 #include "support/ForkedCalls.h"
45 #include "support/gettext.h"
46 #include "support/lstrings.h"
47 #include "support/os.h"
48 #include "support/Package.h"
49
50 #include <QClipboard>
51 #include <QEventLoop>
52 #include <QFileOpenEvent>
53 #include <QLocale>
54 #include <QLibraryInfo>
55 #include <QMenuBar>
56 #include <QPixmapCache>
57 #include <QRegExp>
58 #include <QSessionManager>
59 #include <QSocketNotifier>
60 #include <QSortFilterProxyModel>
61 #include <QStandardItemModel>
62 #include <QTextCodec>
63 #include <QTimer>
64 #include <QTranslator>
65 #include <QWidget>
66
67 #ifdef Q_WS_X11
68 #include <X11/Xatom.h>
69 #include <X11/Xlib.h>
70 #undef CursorShape
71 #undef None
72 #endif
73
74 #include <boost/bind.hpp>
75
76 #include <exception>
77
78 using namespace std;
79 using namespace lyx::support;
80
81 namespace lyx {
82
83 frontend::Application * createApplication(int & argc, char * argv[])
84 {
85         return new frontend::GuiApplication(argc, argv);
86 }
87
88
89 namespace frontend {
90
91 class SocketNotifier : public QSocketNotifier
92 {
93 public:
94         /// connect a connection notification from the LyXServerSocket
95         SocketNotifier(QObject * parent, int fd, Application::SocketCallback func)
96                 : QSocketNotifier(fd, QSocketNotifier::Read, parent), func_(func)
97         {}
98
99 public:
100         /// The callback function
101         Application::SocketCallback func_;
102 };
103
104
105 ////////////////////////////////////////////////////////////////////////
106 // Mac specific stuff goes here...
107
108 class MenuTranslator : public QTranslator
109 {
110 public:
111         MenuTranslator(QObject * parent)
112                 : QTranslator(parent)
113         {}
114
115         QString translate(const char * /*context*/, 
116           const char * sourceText, 
117           const char * /*comment*/ = 0) 
118         {
119                 string const s = sourceText;
120                 if (s == N_("About %1") || s == N_("Preferences") 
121                                 || s == N_("Reconfigure") || s == N_("Quit %1"))
122                         return qt_(s);
123                 else 
124                         return QString();
125         }
126 };
127
128 class GlobalMenuBar : public QMenuBar
129 {
130 public:
131         ///
132         GlobalMenuBar() : QMenuBar(0) {}
133         
134         ///
135         bool event(QEvent * e)
136         {
137                 if (e->type() == QEvent::ShortcutOverride) {
138                         //          && activeWindow() == 0) {
139                         QKeyEvent * ke = static_cast<QKeyEvent*>(e);
140                         KeySymbol sym;
141                         setKeySymbol(&sym, ke);
142                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
143                         e->accept();
144                         return true;
145                 }
146                 return false;
147         }
148 };
149
150 ///////////////////////////////////////////////////////////////
151 // You can find more platform specific stuff
152 // at the end of this file...
153 ///////////////////////////////////////////////////////////////
154
155
156 GuiApplication * guiApp;
157
158
159 GuiApplication::GuiApplication(int & argc, char ** argv)
160         : QApplication(argc, argv), Application(), language_model_(0),
161         current_view_(0), global_menubar_(0)
162 {
163         QString app_name = "LyX";
164         QCoreApplication::setOrganizationName(app_name);
165         QCoreApplication::setOrganizationDomain("lyx.org");
166         QCoreApplication::setApplicationName(app_name + "-" + lyx_version);
167
168         // FIXME: quitOnLastWindowClosed is true by default. We should have a
169         // lyxrc setting for this in order to let the application stay resident.
170         // But then we need some kind of dock icon, at least on Windows.
171         /*
172         if (lyxrc.quit_on_last_window_closed)
173                 setQuitOnLastWindowClosed(false);
174         */
175 #ifdef Q_WS_MACX
176         // FIXME: Do we need a lyxrc setting for this on Mac? This behaviour
177         // seems to be the default case for applications like LyX.
178         setQuitOnLastWindowClosed(false);
179 #endif
180         
181 #ifdef Q_WS_X11
182         // doubleClickInterval() is 400 ms on X11 which is just too long.
183         // On Windows and Mac OS X, the operating system's value is used.
184         // On Microsoft Windows, calling this function sets the double
185         // click interval for all applications. So we don't!
186         QApplication::setDoubleClickInterval(300);
187 #endif
188
189         // install translation file for Qt built-in dialogs
190         QString language_name = QString("qt_") + QLocale::system().name();
191         
192         // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN). 
193         // Short-named translator can be loaded from a long name, but not the
194         // opposite. Therefore, long name should be used without truncation.
195         // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
196         if (qt_trans_.load(language_name,
197                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
198         {
199                 installTranslator(&qt_trans_);
200                 // even if the language calls for RtL, don't do that
201                 setLayoutDirection(Qt::LeftToRight);
202                 LYXERR(Debug::GUI, "Successfully installed Qt translations for locale "
203                         << language_name);
204         } else
205                 LYXERR(Debug::GUI, "Could not find  Qt translations for locale "
206                         << language_name);
207
208 #ifdef Q_WS_MACX
209         // This allows to translate the strings that appear in the LyX menu.
210         addMenuTranslator();
211 #endif
212         connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onLastWindowClosed()));
213
214         using namespace lyx::graphics;
215
216         Image::newImage = boost::bind(&GuiImage::newImage);
217         Image::loadableFormats = boost::bind(&GuiImage::loadableFormats);
218
219         // needs to be done before reading lyxrc
220         QWidget w;
221         lyxrc.dpi = (w.logicalDpiX() + w.logicalDpiY()) / 2;
222
223         guiApp = this;
224
225         // Set the cache to 5120 kilobytes which corresponds to screen size of
226         // 1280 by 1024 pixels with a color depth of 32 bits.
227         QPixmapCache::setCacheLimit(5120);
228
229         // Initialize RC Fonts
230         if (lyxrc.roman_font_name.empty())
231                 lyxrc.roman_font_name = fromqstr(romanFontName());
232
233         if (lyxrc.sans_font_name.empty())
234                 lyxrc.sans_font_name = fromqstr(sansFontName());
235
236         if (lyxrc.typewriter_font_name.empty())
237                 lyxrc.typewriter_font_name = fromqstr(typewriterFontName());
238
239         general_timer_.setInterval(500);
240         connect(&general_timer_, SIGNAL(timeout()),
241                 this, SLOT(handleRegularEvents()));
242         general_timer_.start();
243
244         
245 #ifdef Q_WS_MACX
246         if (global_menubar_ == 0) {
247                 // Create the global default menubar which is shown for the dialogs
248                 // and if no GuiView is visible.
249                 global_menubar_ = new GlobalMenuBar();
250         }
251 #endif
252 }
253
254
255 FuncStatus GuiApplication::getStatus(FuncRequest const & cmd)
256 {
257         FuncStatus flag;
258         bool enable = true;
259
260         switch(cmd.action) {
261
262         case LFUN_WINDOW_CLOSE:
263                 enable = view_ids_.size() > 0;
264                 break;
265
266         default:
267                 if (!current_view_) {
268                         enable = false;
269                         break;
270                 }
271         }
272
273         if (!enable)
274                 flag.enabled(false);
275
276         return flag;
277 }
278
279         
280 bool GuiApplication::dispatch(FuncRequest const & cmd)
281 {
282         switch(cmd.action) {
283
284         case LFUN_WINDOW_NEW:
285                 createView(toqstr(cmd.argument()));
286                 break;
287
288         case LFUN_WINDOW_CLOSE:
289                 // update bookmark pit of the current buffer before window close
290                 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
291                         theLyXFunc().gotoBookmark(i+1, false, false);
292                 current_view_->close();
293                 break;
294
295         case LFUN_LYX_QUIT:
296                 // quitting is triggered by the gui code
297                 // (leaving the event loop).
298                 if (current_view_)
299                         current_view_->message(from_utf8(N_("Exiting.")));
300                 if (closeAllViews())
301                         quit();
302                 break;
303
304         case LFUN_SCREEN_FONT_UPDATE: {
305                 // handle the screen font changes.
306                 font_loader_.update();
307                 // Backup current_view_
308                 GuiView * view = current_view_;
309                 // Set current_view_ to zero to forbid GuiWorkArea::redraw()
310                 // to skip the refresh.
311                 current_view_ = 0;
312                 BufferList::iterator it = theBufferList().begin();
313                 BufferList::iterator const end = theBufferList().end();
314                 for (; it != end; ++it)
315                         (*it)->changed();
316                 // Restore current_view_
317                 current_view_ = view;
318                 break;
319         }
320
321         case LFUN_BUFFER_NEW:
322                 if (viewCount() == 0
323                     || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
324                         createView(QString(), false); // keep hidden
325                         current_view_->newDocument(to_utf8(cmd.argument()), false);
326                         current_view_->show();
327                         setActiveWindow(current_view_);
328                 } else
329                         current_view_->newDocument(to_utf8(cmd.argument()), false);
330                 break;
331
332         case LFUN_BUFFER_NEW_TEMPLATE:
333                 if (viewCount() == 0 
334                     || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
335                         createView();
336                         current_view_->newDocument(to_utf8(cmd.argument()), true);
337                         if (!current_view_->buffer())
338                                 current_view_->close();
339                 } else
340                         current_view_->newDocument(to_utf8(cmd.argument()), true);
341                 break;
342
343         case LFUN_FILE_OPEN:
344                 if (viewCount() == 0
345                     || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
346                         createView();
347                         current_view_->openDocument(to_utf8(cmd.argument()));
348                         if (!current_view_->buffer())
349                                 current_view_->close();
350                 } else
351                         current_view_->openDocument(to_utf8(cmd.argument()));
352                 break;
353
354         default:
355                 // Notify the caller that the action has not been dispatched.
356                 return false;
357         }
358
359         // The action has been dispatched.
360         return true;
361 }
362
363
364 void GuiApplication::resetGui()
365 {
366         map<int, GuiView *>::iterator it;
367         for (it = views_.begin(); it != views_.end(); ++it)
368                 it->second->resetDialogs();
369
370         dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
371 }
372
373
374 static void updateIds(map<int, GuiView *> const & stdmap, vector<int> & ids)
375 {
376         ids.clear();
377         map<int, GuiView *>::const_iterator it;
378         for (it = stdmap.begin(); it != stdmap.end(); ++it)
379                 ids.push_back(it->first);
380 }
381
382
383 void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
384 {
385         // release the keyboard which might have been grabed by the global
386         // menubar on Mac to catch shortcuts even without any GuiView.
387         if (global_menubar_)
388                 global_menubar_->releaseKeyboard();
389
390         // create new view
391         updateIds(views_, view_ids_);
392         int id = 0;
393         while (views_.find(id) != views_.end())
394                 id++;
395         GuiView * view = new GuiView(id);
396         
397         // copy the icon size from old view
398         if (viewCount() > 0)
399                 view->setIconSize(current_view_->iconSize());
400
401         // register view
402         views_[id] = view;
403         updateIds(views_, view_ids_);
404
405         if (autoShow) {
406                 view->show();
407                 setActiveWindow(view);
408         }
409
410         if (!geometry_arg.isEmpty()) {
411 #ifdef Q_WS_WIN
412                 int x, y;
413                 int w, h;
414                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
415                 re.indexIn(geometry_arg);
416                 w = re.cap(1).toInt();
417                 h = re.cap(2).toInt();
418                 x = re.cap(3).toInt();
419                 y = re.cap(4).toInt();
420                 view->setGeometry(x, y, w, h);
421 #endif
422         }
423         view->setFocus();
424         setCurrentView(view);
425 }
426
427
428
429
430 Clipboard & GuiApplication::clipboard()
431 {
432         return clipboard_;
433 }
434
435
436 Selection & GuiApplication::selection()
437 {
438         return selection_;
439 }
440
441
442 int GuiApplication::exec()
443 {
444         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
445         return QApplication::exec();
446 }
447
448
449 void GuiApplication::exit(int status)
450 {
451         QApplication::exit(status);
452 }
453
454
455 void GuiApplication::execBatchCommands()
456 {
457         LyX::ref().execBatchCommands();
458 }
459
460 QAbstractItemModel * GuiApplication::languageModel()
461 {
462         if (language_model_)
463                 return language_model_;
464
465         QStandardItemModel * lang_model = new QStandardItemModel(this);
466         lang_model->insertColumns(0, 1);
467         int current_row;
468         Languages::const_iterator it = languages.begin();
469         Languages::const_iterator end = languages.end();
470         for (; it != end; ++it) {
471                 current_row = lang_model->rowCount();
472                 lang_model->insertRows(current_row, 1);
473                 QModelIndex item = lang_model->index(current_row, 0);
474                 lang_model->setData(item, qt_(it->second.display()), Qt::DisplayRole);
475                 lang_model->setData(item, toqstr(it->second.lang()), Qt::UserRole);
476         }
477         language_model_ = new QSortFilterProxyModel(this);
478         language_model_->setSourceModel(lang_model);
479         language_model_->setSortLocaleAware(true);
480         return language_model_;
481 }
482
483
484 void GuiApplication::restoreGuiSession()
485 {
486         if (!lyxrc.load_session)
487                 return;
488
489         Session & session = LyX::ref().session();
490         vector<FileName> const & lastopened = session.lastOpened().getfiles();
491         // do not add to the lastfile list since these files are restored from
492         // last session, and should be already there (regular files), or should
493         // not be added at all (help files).
494         for_each(lastopened.begin(), lastopened.end(),
495                 bind(&GuiView::loadDocument, current_view_, _1, false));
496
497         // clear this list to save a few bytes of RAM
498         session.lastOpened().clear();
499 }
500
501
502 QString const GuiApplication::romanFontName()
503 {
504         QFont font;
505         font.setKerning(false);
506         font.setStyleHint(QFont::Serif);
507         font.setFamily("serif");
508
509         return QFontInfo(font).family();
510 }
511
512
513 QString const GuiApplication::sansFontName()
514 {
515         QFont font;
516         font.setKerning(false);
517         font.setStyleHint(QFont::SansSerif);
518         font.setFamily("sans");
519
520         return QFontInfo(font).family();
521 }
522
523
524 QString const GuiApplication::typewriterFontName()
525 {
526         QFont font;
527         font.setKerning(false);
528         font.setStyleHint(QFont::TypeWriter);
529         font.setFamily("monospace");
530
531         return QFontInfo(font).family();
532 }
533
534
535 void GuiApplication::handleRegularEvents()
536 {
537         ForkedCallsController::handleCompletedProcesses();
538 }
539
540
541 bool GuiApplication::event(QEvent * e)
542 {
543         switch(e->type()) {
544         case QEvent::FileOpen: {
545                 // Open a file; this happens only on Mac OS X for now
546                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
547
548                 if (!current_view_ || !current_view_->view())
549                         // The application is not properly initialized yet.
550                         // So we acknowledge the event and delay the file opening
551                         // until LyX is ready.
552                         // FIXME UNICODE: FileName accept an utf8 encoded string.
553                         LyX::ref().addFileToLoad(fromqstr(foe->file()));
554                 else
555                         lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
556                                 qstring_to_ucs4(foe->file())));
557
558                 e->accept();
559                 return true;
560         }
561         default:
562                 return QApplication::event(e);
563         }
564 }
565
566
567 bool GuiApplication::notify(QObject * receiver, QEvent * event)
568 {
569         try {
570                 return QApplication::notify(receiver, event);
571         }
572         catch (ExceptionMessage const & e) {
573                 switch(e.type_) { 
574                 case ErrorException:
575                         LyX::cref().emergencyCleanup();
576                         setQuitOnLastWindowClosed(false);
577                         closeAllViews();
578                         Alert::error(e.title_, e.details_);
579 #ifndef NDEBUG
580                         // Properly crash in debug mode in order to get a useful backtrace.
581                         abort();
582 #endif
583                         // In release mode, try to exit gracefully.
584                         this->exit(1);
585
586                 case BufferException: {
587                         Buffer * buf = current_view_->buffer();
588                         docstring details = e.details_ + '\n';
589                         details += theBufferList().emergencyWrite(buf);
590                         theBufferList().release(buf);
591                         details += "\n" + _("The current document was closed.");
592                         Alert::error(e.title_, details);
593                         return false;
594                 }
595                 case WarningException:
596                         Alert::warning(e.title_, e.details_);
597                         return false;
598                 }
599         }
600         catch (exception const & e) {
601                 docstring s = _("LyX has caught an exception, it will now "
602                         "attempt to save all unsaved documents and exit."
603                         "\n\nException: ");
604                 s += from_ascii(e.what());
605                 Alert::error(_("Software exception Detected"), s);
606                 LyX::cref().exit(1);
607         }
608         catch (...) {
609                 docstring s = _("LyX has caught some really weird exception, it will "
610                         "now attempt to save all unsaved documents and exit.");
611                 Alert::error(_("Software exception Detected"), s);
612                 LyX::cref().exit(1);
613         }
614
615         return false;
616 }
617
618
619 bool GuiApplication::getRgbColor(ColorCode col, RGBColor & rgbcol)
620 {
621         QColor const & qcol = color_cache_.get(col);
622         if (!qcol.isValid()) {
623                 rgbcol.r = 0;
624                 rgbcol.g = 0;
625                 rgbcol.b = 0;
626                 return false;
627         }
628         rgbcol.r = qcol.red();
629         rgbcol.g = qcol.green();
630         rgbcol.b = qcol.blue();
631         return true;
632 }
633
634
635 string const GuiApplication::hexName(ColorCode col)
636 {
637         return ltrim(fromqstr(color_cache_.get(col).name()), "#");
638 }
639
640
641 void GuiApplication::updateColor(ColorCode)
642 {
643         // FIXME: Bleh, can't we just clear them all at once ?
644         color_cache_.clear();
645 }
646
647
648 void GuiApplication::registerSocketCallback(int fd, SocketCallback func)
649 {
650         SocketNotifier * sn = new SocketNotifier(this, fd, func);
651         socket_notifiers_[fd] = sn;
652         connect(sn, SIGNAL(activated(int)), this, SLOT(socketDataReceived(int)));
653 }
654
655
656 void GuiApplication::socketDataReceived(int fd)
657 {
658         socket_notifiers_[fd]->func_();
659 }
660
661
662 void GuiApplication::unregisterSocketCallback(int fd)
663 {
664         socket_notifiers_.erase(fd);
665 }
666
667
668 void GuiApplication::commitData(QSessionManager & sm)
669 {
670         /// The implementation is required to avoid an application exit
671         /// when session state save is triggered by session manager.
672         /// The default implementation sends a close event to all
673         /// visible top level widgets when session managment allows
674         /// interaction.
675         /// We are changing that to close all wiew one by one.
676         /// FIXME: verify if the default implementation is enough now.
677         if (sm.allowsInteraction() && !closeAllViews())
678                 sm.cancel();
679 }
680
681
682 void GuiApplication::addMenuTranslator()
683 {
684         installTranslator(new MenuTranslator(this));
685 }
686
687
688 bool GuiApplication::unregisterView(int id)
689 {
690         updateIds(views_, view_ids_);
691         LASSERT(views_.find(id) != views_.end(), /**/);
692         LASSERT(views_[id], /**/);
693
694         map<int, GuiView *>::iterator it;
695         for (it = views_.begin(); it != views_.end(); ++it) {
696                 if (it->first == id) {
697                         views_.erase(id);
698                         break;
699                 }
700         }
701         updateIds(views_, view_ids_);
702         return true;
703 }
704
705
706 bool GuiApplication::closeAllViews()
707 {
708         updateIds(views_, view_ids_);
709         if (views_.empty())
710                 return true;
711
712         map<int, GuiView*> const cmap = views_;
713         map<int, GuiView*>::const_iterator it;
714         for (it = cmap.begin(); it != cmap.end(); ++it) {
715                 if (!it->second->close())
716                         return false;
717         }
718
719         views_.clear();
720         view_ids_.clear();
721         return true;
722 }
723
724
725 GuiView & GuiApplication::view(int id) const
726 {
727         LASSERT(views_.find(id) != views_.end(), /**/);
728         return *views_.find(id)->second;
729 }
730
731
732 void GuiApplication::hideDialogs(string const & name, Inset * inset) const
733 {
734         vector<int>::const_iterator it = view_ids_.begin();
735         vector<int>::const_iterator const end = view_ids_.end();
736         for (; it != end; ++it)
737                 view(*it).hideDialog(name, inset);
738 }
739
740
741 Buffer const * GuiApplication::updateInset(Inset const * inset) const
742 {
743         Buffer const * buffer_ = 0;
744         vector<int>::const_iterator it = view_ids_.begin();
745         vector<int>::const_iterator const end = view_ids_.end();
746         for (; it != end; ++it) {
747                 Buffer const * ptr = view(*it).updateInset(inset);
748                 if (ptr)
749                         buffer_ = ptr;
750         }
751         return buffer_;
752 }
753
754
755 void GuiApplication::readMenus(Lexer & lex)
756 {
757         menus().read(lex);
758 }
759
760
761 bool GuiApplication::searchMenu(FuncRequest const & func,
762         vector<docstring> & names) const
763 {
764         return menus().searchMenu(func, names);
765 }
766
767
768 void GuiApplication::initGlobalMenu()
769 {
770         if (global_menubar_)
771                 menus().fillMenuBar(global_menubar_, 0, true);
772 }
773
774
775 void GuiApplication::onLastWindowClosed()
776 {
777         if (global_menubar_)
778                 global_menubar_->grabKeyboard();
779 }
780
781
782 ////////////////////////////////////////////////////////////////////////
783 //
784 // X11 specific stuff goes here...
785
786 #ifdef Q_WS_X11
787 bool GuiApplication::x11EventFilter(XEvent * xev)
788 {
789         if (!current_view_)
790                 return false;
791
792         switch (xev->type) {
793         case SelectionRequest: {
794                 if (xev->xselectionrequest.selection != XA_PRIMARY)
795                         break;
796                 LYXERR(Debug::GUI, "X requested selection.");
797                 BufferView * bv = current_view_->view();
798                 if (bv) {
799                         docstring const sel = bv->requestSelection();
800                         if (!sel.empty())
801                                 selection_.put(sel);
802                 }
803                 break;
804         }
805         case SelectionClear: {
806                 if (xev->xselectionclear.selection != XA_PRIMARY)
807                         break;
808                 LYXERR(Debug::GUI, "Lost selection.");
809                 BufferView * bv = current_view_->view();
810                 if (bv)
811                         bv->clearSelection();
812                 break;
813         }
814         }
815         return false;
816 }
817 #endif
818
819 } // namespace frontend
820
821
822 void hideDialogs(std::string const & name, Inset * inset)
823 {
824         if (theApp())
825                 theApp()->hideDialogs(name, inset);
826 }
827
828
829 ////////////////////////////////////////////////////////////////////
830 //
831 // Font stuff
832 //
833 ////////////////////////////////////////////////////////////////////
834
835 frontend::FontLoader & theFontLoader()
836 {
837         LASSERT(frontend::guiApp, /**/);
838         return frontend::guiApp->fontLoader();
839 }
840
841
842 frontend::FontMetrics const & theFontMetrics(Font const & f)
843 {
844         return theFontMetrics(f.fontInfo());
845 }
846
847
848 frontend::FontMetrics const & theFontMetrics(FontInfo const & f)
849 {
850         LASSERT(frontend::guiApp, /**/);
851         return frontend::guiApp->fontLoader().metrics(f);
852 }
853
854
855 ////////////////////////////////////////////////////////////////////
856 //
857 // Misc stuff
858 //
859 ////////////////////////////////////////////////////////////////////
860
861 frontend::Clipboard & theClipboard()
862 {
863         LASSERT(frontend::guiApp, /**/);
864         return frontend::guiApp->clipboard();
865 }
866
867
868 frontend::Selection & theSelection()
869 {
870         LASSERT(frontend::guiApp, /**/);
871         return frontend::guiApp->selection();
872 }
873
874 } // namespace lyx
875
876 #include "GuiApplication_moc.cpp"