GtkClipboard problems
- From: "Miroslav Rajcic" <rajcic sokrates hr>
- To: <gtk-app-devel-list gnome org>
- Subject: GtkClipboard problems
- Date: Fri, 29 Feb 2008 14:56:13 +0100
When user initiates clipboard copy action, I want to push the data to
clipboard in more than one format (plain text and HTML). My problem is that
the code below does not seem to work for me (tested on Windows).
Does anyone know what could be wrong here ?
Clipboard function for getting data is never called, even if
gtk_clipboard_set_with_data() returned true.
Using ClipSpy shows no data on the clipboard. I am using GTK 2.12.6 from
http://gtk.org/
Any ideas ?
Regards,
Miroslav
------------- code ------------
#include <gtk/gtk.h>
void create_window();
void on_copy_clicked (GtkButton *button, gpointer user_data);
void MyGtkClipboardGetFunc(GtkClipboard *clipboard, GtkSelectionData
*selection_data, guint info, gpointer user_data_or_owner);
void MyGtkClipboardClearFunc(GtkClipboard *clipboard, gpointer
user_data_or_owner);
GdkAtom atomTextHtml;
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
create_window();
atomTextHtml = gdk_atom_intern("text/html", FALSE);
gtk_main ();
return 0;
}
#ifdef _WIN32
#include <windows.h>
int APIENTRY WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return main( __argc, __argv );
}
#endif
void create_window()
{
GtkWidget *window1;
GtkWidget *button1;
window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window1), "test");
gtk_widget_show (window1);
g_signal_connect (window1, "destroy", G_CALLBACK (gtk_main_quit), NULL);
button1 = gtk_button_new_with_mnemonic("Copy");
gtk_widget_show (button1);
gtk_container_add (GTK_CONTAINER (window1), button1);
g_signal_connect(button1, "clicked", G_CALLBACK (on_copy_clicked),
NULL);
}
void on_copy_clicked (GtkButton *button, gpointer user_data)
{
GtkClipboard *clipboard = gtk_clipboard_get_for_display
(gdk_display_get_default (), GDK_SELECTION_PRIMARY);
GtkTargetList *list = gtk_target_list_new (NULL, 0);
gtk_target_list_add(list, atomTextHtml, 0, 0);
gtk_target_list_add_text_targets (list, 0);
int nTargets = g_list_length (list->list);
GtkTargetEntry *targets = g_new0 (GtkTargetEntry, nTargets);
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);
}
gboolean bOK = gtk_clipboard_set_with_data(clipboard, targets,
G_N_ELEMENTS (targets), MyGtkClipboardGetFunc, MyGtkClipboardClearFunc, 0);
gtk_clipboard_set_can_store (clipboard, targets, 1); //mark 1st as
suitable for storing
gtk_clipboard_store(clipboard);
for (i = 0; i < nTargets; i++)
g_free (targets[i].target);
g_free (targets);
gtk_target_list_unref (list);
}
void MyGtkClipboardGetFunc(GtkClipboard *clipboard, GtkSelectionData
*selection_data, guint info, gpointer user_data_or_owner)
{
printf("Get clipboard data\n");
gtk_selection_data_set_text (selection_data, "aaa", -1);
gtk_selection_data_set(selection_data, atomTextHtml, 0, (guchar
*)"<b>aaa</b>", strlen("<b>aaa</b>"));
}
void MyGtkClipboardClearFunc(GtkClipboard *clipboard, gpointer
user_data_or_owner)
{
printf("Clear clipboard data\n");
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]