CentOS 6.4 and VirtualBox Additions

When you install CentOS 6.4 in VirtualBox, quite quickly you might be annoyed by a lack of a mouse integration. Usual cure in form of VM guest additions simply fails with

Building the main Guest Additions module   [FAILED]

Fortunately this message comes with some additional information which points to lack of compiler and kernel headers. Easiest way to install them is in terminal:

su - root
yum install gcc
yum install kernel-devel-`uname -r`

After this you can retry guest additions installation and you should see better results.

PS: This method probably works for RedHat also.

ShortcutKeyDisplayString in Tray Context-menu

Illustration

It all started when I wanted to show custom shortcut text next to a menu item on a tray icon’s context menu. Usually this is as easy as setting ShortcutKeyDisplayString property. So I did, and it worked. Sort-of.

For some reason ContextMenuStrip on TrayIcon is not shown on first right-click but only on second. While it is not a major issue, I found it really annoying. That meant that I had to stick to good old ContextMenu and its MenuItem. Unfortunately that also meant that there was no ShortcutKeyDisplayString to help me.

And then I remembered trick from my VB 6 days: anything could be displayed in shortcut position if you would separate it by tab character. So I tried this:

someMenuItem.Text = "Show application" + "\t" + "Ctrl+Alt+P";

Surprisingly, this trick still works.

Editing Labels in a Sorted TreeView

In my previous post we’ve been dealing with TreeView drag&drop. One other functionality that is almost mandatory for TreeView is renaming a node. While basic code is quite straight forward, there are few tricks in order to get better-than-default behavior.

First order of business is BeforeLabelEdit event. There we define which nodes will have fixed name. In our case, we will not allow editing of folder names:

e.CancelEdit = (e.Node.ImageIndex == 0); //don't allow editing folders

In AfterLabel event we handle everything else. We want new text without spaces on either end and no duplicates are allowed. It complicates code a bit but not by much. Probably only non obvious thing is actual sorting. Here we just “schedule” it after event handler is done with processing:

if (e.Label == null) { return; } //no change was made
e.CancelEdit = true; //we will handle changes manually
string newText = e.Label.Trim(); //no spaces

var nodes = (e.Node.Parent == null) ? tree.Nodes : e.Node.Parent.Nodes;
foreach (TreeNode node in nodes) {
    if ((node != e.Node) && string.Equals(newText, node.Text, StringComparison.Ordinal)) {
        return; //duplicate name
    }
}

e.Node.Text = newText; //rename manually

tree.BeginInvoke(new Action<TreeNode>(delegate(TreeNode node) { //sort again
    tree.Sort();
    tree.SelectedNode = node;
}), e.Node);

Full sample can be downloaded here.

PS: In sample code you will see that I use ImageIndex==0 to determine whether node is of folder type. In real program you would probably go with sub-classing TreeNode.

Drag&drop in a Sorted TreeView

Illustration

If you have a TreeView, chances are that you want it sorted and with a drag&drop functionality. And that is not too hard.

In order to sort items, don’t forget to assign TreeViewNodeSorter property. This requires simple IComparer, e.g.:

internal class NodeSorter : IComparer {
    public int Compare(object item1, object item2) {
        var node1 = item1 as TreeNode;
        var node2 = item2 as TreeNode;

        if (node1.ImageIndex == node2.ImageIndex) { //both are of same type
            return string.Compare(node1.Text, node2.Text, StringComparison.CurrentCultureIgnoreCase);
        } else {
            return (node1.ImageIndex == 0) ? -1 : +1;
        }
    }
}

This will ensure that “folders” (with ImageIndex==0) are sorted before files (any other value of ImageIndex). All that is left is to call Sort method when needed.

In order to support drag&drop, a bit more work is needed. Before we even start doing anything, we need to set AllowDrop=true on our TreeView. Only then we can setup events. To initiate drag we just work with ItemDrag event:

this.DoDragDrop(e.Item, DragDropEffects.Move);

In DragOver we need to check for “droppability” of each item. Rules are simple; We allow only tree nodes in; if we drop file on file, it will actually drop it in file’s folder; and don’t allow parent to be dropped into its child. This class will then either allow movement (DragDropEffects.Move) or it will deny it (DragDropEffects.None).

var fromNode = e.Data.GetData("System.Windows.Forms.TreeNode") as TreeNode;
if (fromNode == null) { return; } //not our stuff

var dropNode = tree.GetNodeAt(tree.PointToClient(new Point(e.X, e.Y)));
while ((dropNode != null) && (dropNode.ImageIndex != 0)) { //search for suitable folder
    dropNode = dropNode.Parent;
}

var noCommonParent = (fromNode.Parent != dropNode);
while (noCommonParent && (dropNode != null)) {
    if (fromNode == dropNode) { noCommonParent = false; } //to stop parent becoming a child
    dropNode = dropNode.Parent;
}

e.Effect = noCommonParent ? DragDropEffects.Move : DragDropEffects.None;

Final movement happens in DragDrop event. First part is same node discovery process we had in DragOver. After that we simply move nodes from one parent to another and we wrap all up by performing a sort.

var fromNode = e.Data.GetData("System.Windows.Forms.TreeNode") as TreeNode;
var dropNode = tree.GetNodeAt(tree.PointToClient(new Point(e.X, e.Y)));
while ((dropNode != null) && (dropNode.ImageIndex != 0)) { //search for suitable folder
    dropNode = dropNode.Parent;
}

var fromParentNodes = (fromNode.Parent != null) ? fromNode.Parent.Nodes : tree.Nodes;
fromParentNodes.Remove(fromNode);
if (dropNode == null) {
    tree.Nodes.Add(fromNode);
} else {
    dropNode.Nodes.Add(fromNode);
}

tree.Sort();
tree.SelectedNode = fromNode;

Full sample can be downloaded here.

PS: In sample code you will see that I use ImageIndex==0 to determine whether node is of folder type. In real program you would probably go with sub-classing TreeNode.

Chromecast

Illustration

Chromecast arrives in a very stylish box with a device on very top and all other stuff under. It comes with a short HDMI extension cable, USB-A to micro-USB cable, and a nice 850 mA power adapter.

First thing to note is that this device CANNOT work once you plug it into a HDMI port. You must use micro-USB on the other end to bring it power. While you do get everything you need in the box, it makes for not-so-clean look if it is visible (e.g. you have only free HDMI on side of your TV). It is not too bad if you have USB on your TV, but it gets annoying if you need to add, yet another, power brick to the back.

Yes, need for additional power is not a big surprise since HDMI allows for only 50 mA. What I am surprised about is Google forgetting to mention that. All Google’s pictures with Chromecast plugged in (including Chromecast introduction event) show device plugged in HDMI-only. Somehow every picture has one essential (and messy) cable missing. Sneaky…

Without any video running current consumption is around hefty 300 mA and definitely more than I would expect. During playback it needs about 50 mA more with occasional (short) jumps to 500 mA range. Device will get warm during use but there were no problems even after couple hours of playback so there is no major issue here.

As you plug device in, it will require short setup which is as simple as it gets. Especially so because you do everything on your computer. Most other devices (e.g. Roku) force you to enter wireless password on TV which is always annoying when there is no keyboard ready. Chromecast has successfully avoided this.

Use is equally simple. Just select the cast button in a supporting application and Chromecast will start playing content. Of course, there will be a small delay because Chromecast will have to independently visit video stream location by using its own wireless connection. Benefit of such architecture is that you save on bandwidth between devices and you can even turn off your mobile without interrupting a playback. Your glorious mobile phone becomes just a remote control.

There is an extension that enables you to cast a Chrome browser tab but I found Chromecast to be very lacking in this area. Casting a tab is unbearably slow. Any scroll operation takes ages and videos are barely a slideshow. And you can forget about sound. Only thing that works from PC is actually a YouTube.

All this brings us to most serious downfall - application support for Chromecast is lacking at best. You can really only count on Netflix, Google Play, and YouTube. If you need something else, though luck. There might be some improvement in the future but I wouldn’t hold my breath there.

Could you live without Chromecast? Definitely yes, especially if you already have your laptop connected. But at $35 this device is a steal. Assuming that you have free HDMI port and you are living in States, it is a good buy.

If you are outside of States, it is not a useful device at all. Since Google Play and Netflix are restricted to US market, all you can really do is play Youtube videos and play with Chrome tabs. Considering all the hops you need to get around in order to purchase it in the first place, it is hard to justify even thinking about it.