summaryrefslogtreecommitdiff
path: root/src/skins-qt/playlist.cc
blob: e8e8efea2f9e05e0be5baa03eb78108acf614bcd (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*  Audacious - Cross-platform multimedia player
 *  Copyright (C) 2005-2011  Audacious development team.
 *
 *  BMP - Cross-platform multimedia player
 *  Copyright (C) 2003-2004  BMP development team.
 *
 *  Based on XMMS:
 *  Copyright (C) 1998-2003  XMMS development team.
 *
 *  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 <inttypes.h>
#include <string.h>

#include <libaudcore/audstrings.h>
#include <libaudcore/drct.h>
#include <libaudcore/hook.h>
#include <libaudcore/i18n.h>
#include <libaudcore/playlist.h>
#include <libaudcore/runtime.h>
#include <libaudcore/tuple.h>

#include "actions-mainwin.h"
#include "dnd.h"
#include "drag-handle.h"
#include "menus.h"
#include "plugin.h"
#include "skins_cfg.h"
#include "main.h"
#include "playlist.h"
#include "button.h"
#include "playlist-widget.h"
#include "playlist-slider.h"
#include "textbox.h"
#include "window.h"
#include "view.h"

#define PLAYLISTWIN_MIN_WIDTH           MAINWIN_WIDTH
#define PLAYLISTWIN_MIN_HEIGHT          MAINWIN_HEIGHT
#define PLAYLISTWIN_WIDTH_SNAP          25
#define PLAYLISTWIN_HEIGHT_SNAP         29
#define PLAYLISTWIN_SHADED_HEIGHT       MAINWIN_SHADED_HEIGHT

#define APPEND(b, ...) snprintf (b + strlen (b), sizeof b - strlen (b), __VA_ARGS__)

class PlWindow : public Window
{
public:
    PlWindow (bool shaded) :
    Window (WINDOW_PLAYLIST, & config.playlist_x, & config.playlist_y,
     config.playlist_width, shaded ? PLAYLISTWIN_SHADED_HEIGHT :
     config.playlist_height, shaded) {}

private:
    void draw (QPainter & cr);
    bool button_press (QMouseEvent * event);
    bool scroll (QWheelEvent * event);
};

Window * playlistwin;
PlaylistWidget * playlistwin_list;
TextBox * playlistwin_sinfo;

static Button * playlistwin_shade, * playlistwin_close;
static Button * playlistwin_shaded_shade, * playlistwin_shaded_close;

static PlaylistSlider * playlistwin_slider;
static TextBox * playlistwin_time_min, * playlistwin_time_sec;
static TextBox * playlistwin_info;
static Button * playlistwin_srew, * playlistwin_splay;
static Button * playlistwin_spause, * playlistwin_sstop;
static Button * playlistwin_sfwd, * playlistwin_seject;
static Button * playlistwin_sscroll_up, *playlistwin_sscroll_down;
static DragHandle * resize_handle, * sresize_handle;
static Button * button_add, * button_sub, * button_sel, * button_misc, * button_list;

static int resize_base_width, resize_base_height;
//static int drop_position;
static bool song_changed;

static void update_info ()
{
    auto playlist = Playlist::active_playlist ();
    StringBuf s1 = str_format_time (playlist.selected_length_ms ());
    StringBuf s2 = str_format_time (playlist.total_length_ms ());
    playlistwin_info->set_text (str_concat ({s1, "/", s2}));
}

static void update_rollup_text ()
{
    auto playlist = Playlist::active_playlist ();
    int entry = playlist.get_position ();
    Tuple tuple = playlist.entry_tuple (entry, Playlist::NoWait);
    char scratch[512];

    scratch[0] = 0;

    if (entry > -1)
    {
        String title = tuple.get_str (Tuple::FormattedTitle);
        int length = tuple.get_int (Tuple::Length);

        if (aud_get_bool (nullptr, "show_numbers_in_pl"))
            APPEND (scratch, "%d. ", 1 + entry);

        APPEND (scratch, "%s", (const char *) title);

        if (length >= 0)
        {
            StringBuf buf = str_format_time (length);
            APPEND (scratch, " (%s)", (const char *) buf);
        }
    }

    playlistwin_sinfo->set_text (scratch);
}

static void playlistwin_shade_toggle ()
{
    view_set_playlist_shaded (! aud_get_bool ("skins", "playlist_shaded"));
}

static void playlistwin_scroll (float dir)
{
    int rows, first;

    playlistwin_list->row_info (& rows, & first);
    playlistwin_list->scroll_to (first + (int) (dir * rows / 3));
}

static void playlistwin_scroll_up_pushed ()
{
    playlistwin_scroll (-1);
}

static void playlistwin_scroll_down_pushed ()
{
    playlistwin_scroll (1);
}

/* note: height is ignored if the window is shaded */
static void playlistwin_resize (int w, int h)
{
    int tx, ty;

    tx = (w - PLAYLISTWIN_MIN_WIDTH) / PLAYLISTWIN_WIDTH_SNAP;
    tx = (tx * PLAYLISTWIN_WIDTH_SNAP) + PLAYLISTWIN_MIN_WIDTH;
    if (tx < PLAYLISTWIN_MIN_WIDTH)
        tx = PLAYLISTWIN_MIN_WIDTH;

    if (! aud_get_bool ("skins", "playlist_shaded"))
    {
        ty = (h - PLAYLISTWIN_MIN_HEIGHT) / PLAYLISTWIN_HEIGHT_SNAP;
        ty = (ty * PLAYLISTWIN_HEIGHT_SNAP) + PLAYLISTWIN_MIN_HEIGHT;
        if (ty < PLAYLISTWIN_MIN_HEIGHT)
            ty = PLAYLISTWIN_MIN_HEIGHT;
    }
    else
        ty = config.playlist_height;

    if (tx == config.playlist_width && ty == config.playlist_height)
        return;

    config.playlist_width = w = tx;
    config.playlist_height = h = ty;

    playlistwin_list->resize (w - 31, h - 58);
    playlistwin->move_widget (false, playlistwin_slider, w - 15, 20);
    playlistwin_slider->resize (h - 58);

    playlistwin->move_widget (true, playlistwin_shaded_shade, w - 21, 3);
    playlistwin->move_widget (true, playlistwin_shaded_close, w - 11, 3);
    playlistwin->move_widget (false, playlistwin_shade, w - 21, 3);
    playlistwin->move_widget (false, playlistwin_close, w - 11, 3);

    playlistwin->move_widget (false, playlistwin_time_min, w - 82, h - 15);
    playlistwin->move_widget (false, playlistwin_time_sec, w - 64, h - 15);
    playlistwin->move_widget (false, playlistwin_info, w - 143, h - 28);

    playlistwin->move_widget (false, playlistwin_srew, w - 144, h - 16);
    playlistwin->move_widget (false, playlistwin_splay, w - 138, h - 16);
    playlistwin->move_widget (false, playlistwin_spause, w - 128, h - 16);
    playlistwin->move_widget (false, playlistwin_sstop, w - 118, h - 16);
    playlistwin->move_widget (false, playlistwin_sfwd, w - 109, h - 16);
    playlistwin->move_widget (false, playlistwin_seject, w - 100, h - 16);
    playlistwin->move_widget (false, playlistwin_sscroll_up, w - 14, h - 35);
    playlistwin->move_widget (false, playlistwin_sscroll_down, w - 14, h - 30);

    playlistwin->move_widget (false, resize_handle, w - 20, h - 20);
    playlistwin->move_widget (true, sresize_handle, w - 31, 0);

    playlistwin_sinfo->set_width (w - 35);

    playlistwin->move_widget (false, button_add, 12, h - 29);
    playlistwin->move_widget (false, button_sub, 40, h - 29);
    playlistwin->move_widget (false, button_sel, 68, h - 29);
    playlistwin->move_widget (false, button_misc, 100, h - 29);
    playlistwin->move_widget (false, button_list, w - 46, h - 29);
}

bool PlWindow::scroll (QWheelEvent * event)
{
    float delta = -event->angleDelta ().y () / 120.0f;
    if (delta)
        playlistwin_scroll (delta);

    return true;
}

bool PlWindow::button_press (QMouseEvent * event)
{
    if (event->button () == Qt::LeftButton &&
     event->type () == QEvent::MouseButtonDblClick && event->y () < 14)
    {
        playlistwin_shade_toggle ();
        return true;
    }

    if (event->button () == Qt::RightButton && event->type () == QEvent::MouseButtonPress)
    {
        menu_popup (UI_MENU_PLAYLIST, event->globalX (), event->globalY (), false, false);
        return true;
    }

    return Window::button_press (event);
}

void playlistwin_hide_timer ()
{
    playlistwin_time_min->set_text (nullptr);
    playlistwin_time_sec->set_text (nullptr);
}

void playlistwin_set_time (const char * minutes, const char * seconds)
{
    playlistwin_time_min->set_text (minutes);
    playlistwin_time_sec->set_text (seconds);
}

#if 0
static void drag_motion (GtkWidget * widget, GdkDragContext * context, int x,
 int y, unsigned time, void * unused)
{
    if (! aud_get_bool ("skins", "playlist_shaded"))
        playlistwin_list->hover (x - 12, y - 20);
}

static void drag_leave (GtkWidget * widget, GdkDragContext * context, unsigned time,
 void * unused)
{
    if (! aud_get_bool ("skins", "playlist_shaded"))
        playlistwin_list->hover_end ();
}

static void drag_drop (GtkWidget * widget, GdkDragContext * context, int x,
 int y, unsigned time, void * unused)
{
    if (aud_get_bool ("skins", "playlist_shaded"))
        drop_position = -1;
    else
    {
        playlistwin_list->hover (x - 12, y - 20);
        drop_position = playlistwin_list->hover_end ();
    }
}

static void drag_data_received (GtkWidget * widget, GdkDragContext * context,
 int x, int y, GtkSelectionData * data, unsigned info, unsigned time, void * unused)
{
    audgui_urilist_insert (Playlist::active_playlist (), drop_position,
     (const char *) gtk_selection_data_get_data (data));
    drop_position = -1;
}
#endif

static void playlistwin_hide ()
{
    view_set_show_playlist (false);
}

static void resize_press ()
{
    resize_base_width = config.playlist_width;
    resize_base_height = config.playlist_height;
}

static void resize_drag (int x_offset, int y_offset)
{
    bool shaded = aud_get_bool ("skins", "playlist_shaded");

    /* compromise between rounding and truncating; this has no real
     * justification at all other than it "looks about right". */
    playlistwin_resize (resize_base_width + x_offset + PLAYLISTWIN_WIDTH_SNAP /
     3, resize_base_height + y_offset + PLAYLISTWIN_HEIGHT_SNAP / 3);
    playlistwin->resize (config.playlist_width, shaded ?
     PLAYLISTWIN_SHADED_HEIGHT : config.playlist_height);
}

static void button_add_cb (Button * button, QMouseEvent * event)
{
    int xpos, ypos;
    playlistwin->getPosition (& xpos, & ypos);
    menu_popup (UI_MENU_PLAYLIST_ADD, xpos + 12 * config.scale,
     ypos + (config.playlist_height - 8) * config.scale, false, true);
}

static void button_sub_cb (Button * button, QMouseEvent * event)
{
    int xpos, ypos;
    playlistwin->getPosition (& xpos, & ypos);
    menu_popup (UI_MENU_PLAYLIST_REMOVE, xpos + 40 * config.scale,
     ypos + (config.playlist_height - 8) * config.scale, false, true);
}

static void button_sel_cb (Button * button, QMouseEvent * event)
{
    int xpos, ypos;
    playlistwin->getPosition (& xpos, & ypos);
    menu_popup (UI_MENU_PLAYLIST_SELECT, xpos + 68 * config.scale,
     ypos + (config.playlist_height - 8) * config.scale, false, true);
}

static void button_misc_cb (Button * button, QMouseEvent * event)
{
    int xpos, ypos;
    playlistwin->getPosition (& xpos, & ypos);
    menu_popup (UI_MENU_PLAYLIST_SORT, xpos + 100 * config.scale,
     ypos + (config.playlist_height - 8) * config.scale, false, true);
}

static void button_list_cb (Button * button, QMouseEvent * event)
{
    int xpos, ypos;
    playlistwin->getPosition (& xpos, & ypos);
    menu_popup (UI_MENU_PLAYLIST,
     xpos + (config.playlist_width - 12) * config.scale,
     ypos + (config.playlist_height - 8) * config.scale, true, true);
}

static void playlistwin_create_widgets ()
{
    int w = config.playlist_width, h = config.playlist_height;

    bool shaded = aud_get_bool ("skins", "playlist_shaded");
    playlistwin_sinfo = new TextBox (w - 35, nullptr, shaded && config.autoscroll);
    playlistwin->put_widget (true, playlistwin_sinfo, 4, 4);

    playlistwin_shaded_shade = new Button (9, 9, 128, 45, 150, 42, SKIN_PLEDIT, SKIN_PLEDIT);
    playlistwin->put_widget (true, playlistwin_shaded_shade, w - 21, 3);
    playlistwin_shaded_shade->on_release ((ButtonCB) playlistwin_shade_toggle);

    playlistwin_shaded_close = new Button (9, 9, 138, 45, 52, 42, SKIN_PLEDIT, SKIN_PLEDIT);
    playlistwin->put_widget (true, playlistwin_shaded_close, w - 11, 3);
    playlistwin_shaded_close->on_release ((ButtonCB) playlistwin_hide);

    playlistwin_shade = new Button (9, 9, 157, 3, 62, 42, SKIN_PLEDIT, SKIN_PLEDIT);
    playlistwin->put_widget (false, playlistwin_shade, w - 21, 3);
    playlistwin_shade->on_release ((ButtonCB) playlistwin_shade_toggle);

    playlistwin_close = new Button (9, 9, 167, 3, 52, 42, SKIN_PLEDIT, SKIN_PLEDIT);
    playlistwin->put_widget (false, playlistwin_close, w - 11, 3);
    playlistwin_close->on_release ((ButtonCB) playlistwin_hide);

    String font = aud_get_str ("skins", "playlist_font");
    playlistwin_list = new PlaylistWidget (w - 31, h - 58, font);
    playlistwin->put_widget (false, playlistwin_list, 12, 20);

    /* playlist list box slider */
    playlistwin_slider = new PlaylistSlider (playlistwin_list, h - 58);
    playlistwin->put_widget (false, playlistwin_slider, w - 15, 20);
    playlistwin_list->set_slider (playlistwin_slider);

    playlistwin_time_min = new TextBox (15, nullptr, false);
    playlistwin->put_widget (false, playlistwin_time_min, w - 82, h - 15);
    playlistwin_time_min->on_press (change_timer_mode_cb);

    playlistwin_time_sec = new TextBox (10, nullptr, false);
    playlistwin->put_widget (false, playlistwin_time_sec, w - 64, h - 15);
    playlistwin_time_sec->on_press (change_timer_mode_cb);

    playlistwin_info = new TextBox (90, nullptr, false);
    playlistwin->put_widget (false, playlistwin_info, w - 143, h - 28);

    /* mini play control buttons at right bottom corner */

    playlistwin_srew = new Button (8, 7);
    playlistwin->put_widget (false, playlistwin_srew, w - 144, h - 16);
    playlistwin_srew->on_release ((ButtonCB) aud_drct_pl_prev);

    playlistwin_splay = new Button (10, 7);
    playlistwin->put_widget (false, playlistwin_splay, w - 138, h - 16);
    playlistwin_splay->on_release ((ButtonCB) aud_drct_play);

    playlistwin_spause = new Button (10, 7);
    playlistwin->put_widget (false, playlistwin_spause, w - 128, h - 16);
    playlistwin_spause->on_release ((ButtonCB) aud_drct_pause);

    playlistwin_sstop = new Button (9, 7);
    playlistwin->put_widget (false, playlistwin_sstop, w - 118, h - 16);
    playlistwin_sstop->on_release ((ButtonCB) aud_drct_stop);

    playlistwin_sfwd = new Button (8, 7);
    playlistwin->put_widget (false, playlistwin_sfwd, w - 109, h - 16);
    playlistwin_sfwd->on_release ((ButtonCB) aud_drct_pl_next);

    playlistwin_seject = new Button (9, 7);
    playlistwin->put_widget (false, playlistwin_seject, w - 100, h - 16);
    playlistwin_seject->on_release ((ButtonCB) action_play_file);

    playlistwin_sscroll_up = new Button (8, 5);
    playlistwin->put_widget (false, playlistwin_sscroll_up, w - 14, h - 35);
    playlistwin_sscroll_up->on_release ((ButtonCB) playlistwin_scroll_up_pushed);

    playlistwin_sscroll_down = new Button (8, 5);
    playlistwin->put_widget (false, playlistwin_sscroll_down, w - 14, h - 30);
    playlistwin_sscroll_down->on_release ((ButtonCB) playlistwin_scroll_down_pushed);

    /* resize handles */

    resize_handle = new DragHandle (20, 20, resize_press, resize_drag);
    playlistwin->put_widget (false, resize_handle, w - 20, h - 20);

    sresize_handle = new DragHandle (9, PLAYLISTWIN_SHADED_HEIGHT, resize_press, resize_drag);
    playlistwin->put_widget (true, sresize_handle, w - 31, 0);

    /* lower button row */

    button_add = new Button (25, 18);
    playlistwin->put_widget (false, button_add, 12, h - 29);
    button_add->on_press (button_add_cb);

    button_sub = new Button (25, 18);
    playlistwin->put_widget (false, button_sub, 40, h - 29);
    button_sub->on_press (button_sub_cb);

    button_sel = new Button (25, 18);
    playlistwin->put_widget (false, button_sel, 68, h - 29);
    button_sel->on_press (button_sel_cb);

    button_misc = new Button (25, 18);
    playlistwin->put_widget (false, button_misc, 100, h - 29);
    button_misc->on_press (button_misc_cb);

    button_list = new Button (23, 18);
    playlistwin->put_widget (false, button_list, w - 46, h - 29);
    button_list->on_press (button_list_cb);
}

void PlWindow::draw (QPainter & cr)
{
    if (is_shaded ())
        skin_draw_playlistwin_shaded (cr, config.playlist_width, true);
    else
        skin_draw_playlistwin_frame (cr, config.playlist_width,
         config.playlist_height, true);
}

static void playlistwin_create_window ()
{
    bool shaded = aud_get_bool ("skins", "playlist_shaded");

    playlistwin = new PlWindow (shaded);
    playlistwin->setWindowTitle (_("Audacious Playlist Editor"));

#if 0
    GtkWidget * w = playlistwin->gtk ();
    drag_dest_set (w);
    drop_position = -1;

    g_signal_connect (w, "drag-motion", (GCallback) drag_motion, nullptr);
    g_signal_connect (w, "drag-leave", (GCallback) drag_leave, nullptr);
    g_signal_connect (w, "drag-drop", (GCallback) drag_drop, nullptr);
    g_signal_connect (w, "drag-data-received", (GCallback) drag_data_received, nullptr);
#endif
}

static void update_cb (void *, void *)
{
    playlistwin_list->refresh ();

    if (song_changed)
    {
        playlistwin_list->set_focused (Playlist::active_playlist ().get_position ());
        song_changed = false;
    }

    update_info ();
    update_rollup_text ();
}

static void follow_cb (void * data, void *)
{
    auto list = aud::from_ptr<Playlist> (data);
    list.select_all (false);

    int row = list.get_position ();
    if (row >= 0)
        list.select_entry (row, true);

    if (list == Playlist::active_playlist ())
        song_changed = true;
}

void playlistwin_create ()
{
    playlistwin_create_window ();
    playlistwin_create_widgets ();

    update_info ();
    update_rollup_text ();

    song_changed = false;

    hook_associate ("playlist position", follow_cb, nullptr);
    hook_associate ("playlist activate", update_cb, nullptr);
    hook_associate ("playlist update", update_cb, nullptr);
}

void playlistwin_unhook ()
{
    hook_dissociate ("playlist position", follow_cb);
    hook_dissociate ("playlist activate", update_cb);
    hook_dissociate ("playlist update", update_cb);
}