#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <cstring>
#include <glib/gi18n.h>
#include "stardict.h"
#include "conf.h"
#include "iskeyspressed.hpp"
#include "selection.h"
GdkAtom Selection::TARGETS_Atom;
GdkAtom Selection::UTF8_STRING_Atom;
*/
Selection::Selection()
{
UTF8_STRING_Atom = COMPOUND_TEXT_Atom = GDK_NONE;
IsBusy = 0;
selection_widget = NULL;
timeout = 0;
}
gboolean Selection::Enable()
{
return !gpAppFrame->oTopWin.TextSelected();
}
void Selection::create_selection_widget()
{
if (selection_widget)
gtk_widget_destroy(selection_widget);
selection_widget = gtk_invisible_new ();
g_signal_connect (G_OBJECT (selection_widget), "selection_received", G_CALLBACK (SelectionReceivedCallback), this);
}
void Selection::Init()
{
UTF8_STRING_Atom = gdk_atom_intern("UTF8_STRING",false);
COMPOUND_TEXT_Atom = gdk_atom_intern("COMPOUND_TEXT",false);
create_selection_widget();
}
void Selection::End()
{
stop();
if (selection_widget)
gtk_widget_destroy(selection_widget);
}
void Selection::start()
{
if (!timeout)
timeout = g_timeout_add(SELECTION_INTERVAL,TimeOutCallback,this);
}
void Selection::stop()
{
if (timeout) {
g_source_remove(timeout);
timeout = 0;
}
LastClipWord.clear();
}
gint Selection::TimeOutCallback(gpointer data)
{
Selection *oSelection = (Selection *)data;
if (oSelection->Enable()) {
if (conf->get_bool("/apps/stardict/preferences/dictionary/only_scan_while_modifier_key")) {
bool do_scan = gpAppFrame->unlock_keys->is_pressed();
if (!do_scan)
return true;
}
if (oSelection->IsBusy) {
oSelection->IsBusy++;
if (oSelection->IsBusy*SELECTION_INTERVAL > 8000 ) {
g_warning("Error, selection data didn't received, retring!\n");
oSelection->create_selection_widget();
oSelection->IsBusy = 0;
}
} else {
oSelection->IsBusy = 1;
gtk_selection_convert (oSelection->selection_widget, GDK_SELECTION_PRIMARY, oSelection->UTF8_STRING_Atom, GDK_CURRENT_TIME);
}
}
return true;
}
void Selection::SelectionReceivedCallback(GtkWidget* widget,GtkSelectionData *selection_data, guint time, Selection *oSelection)
{
gchar *result;
result = (gchar *)gtk_selection_data_get_text (selection_data);
if (!result) {
* if we asked for compound_text and didn't get it, try string;
* If we asked for anything else and didn't get it, give up.
*/
if (selection_data->target == oSelection->UTF8_STRING_Atom) {
gtk_selection_convert (widget, GDK_SELECTION_PRIMARY, oSelection->COMPOUND_TEXT_Atom, GDK_CURRENT_TIME);
}
else if (selection_data->target == oSelection->COMPOUND_TEXT_Atom)
{
gtk_selection_convert (widget, GDK_SELECTION_PRIMARY, GDK_TARGET_STRING, GDK_CURRENT_TIME);
}
else {
oSelection->IsBusy = 0;
oSelection->LastClipWord.clear();
}
return;
}
oSelection->IsBusy = 0;
oSelection->SelectionReceived(result);
g_free (result);
}
void Selection::SelectionReceived(gchar* sToken)
{
if(sToken[0] == '\0') {
LastClipWord.clear();
return;
}
gint len = 0;
gchar *a = sToken;
while ((*a) && len < 256) {
a = g_utf8_next_char (a);
++len;
}
*a = '\0';
while (g_ascii_isspace(*sToken))
sToken++;
if(sToken[0] == '\0') {
LastClipWord.clear();
return;
}
a=strchr(sToken,'\n');
if (a)
*a='\0';
if (g_str_has_prefix(sToken,"http://") || g_str_has_prefix(sToken,"ftp://")) {
LastClipWord.clear();
return;
}
if (LastClipWord != sToken)
{
LastClipWord = sToken;
if(bIsPureEnglish(sToken))
{
if ( gpAppFrame->SimpleLookupToFloat(sToken,false) )
return;
a = GetPureEnglishAlpha(sToken);
if (*a) {
if (LastClipWord == a) {
gpAppFrame->ShowNotFoundToFloatWin(a, _("<Not Found!>"), false);
gpAppFrame->oTopWin.InsertHisList(a);
}
else
gpAppFrame->SimpleLookupToFloat(a,true);
}
}
else
{
gpAppFrame->SimpleLookupToFloat(sToken,true);
}
}
}