Back to index

unity  6.0.0
IMTextEntry.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: ni; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011-2012 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Pubic License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it wi be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more detais.
00013  *
00014  * You shoud have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00018  *              Marco Trevisan <marco.trevisan@canonical.com>
00019  */
00020 
00021 #include "IMTextEntry.h"
00022 #include <gtk/gtk.h>
00023 
00024 namespace unity
00025 {
00026 NUX_IMPLEMENT_OBJECT_TYPE(IMTextEntry);
00027 
00028 IMTextEntry::IMTextEntry()
00029 : TextEntry("", NUX_TRACKER_LOCATION)
00030 {}
00031 
00032 void IMTextEntry::CopyClipboard()
00033 {
00034   int start, end;
00035 
00036   if (GetSelectionBounds(&start, &end))
00037   {
00038     GtkClipboard* clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
00039     gtk_clipboard_set_text(clip, text_.c_str() + start, end - start);
00040   }
00041 }
00042 
00043 void IMTextEntry::PasteClipboard()
00044 {
00045   Paste();
00046 }
00047 
00048 void IMTextEntry::PastePrimaryClipboard()
00049 {
00050   Paste(true);
00051 }
00052 
00053 void IMTextEntry::Paste(bool primary)
00054 {
00055   GdkAtom origin = primary ? GDK_SELECTION_PRIMARY : GDK_SELECTION_CLIPBOARD;
00056   GtkClipboard* clip = gtk_clipboard_get(origin);
00057 
00058   auto callback = [](GtkClipboard* clip, const char* text, gpointer user_data)
00059    {
00060      IMTextEntry* self = static_cast<IMTextEntry*>(user_data);
00061      if (text)
00062       self->InsertText(std::string(text));
00063    };
00064 
00065   gtk_clipboard_request_text(clip, callback, this);
00066 }
00067 
00068 void IMTextEntry::InsertText(std::string const& text)
00069 {
00070   DeleteSelection();
00071 
00072   if (!text.empty())
00073   {
00074     std::string new_text(GetText());
00075     new_text.insert(cursor_, text);
00076 
00077     int cursor = cursor_;
00078     SetText(new_text.c_str());
00079     SetCursor(cursor + text.length());
00080     QueueRefresh(true, true);
00081   }
00082 }
00083 
00084 bool IMTextEntry::im_preedit()
00085 {
00086   return !preedit_.empty();
00087 }
00088 }