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