art with code

2008-12-14

Filezoo day 34, part 1: extended context menu, cut-copy-paste


Got cut-copy-paste business logic done, still missing the keyboard shortcuts though. The context menu is getting huge!

Patching up yesterday's missed todo items today, next up are clipboard keyboard shortcuts and dragging items.

The clipboard stuff was a bit of a pain, so here's a reference. I don't know if I'm doing it in The Right Way, but it does work. Can paste files to/from Konqueror at least.

void BuildCopyPasteMenu (Menu menu, ClickHit c)
{
string targetPath = c.Target.FullName;
string targetDir = c.Target.IsDirectory ? c.Target.FullName : Helpers.Dirname(c.Target.FullName);

Separator (menu);
string items = "file://" + c.Target.FullName;
if (App.Selection.Count > 0) {
items = App.GetSelectionData ();
}

AddItem(menu, "Cut", delegate {
SetClipBoard(items);
cut = true;
});

AddItem(menu, "Copy", delegate {
SetClipBoard(items);
cut = false;
});

AddItem(menu, "Paste to "+Helpers.Basename(targetDir)+"/", delegate {
bool handled = false;
clipboard.RequestContents(Gdk.Atom.Intern("text/uri-list", true), delegate(Clipboard cb, SelectionData data) {
if (data.Length > -1) {
handled = true;
App.HandleSelectionData(data, cut ? Gdk.DragAction.Move : Gdk.DragAction.Copy, targetPath);
cut = false;
}
});
clipboard.RequestContents(Gdk.Atom.Intern("application/x-color", true), delegate(Clipboard cb, SelectionData data) {
if (data.Length > -1 && !handled) {
handled = true;
App.HandleSelectionData(data, cut ? Gdk.DragAction.Move : Gdk.DragAction.Copy, targetPath);
}
});
clipboard.RequestContents(Gdk.Atom.Intern("text/plain", true), delegate(Clipboard cb, SelectionData data) {
if (data.Length > -1 && !handled) {
handled = true;
App.HandleSelectionData(data, cut ? Gdk.DragAction.Move : Gdk.DragAction.Copy, targetPath);
}
});
});
Separator (menu);
}

void SetClipBoard (string items)
{
clipboard.SetWithData(targets,
delegate (Clipboard cb, SelectionData data, uint info) {
data.Set(data.Target, 8, System.Text.Encoding.UTF8.GetBytes(items));
data.Text = items;
},
delegate (Clipboard cb) {
cut = false;
}
);
}
Post a Comment

Blog Archive

About Me

My photo

Built art installations, web sites, graphics libraries, web browsers, mobile apps, desktop apps, media player themes, many nutty prototypes, much bad code, much bad art.

Have freelanced for Verizon, Google, Mozilla, Warner Bros, Sony Pictures, Yahoo!, Microsoft, Valve Software, TDK Electronics.

Ex-Chrome Developer Relations.