item move up/down implemented
[melted_gui] / src / omnplay.cpp
index f71a85c..39a6c38 100644 (file)
@@ -415,9 +415,84 @@ 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)
+{
+    int i, j, idx;
+    GtkTreePath* path;
+
+    pthread_mutex_lock(&app->playlist.lock);
+    pthread_mutex_lock(&app->players.lock);
+
+    for(j = 0; j < count; j++)
+    {
+        idx = idxs[j] - j;
+
+        /* fix block types */
+        if(!idx)
+            app->playlist.item[idx - 1].type = (playlist_item_type_t)(app->playlist.item[idx - 1].type |
+                OMNPLAY_PLAYLIST_BLOCK_END);
+        if(idx + 1 < app->playlist.count)
+            app->playlist.item[idx + 1].type = (playlist_item_type_t)(app->playlist.item[idx + 1].type |
+                OMNPLAY_PLAYLIST_BLOCK_BEGIN);
+
+        /* shift playlist items */
+        memmove
+        (
+            &app->playlist.item[idx],
+            &app->playlist.item[idx + 1],
+            (app->playlist.count - idx - 1) * sizeof(playlist_item_t)
+        );
+
+        /* decrement items count */
+        app->playlist.count--;
+
+        /* increment servers indexes */
+        for(i = 0; i < app->players.count; i++)
+            if(app->players.item[i].playlist_start >= idx)
+                app->players.item[i].playlist_start--;
+
+
+    };
+
+    /* redraw playlist */
+    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);
+
+
+    pthread_mutex_unlock(&app->players.lock);
+    pthread_mutex_unlock(&app->playlist.lock);
+};
+
 static void omnplay_playlist_item_del(omnplay_instance_t* app)
 {
+    int i, idx, c;
+    int *list, *list2;
+
+    list = get_selected_items_playlist(app);
+    if(!list) return;
+
+    list2 = (int*)malloc(sizeof(int) * list[0]);
+
+    for(i = 0, c = 0; i < list[0]; i++)
+    {
+        /* check for playing block */
+        if(idx_in_players_range(app, list[i + 1]))
+            continue;
+
+        /* save index */
+        list2[c++] = list[i + 1];
+    };
+
+    if(c)
+        omnplay_playlist_delete_items(app, list2, c);
 
+    free(list2);
+    free(list);
 };
 
 static int omnplay_playlist_insert_check(omnplay_instance_t* app, int idx, playlist_item_type_t* t)
@@ -449,6 +524,7 @@ static void omnplay_playlist_insert_items(omnplay_instance_t* app, int idx,
     playlist_item_t* items, int count)
 {
     int i;
+    GtkTreePath* path;
 
     pthread_mutex_lock(&app->playlist.lock);
     pthread_mutex_lock(&app->players.lock);
@@ -480,6 +556,12 @@ static void omnplay_playlist_insert_items(omnplay_instance_t* app, int idx,
     /* redraw playlist */
     omnplay_playlist_draw(app);
 
+    /* select */
+    path = gtk_tree_path_new_from_indices(idx + count, -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);
 };
@@ -513,7 +595,26 @@ static void omnplay_playlist_item_add(omnplay_instance_t* app, int after)
 
 static void omnplay_playlist_item_edit(omnplay_instance_t* app)
 {
+    int idx;
+    playlist_item_t item;
+
+    /* find insert position */
+    idx = get_first_selected_item_playlist(app);
+
+    if(idx < 0)
+        return;
 
+    /* check for playing block */
+    if(idx_in_players_range(app, idx))
+        return;
+
+    item = app->playlist.item[idx];
+
+    if(ui_playlist_item_dialog(app, &item))
+    {
+        app->playlist.item[idx] = item;
+        omnplay_playlist_draw_item(app, idx);
+    };
 };
 
 static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
@@ -668,6 +769,74 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
     pthread_mutex_unlock(&app->playlist.lock);
 };
 
+static void omnplay_playlist_item_swap(omnplay_instance_t* app, int dir)
+{
+    int sel, a, b;
+    GtkTreePath* path;
+    playlist_item_t item;
+
+    /* find insert position */
+    sel = get_first_selected_item_playlist(app);
+    if(sel < 0)
+        return;
+
+    if(dir < 0)
+    {
+        a = sel - 1;
+        b = sel;
+        sel = a;
+    }
+    else
+    {
+        a = sel;
+        b = sel + 1;
+        sel = b;
+    };
+
+    /* check for playing block */
+    if(idx_in_players_range(app, a) || idx_in_players_range(app, b))
+        return;
+
+    pthread_mutex_lock(&app->playlist.lock);
+    pthread_mutex_lock(&app->players.lock);
+
+    /* swap */
+    item = app->playlist.item[a];
+    app->playlist.item[a] = app->playlist.item[b];
+    app->playlist.item[b] = item;
+
+    /* rewite type */
+    app->playlist.item[a].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE;
+    app->playlist.item[b].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE;
+
+    /* redraw main items */
+    omnplay_playlist_draw_item(app, a);
+    omnplay_playlist_draw_item(app, b);
+
+    /* fix block types */
+    if(!a)
+    {
+        app->playlist.item[a - 1].type = (playlist_item_type_t)(app->playlist.item[a - 1].type |
+            OMNPLAY_PLAYLIST_BLOCK_END);
+        omnplay_playlist_draw_item(app, a - 1);
+    };
+    if(b + 1 < app->playlist.count)
+    {
+        app->playlist.item[b + 1].type = (playlist_item_type_t)(app->playlist.item[b + 1].type |
+            OMNPLAY_PLAYLIST_BLOCK_BEGIN);
+        omnplay_playlist_draw_item(app, b + 1);
+    };
+
+    /* select */
+    path = gtk_tree_path_new_from_indices(sel, -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);
+};
+
 static gboolean omnplay_button_click(omnplay_instance_t* app, control_buttons_t button)
 {
     switch(button)
@@ -692,7 +861,10 @@ static gboolean omnplay_button_click(omnplay_instance_t* app, control_buttons_t
             omnplay_playlist_block(app, button);
             break;
         case BUTTON_PLAYLIST_ITEM_UP:
+            omnplay_playlist_item_swap(app, -1);
+            break;
         case BUTTON_PLAYLIST_ITEM_DOWN:
+            omnplay_playlist_item_swap(app, +1);
             break;
         case BUTTON_PLAYER_CUE:
         case BUTTON_PLAYER_PLAY: