+static gboolean
+on_focus_out (GtkWidget *self,
+ GdkEventFocus *event,
+ gpointer user_data)
+{
+
+ if (!gtk_widget_is_focus (self)) {
+ GtkTreeSelection *selection = NULL;
+ GList *selected_rows = NULL;
+ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
+ if (gtk_tree_selection_count_selected_rows (selection) > 1) {
+ selected_rows = gtk_tree_selection_get_selected_rows (selection, NULL);
+ g_signal_handlers_block_by_func (selection, on_selection_changed, self);
+ gtk_tree_selection_unselect_all (selection);
+ gtk_tree_selection_select_path (selection, (GtkTreePath *) selected_rows->data);
+ g_signal_handlers_unblock_by_func (selection, on_selection_changed, self);
+ g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
+ g_list_free (selected_rows);
+ }
+ }
+ return FALSE;
+}
+
+static gboolean
+on_button_release_event(GtkWidget * self, GdkEventButton * event, gpointer userdata)
+{
+ enable_drag_and_drop(self);
+ return FALSE;
+}
+
+static gboolean
+on_button_press_event(GtkWidget * self, GdkEventButton * event, gpointer userdata)
+{
+ GtkTreeSelection *selection = NULL;
+ GtkTreePath *path = NULL;
+ gboolean already_selected = FALSE;
+
+ if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(self), event->x, event->y, &path, NULL, NULL, NULL)) {
+ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
+ already_selected = gtk_tree_selection_path_is_selected (selection, path);
+ }
+
+ /* Enable drag and drop onlly if the user clicks on a row that
+ it's already selected. If not, let him select items using
+ the pointer */
+ if (!already_selected) {
+ disable_drag_and_drop(self);
+ }
+
+ if (path != NULL) {
+ gtk_tree_path_free(path);
+ }
+
+ return FALSE;
+}
+