#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dnd.h"
#include <ucx/buffer.h>
#ifdef UI_GTK2LEGACY
static gboolean selection_data_set_uris(GtkSelectionData *selection_data,
char **uris) {
UcxBuffer *buf = ucx_buffer_new(
NULL,
1024,
UCX_BUFFER_AUTOEXTEND);
char *uri;
int i =
0;
while((uri = uris[i]) !=
NULL) {
ucx_buffer_puts(buf, uri);
ucx_buffer_puts(buf,
"\r\n");
}
GdkAtom type = gdk_atom_intern(
"text/uri-list",
FALSE);
gtk_selection_data_set(selection_data, type,
8, (guchar*)buf->space, buf->pos);
ucx_buffer_free(buf);
return TRUE;
}
static char** selection_data_get_uris(GtkSelectionData *selection_data) {
return NULL;
}
#define gtk_selection_data_set_uris selection_data_set_uris
#define gtk_selection_data_get_uris selection_data_get_uris
#endif
void ui_selection_settext(UiSelection *sel,
char *str,
int len) {
gtk_selection_data_set_text(sel->data, str, len);
}
void ui_selection_seturis(UiSelection *sel,
char **uris,
int nelm) {
char **uriarray = calloc(nelm+
1,
sizeof(
char*));
for(
int i=
0;i<nelm;i++) {
uriarray[i] = uris[i];
}
uriarray[nelm] =
NULL;
gtk_selection_data_set_uris(sel->data, uriarray);
}
char* ui_selection_gettext(UiSelection *sel) {
guchar *text = gtk_selection_data_get_text(sel->data);
if(text) {
char *textcp = strdup((
char*)text);
g_free(text);
return textcp;
}
return NULL;
}
char** ui_selection_geturis(UiSelection *sel,
size_t *nelm) {
gchar **uris = gtk_selection_data_get_uris(sel->data);
if(uris) {
size_t al =
32;
char **array = malloc(al *
sizeof(
char*));
size_t i =
0;
while(uris[i] !=
NULL) {
if(i >= al) {
al *=
2;
array = realloc(array, al *
sizeof(
char*));
}
array[i] = strdup((
char*)uris[i]);
i++;
}
*nelm = i;
g_strfreev(uris);
return array;
}
return NULL;
}