Exemplo n.º 1
0
static void drop_box_drag_data_received(GtkWidget *drop_box,
			      GdkDragContext    *context,
			      gint              x,
			      gint              y,
			      GtkSelectionData  *selection_data,
			      guint             drag_info,
			      guint32           time)
{
	GList *uris = NULL;
	guchar *path = NULL;
	gboolean success = FALSE;

	if (!selection_data->data)
		goto err; 		/* Timeout? */

	uris = uri_list_to_glist(selection_data->data);

	if (g_list_length(uris) != 1)
	{
		delayed_error(_("Sorry, you need to drop exactly one file "
				"onto the drop area."));
		goto err;
	}
		
	path = get_local_path((EscapedPath *) uris->data);

	if (!path)
	{
		delayed_error(
			_("Sorry, I can't use '%s' because it's not a local "
			  "file."), (guchar *) uris->data);
		goto err;
	}

	if (!file_exists(path))
	{
		delayed_error(_("Can't access '%s':\n%s"), path,
				g_strerror(errno));
		goto err;
	}

	g_signal_emit_by_name(drop_box, "path_dropped", path);

	success = TRUE;
err:
	if (path)
		g_free(path);
	
	if (uris)
		g_list_free(uris);
	gtk_drag_finish(context, success, FALSE, time);	/* Failure */
}
Exemplo n.º 2
0
Arquivo: dnd.c Projeto: ayyi/samplecat
gint
drag_received (GtkWidget* widget, GdkDragContext* drag_context, gint x, gint y, GtkSelectionData* data, guint info, guint time, gpointer user_data)
{
	// this receives drops for the whole window.

	if(!data || data->length < 0){ perr("no data!\n"); return -1; }

	dbg(1, "%s", data->data);

	if(g_str_has_prefix((char*)data->data, "colour:")){

		char* colour_string = (char*)data->data + 7;
		unsigned colour_index = atoi(colour_string) ? atoi(colour_string) - 1 : 0;

		// which row are we on?
		GtkTreePath* path;
		GtkTreeIter iter;
		gint tx, treeview_top;
		gdk_window_get_position(app->libraryview->widget->window, &tx, &treeview_top);
		dbg(2, "treeview_top=%i", y);

#ifdef HAVE_GTK_2_12
		gint bx, by;
		gtk_tree_view_convert_widget_to_bin_window_coords(GTK_TREE_VIEW(app->libraryview->widget), x, y - treeview_top, &bx, &by);
		dbg(2, "coords: %dx%d => %dx%d", x, y, bx, by);

#else
		gint by = y - treeview_top - 20;
#endif

#ifdef USE_GDL
		GdkWindow* top_window = gdk_window_get_toplevel(gtk_widget_get_toplevel(app->libraryview->widget)->window);
		GdkWindow* window = app->libraryview->widget->window;
		while((window = gdk_window_get_parent(window)) != top_window){
			gint x0, y0;
			gdk_window_get_position(window, &x0, &y0);
			by -= y0;
		}
#endif

    if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(app->libraryview->widget), x, by, &path, NULL, NULL, NULL)){

      gtk_tree_model_get_iter(GTK_TREE_MODEL(samplecat.store), &iter, path);
      gchar* path_str = gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(samplecat.store), &iter);
      dbg(2, "path=%s y=%i final_y=%i", path_str, y, y - treeview_top);

      listview_item_set_colour(path, colour_index);

      gtk_tree_path_free(path);
    }
    else dbg(0, "path not found.");

    return FALSE;
  }

  if(info == GPOINTER_TO_INT(GDK_SELECTION_TYPE_STRING)) printf(" type=string.\n");

  if(info == GPOINTER_TO_INT(TARGET_URI_LIST)){
    dbg(1, "type=uri_list. len=%i", data->length);
    GList* list = uri_list_to_glist((char*)data->data);
    if(g_list_length(list) < 1) pwarn("drag drop: uri list parsing found no uri's.\n");
    int i = 0;
    ScanResults result = {0,};
    GList* l = list;
#ifdef __APPLE__
    gdk_threads_enter();
#endif
    for(;l;l=l->next){
      char* u = l->data;

      gchar* method_string;
      vfs_get_method_string(u, &method_string);
      dbg(2, "%i: %s method=%s", i, u, method_string);

      if(!strcmp(method_string, "file")){
        //we could probably better use g_filename_from_uri() here
        //http://10.0.0.151/man/glib-2.0/glib-Character-Set-Conversion.html#g-filename-from-uri
        //-or perhaps get_local_path() from rox/src/support.c

        char* uri_unescaped = vfs_unescape_string(u + strlen(method_string) + 1, NULL);

        char* uri = (strstr(uri_unescaped, "///") == uri_unescaped) ? uri_unescaped + 2 : uri_unescaped;

        if(do_progress(0,0)) break;
        if(is_dir(uri)) application_add_dir(uri, &result);
        else application_add_file(uri, &result);

        g_free(uri_unescaped);
      }
      else pwarn("drag drop: unknown format: '%s'. Ignoring.\n", u);
      i++;
    }
    hide_progress();
#ifdef __APPLE__
    gdk_threads_leave();
#endif

    statusbar_print(1, "import complete. %i files added", result.n_added);

    uri_list_free(list);
  }

  return FALSE;
}