]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSelection.cpp
Make GuiRef and GuiInclude subclasses of GuiCommand.
[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 GuiSelection::GuiSelection()
34         : selection_supported_(qApp->clipboard()->supportsSelection())
35 {
36         connect(qApp->clipboard(), SIGNAL(dataChanged()),
37                 this, SLOT(on_dataChanged()));
38         // initialize clipboard status.
39         on_dataChanged();
40 }
41
42
43 void GuiSelection::haveSelection(bool own)
44 {
45         if (!qApp->clipboard()->supportsSelection())
46                 return;
47
48         // Tell qt that we have a selection by setting a dummy selection.
49         // We don't use the interface provided by Qt for setting the
50         // selection for performance reasons (see documentation of
51         // Selection::put()). Instead we only tell here that we have a
52         // selection by setting the selection to the empty string.
53         // The real selection is set in GuiApplication::x11EventFilter when
54         // an application actually requests it.
55         // This way calling Selection::have() is cheap and we can do it as
56         // often as we want.
57         if (own)
58                 qApp->clipboard()->setText(QString(), QClipboard::Selection);
59         // We don't need to do anything if own = false, as this case is
60         // handled by QT.
61         // FIXME (gb): This is wrong. What is missing here is rather a call of
62         //else
63         //      qApp->clipboard()->clear(QClipboard::Selection);
64         // Since we do not issue this call we rather implement
65         // "persistent selections" as far as X is concerned.
66 }
67
68
69 docstring const GuiSelection::get() const
70 {
71         QString const str = qApp->clipboard()->text(QClipboard::Selection)
72                                 .normalized(QString::NormalizationForm_C);
73         LYXERR(Debug::ACTION) << "GuiSelection::get: " << fromqstr(str)
74                               << endl;
75         if (str.isNull())
76                 return docstring();
77
78         return internalLineEnding(qstring_to_ucs4(str));
79 }
80
81
82 void GuiSelection::put(docstring const & str)
83 {
84         LYXERR(Debug::ACTION) << "GuiSelection::put: " << to_utf8(str) << endl;
85
86         qApp->clipboard()->setText(toqstr(externalLineEnding(str)),
87                                    QClipboard::Selection);
88 }
89
90
91 void GuiSelection::on_dataChanged()
92 {
93         text_selection_empty_ = qApp->clipboard()->
94                 text(QClipboard::Selection).isEmpty();
95 }
96
97
98 bool GuiSelection::empty() const
99 {
100         if (!selection_supported_)
101                 return true;
102
103         return text_selection_empty_;
104 }
105
106 } // namespace frontend
107 } // namespace lyx
108
109 #include "GuiSelection_moc.cpp"