Re: GTK3 porting problem (clipboard)



GtkTargetEntry targets[] = {
  {"text/html", 0, 0},
  {"text/plain", 0, 0}
};

size_t num_targets = sizeof(targets) / sizeof(targets[]);

// This IS required no matter what, or your program WILL BE broken
if (sizeof(size_t) > sizeof(guint) && num_targets > (size_t)G_MAX_GUINT) {
  // fail here because of stupid decision to use guint instead of size_t
// this might get optimised away by compiler (condition is know to be false/true during compile-time
}

gtk_clipboard_set_with_data(clipboard, targets, (guint)num_targets, get_func, clear_func, data);

NOTE: you will get warning about conversion from const char* to char*. This is that even though this was reported as BUG years ago, once it was fixed and that fix was removed because it broke existing apps. Still, the could've fixed this for GTK3, but they didn't. Right way to do this would be either allocate string using strdup and than free it, or use something like this:

char target1[] = "text/html";
char target2[] = "text/plain";

// char *target = "blabla"; // this is NOT right

GtkTargetEntry targets[] = {
  { target1, 0, 0},
  { target2, 0, 0},
};

On 03/11/2011 08:55 AM, Miroslav Rajcic wrote:
You're right, but this seems odd. In the GTK3 version of the
documentation the basic structures GtkTargetEntry and
GtkTargetPair are still exposed in the API, and although
GtkTargetList is said to be opaque (a) it's unclear why this
should be so (it seems to be a trivial composite) and (b) the
functionality that was previously available via the struct itself
is not replicated via accessor functions. For example, there's no

gtk_target_list_get_n_targets()

to replace the original poster's GTK2 idiom:

int nTargetCnt = g_list_length (list->list);

Perhaps someone knows any workaround for what I am trying to do in my
code or a way to implement this differently?

The code does the following:

//create a target list with text and HTML formats
GtkTargetList *list = gtk_target_list_new (NULL, 0);
gtk_target_list_add(list, atomTextHtml, 0, 0);
gtk_target_list_add_text_targets(list, 0);

//now I need to convert the target list to an array of the
GtkTargetEntry structures as needed by gtk_clipboard_set_with_data
int nTargetCnt = g_list_length (list->list);
GtkTargetEntry *targets = g_new0 (GtkTargetEntry, nTargetCnt);
int i=0;
for (GList *l=list->list; l; l=l->next, i++)
{
GtkTargetPair *pair = (GtkTargetPair *)l->data;
targets[i].target = gdk_atom_name (pair->target);
}

//set the clipboard with target formats
gboolean bOK = gtk_clipboard_set_with_data(clipboard, targets,
nTargetCnt, MyGtkClipboardGetFunc, NULL, 0);

Basically the big problem is that gtk_clipboard_set_with_data API does
not use GtktargetList directly.

Regards,
Miroslav
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]