summaryrefslogtreecommitdiff
path: root/src/gtkui/playlist_util.c
blob: 65e86819ff8e84b22ea8863041cdedb84511a082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*  Audacious - Cross-platform multimedia player
 *  Copyright (C) 2005-2011  Audacious development team
 *  Copyright (C) 2010 Michał Lipski <tallica@o2.pl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; under version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses>.
 *
 *  The Audacious team does not consider modular code linking to
 *  Audacious or using our public API to be a derived work.
 */

#include <gtk/gtk.h>

#include <audacious/drct.h>
#include <audacious/playlist.h>
#include <libaudgui/libaudgui.h>
#include <libaudgui/list.h>

#include "playlist_util.h"
#include "ui_playlist_notebook.h"

GtkWidget * playlist_get_treeview (gint playlist)
{
    GtkWidget *page = gtk_notebook_get_nth_page(UI_PLAYLIST_NOTEBOOK, playlist);

    if (!page)
        return NULL;

    return g_object_get_data ((GObject *) page, "treeview");
}

gint playlist_count_selected_in_range (gint list, gint top, gint length)
{
    gint selected = 0;
    gint count;

    for (count = 0; count < length; count ++)
    {
        if (aud_playlist_entry_get_selected (list, top + count))
            selected ++;
    }

    return selected;
}

gint playlist_get_focus (gint list)
{
    GtkWidget * tree = playlist_get_treeview (list);
    g_return_val_if_fail (tree, -1);

    return audgui_list_get_focus (tree);
}

void playlist_song_info (void)
{
    gint list = aud_playlist_get_active ();
    gint focus = playlist_get_focus (list);

    if (focus < 0)
        return;

    audgui_infowin_show (list, focus);
}

void playlist_queue_toggle (void)
{
    gint list = aud_playlist_get_active ();
    gint focus = playlist_get_focus (list);

    if (focus < 0)
        return;

    gint at = aud_playlist_queue_find_entry (list, focus);

    if (at < 0)
        aud_playlist_queue_insert (list, -1, focus);
    else
        aud_playlist_queue_delete (list, at, 1);
}

void playlist_delete_selected (void)
{
    gint list = aud_playlist_get_active ();
    gint focus = playlist_get_focus (list);
    focus -= playlist_count_selected_in_range (list, 0, focus);

    aud_drct_pl_delete_selected (list);

    if (aud_playlist_selected_count (list)) /* song changed? */
        return;

    if (focus == aud_playlist_entry_count (list))
        focus --;
    if (focus >= 0)
    {
        aud_playlist_entry_set_selected (list, focus, TRUE);
        playlist_set_focus (list, focus);
    }
}

void playlist_copy (void)
{
    gchar * text = audgui_urilist_create_from_selected (aud_playlist_get_active ());
    if (! text)
        return;

    gtk_clipboard_set_text (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), text, -1);
    g_free (text);
}

void playlist_cut (void)
{
    playlist_copy ();
    playlist_delete_selected ();
}

void playlist_paste (void)
{
    gchar * text = gtk_clipboard_wait_for_text (gtk_clipboard_get
     (GDK_SELECTION_CLIPBOARD));
    if (! text)
        return;

    gint list = aud_playlist_get_active ();
    audgui_urilist_insert (list, playlist_get_focus (list), text);
    g_free (text);
}

void playlist_shift (gint offset)
{
    gint list = aud_playlist_get_active ();
    gint focus = playlist_get_focus (list);

    if (focus < 0 || ! aud_playlist_entry_get_selected (list, focus))
        return;

    focus += aud_playlist_shift (list, focus, offset);
    playlist_set_focus (list, focus);
}