GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 31, 2014 8:43 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Copying files to clipboard in Windows
PostPosted: Wed Jul 03, 2013 10:10 am 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
Hello, everybody!

My program has to copy files to clipboard so then user can paste it in other applications like TotalCommander or file explorer. I do it so:

Code:
static GdkAtom gnomeCopyAtom = 0;
static GdkAtom fileCopyAtom = 0;
static GdkAtom effectCopyAtom = 0;

void GetNoOp(GtkClipboard* clipboard, GtkSelectionData* selection_data,
             guint info,
             gpointer user_data)
{
  GdkAtom target;
  gchar text[1024];
 
  target = gtk_selection_data_get_target(selection_data);

  if (target == effectCopyAtom)
  {
    unsigned int t = 5;
    gtk_selection_data_set(selection_data, target, 32, &t, 4);
  }
  else if (target == gnomeCopyAtom)
  {
    sprintf(text, "copy\nfile://%s", (gchar*)user_data);
    gtk_selection_data_set(selection_data, target, 8, text, strlen(text) + 1);
  }
  else if (target == fileCopyAtom)
  {
    sprintf(text, "%s", (gchar*)user_data);
    gtk_selection_data_set(selection_data, target, 8, text, strlen(text) + 1);
  }
}

void ClearNoOp(GtkClipboard* clipboard, gpointer user_data)
{
  if (user_data) g_free(user_data);
}

static void log_popup_copy_file(GtkWidget *menuitem, gpointer userdata)
{
  GtkTargetList *target_list;
  GtkTargetEntry *targets;
  int n_targets;
  gchar *stat_data = malloc(strlen(userdata) + 1);
 
  if (stat_data)
  {
    strcpy(stat_data, userdata);
    if (gnomeCopyAtom == 0)
    {
      gnomeCopyAtom = gdk_atom_intern("x-special/gnome-copied-files", FALSE);
    }
    if (fileCopyAtom == 0)
    {
      fileCopyAtom = gdk_atom_intern("FileName", FALSE);
    }
    if (effectCopyAtom == 0)
    {
      effectCopyAtom = gdk_atom_intern("Preferred Dropeffect", FALSE);
    }
    target_list = gtk_target_list_new(NULL, 0);
    gtk_target_list_add(target_list, gnomeCopyAtom, 0, 0);
    gtk_target_list_add(target_list, fileCopyAtom, 0, 0);
    gtk_target_list_add(target_list, effectCopyAtom, 0, 0);
    gtk_target_list_add_uri_targets(target_list, 0);
    gtk_target_list_add_text_targets(target_list, 0);
   
    targets = gtk_target_table_new_from_list (target_list, &n_targets);
    gtk_target_list_unref (target_list);
   
    gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
                                targets, n_targets, GetNoOp, ClearNoOp,
                                stat_data);
   
    gtk_target_table_free(targets, n_targets);
  }
}


In Linux it works fine, but in Windows it causes error with MessageBox with text "Cannot find %1" on side of pasting application. What do I wrong?

upd: or development for Windows with GTK+ is very exotic and I never get answer? :(


Top
 Profile  
 
 Post subject: Re: Copying files to clipboard in Windows
PostPosted: Wed Nov 06, 2013 4:16 pm 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
So, when I use atom "FileNameW" instead of "FileName" and use UTF-16 string instead UTF-8 I can paste file on Windows to other applications... but now other applications retrieve my file as "fragment.shs", not with original name!
Another thing: I use IDataObject Viewer and see - when I copy file to clipboard from any file manager, member "tymed" of the structure FORMATETC is HGLOBAL. When I my code copies file (as you can see above), member "tymed" of the structure FORMATETC is HGLOBAL | ISTREAM. Why? Why and how GTK does it? I even have seen all GTK source code tree, but I haven't found code with ISTREAM, only with HGLOBAL. So... what's wrong with my code? Please help me :(


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group