rework a remain timecode display
[omnplay] / src / omnplay.cpp
index 8c2bafc..dbdc7d3 100644 (file)
@@ -46,6 +46,8 @@ int omnplay_get_content(omnplay_instance_t* app, playlist_item_t *items, int lim
     OmPlrClipInfo clip_info;
     char clip_name[omPlrMaxClipDirLen];
 
+    app->library.id_display_idx = 0;
+
     pthread_mutex_lock(&app->players.lock);
 
     r = OmPlrClipGetFirst((OmPlrHandle)app->players.item[0].handle, clip_name, sizeof(clip_name));
@@ -186,14 +188,11 @@ static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev ,
     gdk_threads_enter();
     pthread_mutex_lock(&player->app->playlist.lock);
     pthread_mutex_lock(&player->app->players.lock);
-    if(curr->state == omPlrStatePlay || curr->state == omPlrStateCuePlay)
+
+    /* check if playlist exist */
+    if(player->playlist_length)
     {
-        idx = find_index_of_playlist_item(player->app, player->playlist_start, curr->currClipNum);
-        if(idx >= 0)
-        {
-            frames2tc(curr->currClipStartPos + curr->currClipLen - curr->pos, 25.0, tc_rem);
-            omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
-        }
+        /* clear remain on "previous" item */
         if(curr->currClipNum != prev->currClipNum && 1 != prev->numClips)
         {
             tc_rem[0] = 0;
@@ -201,16 +200,21 @@ static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev ,
             if(idx >= 0)
                 omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
         };
-    }
-    else
-    {
-        tc_rem[0] = 0;
+
+        /* update current item */
         idx = find_index_of_playlist_item(player->app, player->playlist_start, curr->currClipNum);
         if(idx >= 0)
+        {
+            /* reset value */
+            tc_rem[0] = 0;
+
+            /* for play and cue calc new value */
+            if(curr->state == omPlrStatePlay || curr->state == omPlrStateCuePlay)
+                frames2tc(curr->currClipStartPos + curr->currClipLen - curr->pos, 25.0, tc_rem);
+
+            /* setup that value */
             omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
-        idx = find_index_of_playlist_item(player->app, player->playlist_start, prev->currClipNum);
-        if(idx >= 0)
-            omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
+        };
     };
     pthread_mutex_unlock(&player->app->players.lock);
     pthread_mutex_unlock(&player->app->playlist.lock);
@@ -307,7 +311,7 @@ void get_selected_items_playlist_proc(GtkTreeModel *model, GtkTreePath *path, Gt
     list[0] = list[0] + 1;
 };
 
-static int* get_selected_items_playlist(omnplay_instance_t* app)
+int* omnplay_selected_idxs_playlist(omnplay_instance_t* app)
 {
     int* list = NULL;
     GtkTreeSelection *selection;
@@ -385,7 +389,7 @@ static int idxs_in_players_range(omnplay_instance_t* app, int start, int stop)
 static void omnplay_playlist_block(omnplay_instance_t* app, control_buttons_t button)
 {
     int start, stop, r, i;
-    int* list = get_selected_items_playlist(app);
+    int* list = omnplay_selected_idxs_playlist(app);
 
     if(!list)
         return;
@@ -440,7 +444,7 @@ static void omnplay_playlist_block(omnplay_instance_t* app, control_buttons_t bu
 static int get_first_selected_item_playlist(omnplay_instance_t* app)
 {
     int idx;
-    int* list = get_selected_items_playlist(app);
+    int* list = omnplay_selected_idxs_playlist(app);
     if(!list) return -1;
     idx = list[1];
     free(list);
@@ -481,10 +485,9 @@ static omnplay_player_t *get_player_at_pos(omnplay_instance_t* app, int pos)
     return NULL;
 };
 
-static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, int count)
+static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, int count, int sel)
 {
     int i, j, idx;
-    GtkTreePath* path;
 
     pthread_mutex_lock(&app->playlist.lock);
     pthread_mutex_lock(&app->players.lock);
@@ -528,11 +531,14 @@ static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, in
     omnplay_playlist_draw(app);
 
     /* select */
-    path = gtk_tree_path_new_from_indices(idxs[0], -1);
-    gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
-    gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
-    gtk_tree_path_free(path);
-
+    if(sel)
+    {
+        GtkTreePath* path;
+        path = gtk_tree_path_new_from_indices(idxs[0], -1);
+        gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
+        gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
+        gtk_tree_path_free(path);
+    };
 
     pthread_mutex_unlock(&app->players.lock);
     pthread_mutex_unlock(&app->playlist.lock);
@@ -543,7 +549,7 @@ static void omnplay_playlist_item_del(omnplay_instance_t* app)
     int i, idx, c;
     int *list, *list2;
 
-    list = get_selected_items_playlist(app);
+    list = omnplay_selected_idxs_playlist(app);
     if(!list) return;
 
     list2 = (int*)malloc(sizeof(int) * list[0]);
@@ -559,7 +565,7 @@ static void omnplay_playlist_item_del(omnplay_instance_t* app)
     };
 
     if(c)
-        omnplay_playlist_delete_items(app, list2, c);
+        omnplay_playlist_delete_items(app, list2, c, 1);
 
     free(list2);
     free(list);
@@ -627,7 +633,7 @@ static void omnplay_playlist_insert_items(omnplay_instance_t* app, int idx,
     omnplay_playlist_draw(app);
 
     /* select */
-    path = gtk_tree_path_new_from_indices(idx + count, -1);
+    path = gtk_tree_path_new_from_indices(idx, -1);
     gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
     gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
     gtk_tree_path_free(path);
@@ -1014,7 +1020,7 @@ static void omnplay_playlist_item_copy(omnplay_instance_t* app)
 {
     int *list, i;
 
-    list = get_selected_items_playlist(app);
+    list = omnplay_selected_idxs_playlist(app);
     if(!list) return;
 
     for(i = 0; i < list[0]; i++)
@@ -1204,17 +1210,199 @@ static gboolean on_library_grid_button(GtkWidget *widget, GdkEventButton *event,
 
 static gboolean on_playlist_grid_button(GtkWidget *widget, GdkEventButton *event, gpointer data)
 {
+    omnplay_instance_t* app = (omnplay_instance_t*)data;
+
 //    g_warning("on_playlist_grid_button");
 
     if(event->button==1 && event->type==GDK_2BUTTON_PRESS)
     {
-        omnplay_ctl((omnplay_instance_t* )data, BUTTON_PLAYER_CUE);
+        omnplay_playlist_item_edit(app);
         return TRUE;
     };
 
     return FALSE;
 };
 
+
+
+static void library_grid_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
+    GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
+{
+    int c;
+    playlist_item_t* items;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    g_warning("library_grid_drag_data_get_cb");
+
+    items = omnplay_library_get_selected(app, &c);
+
+    /* clear item */
+    if(items)
+    {
+        gtk_selection_data_set(selection_data, selection_data->target, 8,
+            (const guchar *)items, sizeof(playlist_item_t) * c);
+        free(items);
+    };
+};
+
+static void playlist_grid_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
+    GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
+{
+    int *list, i;
+    playlist_item_t* items;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    list = omnplay_selected_idxs_playlist(app);
+    if(!list) return;
+
+    /* clear delete flag */
+    for(i = 0; i < app->playlist.count; i++)
+        app->playlist.item[i].del = 0;
+
+    items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * list[0]);
+    for(i = 0; i < list[0]; i++)
+    {
+        items[i] = app->playlist.item[list[i + 1]];
+        if(context->action == GDK_ACTION_MOVE)
+            app->playlist.item[list[i + 1]].del = 1;
+    }
+    gtk_selection_data_set(selection_data, selection_data->target, 8,
+        (const guchar *)items, sizeof(playlist_item_t) * list[0]);
+
+    free(items);
+    free(list);
+};
+
+static void library_grid_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
+{
+    g_warning("library_grid_drag_begin_cb");
+    gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
+};
+
+static void playlist_grid_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
+{
+    g_warning("playlist_grid_drag_begin_cb");
+    gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
+};
+
+static void playlist_grid_drag_data_received(GtkWidget *widget, GdkDragContext *context,
+    gint x, gint y,  GtkSelectionData *selection_data,  guint info, guint time, gpointer userdata)
+{
+    int c, i, idx;
+    playlist_item_type_t t;
+    playlist_item_t* items;
+    GtkTreePath *path = NULL;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    g_warning("playlist_grid_drag_data_received: context->action=%d", context->action);
+
+    items = (playlist_item_t*)gtk_selection_data_get_data(selection_data);
+    c = gtk_selection_data_get_length(selection_data);
+
+    if(c % sizeof(playlist_item_t))
+    {
+        g_warning("playlist_grid_drag_data_received: ODD ITEMS");
+    }
+    else
+    {
+        c /= sizeof(playlist_item_t);
+
+        if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y, &path, NULL, NULL, NULL))
+        {
+            idx = gtk_tree_path_get_indices(path)[0];
+            gtk_tree_path_free(path);
+
+            g_warning("playlist_grid_drag_data_received: gtk_tree_path_get_indice[0]=%d", idx);
+
+            /* normalize, FIX ME */
+            idx--; if(idx < 0) idx = 0;
+        }
+        else
+            idx = app->playlist.count;
+
+        g_warning("playlist_grid_drag_data_received: idx=%d", idx);
+
+        if(omnplay_playlist_insert_check(app, idx, &t))
+        {
+            for(i = 0; i < c; i++)
+            {
+                items[i].type = t;
+                items[i].error = 0;
+            };
+            omnplay_playlist_insert_items(app, idx, items, c);
+        };
+    };
+
+    /* Finish the drag */
+    gtk_drag_finish(context, TRUE, FALSE, time);
+};
+
+static void playlist_grid_drag_data_delete(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
+{
+    int c, i, *list;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    g_warning("playlist_grid_drag_data_delete");
+
+    list = (int*)malloc(sizeof(int) * MAX_PLAYLIST_ITEMS);
+
+    for(i = 0, c = 0; i < app->playlist.count; i++)
+        if(app->playlist.item[i].del)
+            if(!idx_in_players_range(app, i))
+            {
+                /* save index */
+                list[c++] = i;
+                g_warning("playlist_grid_drag_data_delete: i=%d, c=%d", i, c);
+            };
+
+    if(c)
+        omnplay_playlist_delete_items(app, list, c, 0);
+
+    free(list);
+};
+
+/*
+ * http://www.mail-archive.com/mahogany-users@lists.sourceforge.net/msg00286.html
+ */
+static gboolean playlist_grid_drag_motion(GtkWidget *widget, GdkDragContext *context,
+    gint x, gint y, guint time, gpointer data)
+{
+    gboolean same;
+    GtkWidget *source_widget;
+
+    g_warning("playlist_grid_drag_motion");
+
+    /* Get source widget and check if it is the same as the
+     * destination widget.
+     */
+    source_widget = gtk_drag_get_source_widget(context);
+    same = ((source_widget == widget) ? TRUE : FALSE);
+
+    /* Put additional checks here, perhaps if same is FALSE then
+     * set the default drag to GDK_ACTION_COPY.
+     */
+
+    /* Say we just want to allow GDK_ACTION_MOVE, first we check
+     * if that is in the list of allowed actions on the dc. If
+     * so then we set it to that. Note if the user holds down the
+     * ctrl key then the only flag in dc->actions will be
+     * GDK_ACTION_COPY. The constraint for dc->actions is that
+     * specified from the given actions in gtk_drag_dest_set() and
+     * gtk_drag_source_set().
+     */
+    if(same)
+    {
+        if(context->actions == GDK_ACTION_MOVE)
+            gdk_drag_status(context, GDK_ACTION_COPY, time);
+        else
+            gdk_drag_status(context, GDK_ACTION_MOVE, time);
+    }
+    else
+        gdk_drag_status(context, context->actions, time);
+
+    return(TRUE);
+}
+
 void omnplay_init(omnplay_instance_t* app)
 {
     int i;
@@ -1264,6 +1452,26 @@ void omnplay_init(omnplay_instance_t* app)
     omnplay_library_load(app);
 
     pthread_mutexattr_destroy(&attr);
+
+    /* setup drag n drop source/target */
+    static GtkTargetEntry drag_targets[] = { { (char*) "application/playlist_item_t", 0, 0 } };
+
+    gtk_drag_source_set(app->library_grid, GDK_BUTTON1_MASK,
+        drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY));
+
+    gtk_drag_source_set(app->playlist_grid, GDK_BUTTON1_MASK,
+        drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
+
+    gtk_drag_dest_set(app->playlist_grid, (GtkDestDefaults)(GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
+        drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
+
+    g_signal_connect (app->library_grid, "drag_data_get", G_CALLBACK(library_grid_drag_data_get_cb), app);
+    g_signal_connect (app->playlist_grid, "drag_data_get", G_CALLBACK(playlist_grid_drag_data_get_cb), app);
+    g_signal_connect (app->library_grid, "drag_begin", G_CALLBACK(library_grid_drag_begin_cb), app);
+    g_signal_connect (app->playlist_grid, "drag_begin", G_CALLBACK(playlist_grid_drag_begin_cb), app);
+    g_signal_connect (app->playlist_grid, "drag_data_received", G_CALLBACK (playlist_grid_drag_data_received), app);
+    g_signal_connect (app->playlist_grid, "drag_data_delete", G_CALLBACK (playlist_grid_drag_data_delete), app);
+    g_signal_connect (app->playlist_grid, "drag_motion", G_CALLBACK (playlist_grid_drag_motion), app);
 };
 
 void omnplay_release(omnplay_instance_t* app)
@@ -1302,7 +1510,7 @@ void omnplay_playlist_normalize(omnplay_instance_t* app)
 void omnplay_set_status(omnplay_instance_t* app, char* str)
 {
     gdk_threads_enter();
-//    gtk_label_set_text(GTK_LABEL(data), str);
+    gtk_label_set_text(GTK_LABEL(app->status_label), str);
     gdk_flush();
     gdk_threads_leave();
 };