[Gtk-sharp-list] TextView and DnD

Jakob Henriksson b0kaj@lysator.liu.se
Sun, 15 Feb 2004 21:45:34 +0100


When I send some random text to a TextView via DnD it gets received  
twice. How can I prevent this from happening?

I have attatched some runnable code to demonstrate the situation.

Compile using the following command assuming the code is saved in a  
file named TextViewDnD.cs:

mcs -r:gtk-sharp.dll,gdk-sharp.dll,glib-sharp.dll TextViewDnD.cs

I am running gtk-sharp from CVS and Mono version 0.30.99.20040204.

I would appreciate any comments on this. Perhaps it is not in any way  
related to gtk-sharp but simple a matter of me not knowing how to do  
it, anyway...

Jakob

--- code start
using Gtk;
using Gdk;
using GtkSharp;
using System;

public class TextViewDnD {

    TextBuffer textbuffer;

    enum TargetType {
	String,
    };

    private static TargetEntry[] targetTable = new TargetEntry [] {
	new TargetEntry ("STRING", 0, (uint) TargetType.String ),
	new TargetEntry ("text/plain", 0, (uint) TargetType.String)
    };

    public TextViewDnD() {

	Application.Init();
	Gtk.Window win = new Gtk.Window("test");

	Button button = new Button("drag me to thee TextView");
	// setup Label as DnD sender
	Gtk.Drag.SourceSet(button, Gdk.ModifierType.Button1Mask,
			   targetTable, DragAction.Copy);
	button.DragDataGet +=
	    new DragDataGetHandler(SourceDataGetHandler);
	
	TextTagTable textTagTable = new TextTagTable();
	textbuffer = new TextBuffer(textTagTable);
	TextView textview = new TextView();
	
	// set up the TextView as a DnD receiver
	Gtk.Drag.DestSet(textview, DestDefaults.All, targetTable,
			 DragAction.Copy);
	textview.DragDataReceived +=
	    new DragDataReceivedHandler(DestDataReceiveHandler);

	VBox vbox = new VBox(false, 0);
	vbox.PackStart(button, true, true, 0);
	vbox.PackStart(textview, true, true, 0);
	
	win.Add(vbox);
	win.ShowAll();
	Application.Run();
    }

    private void SourceDataGetHandler(object sender,
				      DragDataGetArgs args)
    {
	args.SelectionData.Text = "DATA";
    }

    private void DestDataReceiveHandler(object receiver,
					DragDataReceivedArgs args)
    {
	if ((args.SelectionData.Length >= 0) &&
	    (args.SelectionData.Format == 8))
	    {
		Console.WriteLine ("Received {0} in canvas",
				   args.SelectionData.Text);
		
		textbuffer.Text = args.SelectionData.Text;
		
		Gtk.Drag.Finish (args.Context, true, false, args.Time);
	    }
	
	Gtk.Drag.Finish (args.Context, false, false, args.Time);
    }

    public static int Main(string[] args) {

	TextViewDnD tvdnd = new TextViewDnD();
	return 0;
    }

}
--- code end