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