]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSelection.cpp
GuiSelection: make sure the initial check is done
[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
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 using support::internalLineEnding;
31 using support::externalLineEnding;
32
33
34 GuiSelection::GuiSelection()
35         : selection_supported_(qApp->clipboard()->supportsSelection()),
36         schedule_check_(true)
37 {
38         connect(qApp->clipboard(), SIGNAL(selectionChanged()),
39                 this, SLOT(on_dataChanged()));
40         // initialize clipboard status.
41         on_dataChanged();
42 }
43
44
45 void GuiSelection::haveSelection(bool own)
46 {
47         if (!qApp->clipboard()->supportsSelection())
48                 return;
49
50         // Tell qt that we have a selection by setting a dummy selection.
51         // We don't use the interface provided by Qt for setting the
52         // selection for performance reasons (see documentation of
53         // Selection::put()). Instead we only tell here that we have a
54         // selection by setting the selection to the empty string.
55         // The real selection is set in GuiApplication::x11EventFilter when
56         // an application actually requests it.
57         // This way calling Selection::have() is cheap and we can do it as
58         // often as we want.
59         //LYXERR(Debug::SELECTION, "GuiSelection: setting dummy selection");
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::SELECTION, "GuiSelection::get: " << str);
77         if (str.isNull())
78                 return docstring();
79
80         return internalLineEnding(qstring_to_ucs4(str));
81 }
82
83
84 void GuiSelection::put(docstring const & str)
85 {
86         LYXERR(Debug::SELECTION, "GuiSelection::put: " << to_utf8(str));
87
88         qApp->clipboard()->setText(toqstr(externalLineEnding(str)),
89                                    QClipboard::Selection);
90 }
91
92
93 void GuiSelection::on_dataChanged()
94 {
95         schedule_check_ = true;
96         LYXERR(Debug::SELECTION, "GuiSelection::on_dataChanged");
97 }
98
99
100 bool GuiSelection::empty() const
101 {
102         if (!selection_supported_)
103                 return true;
104
105         // Cache which is to speed up selection-status read
106         // (4 calls when openi Edit menu).
107         static bool text_selection_empty;
108         if (schedule_check_) {
109                 text_selection_empty = qApp->clipboard()->
110                         text(QClipboard::Selection).isEmpty();
111                 schedule_check_ = false;
112         }
113
114         LYXERR(Debug::SELECTION, "GuiSelection::filled: " << !text_selection_empty);
115         return text_selection_empty;
116 }
117
118 } // namespace frontend
119 } // namespace lyx
120
121 #include "GuiSelection_moc.cpp"