]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/gtk/GuiSelection.C
make it compile again
[lyx.git] / src / frontends / gtk / GuiSelection.C
index cae4af7abe772b32e0d832c1cc789e494b6cb21f..c1193f1ece1736a53ae9326178a5ecdce82c2b9b 100644 (file)
 #endif
 
 #include "GuiSelection.h"
+
+#include "BufferView.h"
 #include "debug.h"
 
+#include "frontends/Application.h"
+#include "frontends/Gui.h"
+#include "frontends/LyXView.h"
+
 #include <gtkmm.h>
 
 using std::endl;
@@ -31,29 +37,56 @@ using std::string;
 namespace lyx {
 namespace frontend {
 
-// ENCODING: Gtk::Clipboard returns UTF-8, we assume that the backend
-// wants ISO-8859-1 and convert it to that.
-// FIXME: Wrong!
-string const GuiSelection::get() const
+docstring const GuiSelection::get() const
 {
        Glib::RefPtr<Gtk::Clipboard> clipboard =
                Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
-       string const str = Glib::convert_with_fallback(
-                       clipboard->wait_for_text(), "ISO-8859-1", "UTF-8");
-       lyxerr[Debug::ACTION] << "GuiClipboard::get: " << str << endl;
-       return str;
+       string const str = clipboard->wait_for_text();
+       lyxerr[Debug::ACTION] << "GuiSelection::get: " << str << endl;
+       return lyx::from_utf8(str);
 }
 
 
-// ENCODING: we assume that the backend passes us ISO-8859-1 and
-// convert from that to UTF-8 before passing to GTK
-// FIXME: Wrong!
-void GuiSelection::put(string const & str)
+void GuiSelection::put(docstring const & str)
 {
-       lyxerr[Debug::ACTION] << "GuiClipboard::put: " << str << endl;
+       string const utf8 = lyx::to_utf8(str);
+       lyxerr[Debug::ACTION] << "GuiSelection::put: " << utf8 << endl;
        Glib::RefPtr<Gtk::Clipboard> clipboard =
                Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
-       clipboard->set_text(Glib::convert(str, "UTF-8", "ISO-8859-1"));
+       clipboard->set_text(utf8);
+}
+
+
+void GuiSelection::onClipboardGet(Gtk::SelectionData & /*selection_data*/,
+               guint /*info*/)
+{
+       // FIXME: This assumes only one LyXView
+       lyx::docstring const sel = theApp->gui().view(0).view()->requestSelection();
+       if (!sel.empty())
+               put(sel);
+}
+
+
+void GuiSelection::onClipboardClear()
+{
+       // FIXME: This assumes only one LyXView
+       theApp->gui().view(0).view()->clearSelection();
+}
+
+
+void GuiSelection::haveSelection(bool toHave)
+{
+       if (toHave) {
+               Glib::RefPtr<Gtk::Clipboard> clipboard =
+                       Gtk::Clipboard::get(GDK_SELECTION_PRIMARY);
+               std::vector<Gtk::TargetEntry> listTargets;
+               listTargets.push_back(Gtk::TargetEntry("UTF8_STRING"));
+               clipboard->set(listTargets,
+                               sigc::mem_fun(const_cast<GuiSelection&>(*this),
+                                       &GuiSelection::onClipboardGet),
+                               sigc::mem_fun(const_cast<GuiSelection&>(*this),
+                                       &GuiSelection::onClipboardClear));
+       }
 }
 
 } // namespace frontend