]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSelection.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / GuiSelection.cpp
1 // -*- C++ -*-
2 /**
3  * \file qt4/GuiSelection.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
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 "GuiSelection.h"
16 #include "qt_helpers.h"
17
18 #include "debug.h"
19
20 #include <QApplication>
21 #include <QClipboard>
22 #include <QString>
23
24 #include "support/lstrings.h"
25 using lyx::support::internalLineEnding;
26 using lyx::support::externalLineEnding;
27
28 using std::endl;
29
30 namespace lyx {
31 namespace frontend {
32
33 void GuiSelection::haveSelection(bool own)
34 {
35         if (!qApp->clipboard()->supportsSelection())
36                 return;
37
38         // Tell qt that we have a selection by setting a dummy selection.
39         // We don't use the interface provided by Qt for setting the
40         // selection for performance reasons (see documentation of
41         // Selection::put()). Instead we only tell here that we have a
42         // selection by setting the selection to the empty string.
43         // The real selection is set in GuiApplication::x11EventFilter when
44         // an application actually requests it.
45         // This way calling Selection::have() is cheap and we can do it as
46         // often as we want.
47         if (own)
48                 qApp->clipboard()->setText(QString(), QClipboard::Selection);
49         // We don't need to do anything if own = false, as this case is
50         // handled by QT.
51         // FIXME (gb): This is wrong. What is missing here is rather a call of
52         //else
53         //      qApp->clipboard()->clear(QClipboard::Selection);
54         // Since we do not issue this call we rather implement
55         // "persistent selections" as far as X is concerned.
56 }
57
58
59 docstring const GuiSelection::get() const
60 {
61         QString const str = qApp->clipboard()->text(QClipboard::Selection)
62                                 .normalized(QString::NormalizationForm_KC);
63         LYXERR(Debug::ACTION) << "GuiSelection::get: " << fromqstr(str)
64                               << endl;
65         if (str.isNull())
66                 return docstring();
67
68         return internalLineEnding(qstring_to_ucs4(str));
69 }
70
71
72 void GuiSelection::put(docstring const & str)
73 {
74         LYXERR(Debug::ACTION) << "GuiSelection::put: " << to_utf8(str) << endl;
75
76         qApp->clipboard()->setText(toqstr(externalLineEnding(str)),
77                                    QClipboard::Selection);
78 }
79
80
81 bool GuiSelection::empty() const
82 {
83         if (!qApp->clipboard()->supportsSelection())
84                 return true;
85
86         return qApp->clipboard()->text(QClipboard::Selection).isEmpty();
87 }
88
89 } // namespace frontend
90 } // namespace lyx