summaryrefslogtreecommitdiff
path: root/plugins/gtkui/tabs.c
blob: 6d472e3b10e22ef9399c76396223e8d1941281c9 (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
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>

    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; either version 2
    of the License, or (at your option) any later version.
    
    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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
#include "../../deadbeef.h"
#include <gtk/gtk.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include <string.h>
#include "gtkui.h"
#include "interface.h"
#include "support.h"
#include "drawing.h"

static int margin_size = 10;
static int tab_dragging = -1;
static int tab_movepos;
static int tab_clicked = -1;

void
tabbar_draw (GtkWidget *widget) {
    GdkDrawable *backbuf = widget->window;
    int hscrollpos = 0;
    int x = -hscrollpos;
    int w = 0;
    int h = widget->allocation.height;
    const char *detail = "button";
    int cnt = deadbeef->plt_get_count ();
    int tab_selected = deadbeef->plt_get_curr ();

    // fill background
    gdk_draw_rectangle (backbuf, widget->style->bg_gc[GTK_STATE_NORMAL], TRUE, 0, 0, widget->allocation.width, widget->allocation.height);
    draw_begin ((uintptr_t)backbuf);
    int need_draw_moving = 0;
    int idx;
    int widths[cnt];
    int fullwidth = 0;
    for (idx = 0; idx < cnt; idx++) {
        const char *title = deadbeef->plt_get_title (idx);
        widths[idx] = 0;
        int h = 0;
        draw_get_text_extents (title, strlen (title), &widths[idx], &h);
        widths[idx] += margin_size + 10;
        fullwidth += widths[idx];
    }
    fullwidth += margin_size;

    x = -hscrollpos + fullwidth;
    for (idx = cnt-1; idx >= 0; idx--) {
        w = widths[idx];
        x -= w + margin_size;
        GdkRectangle area;
        area.x = x;
        area.y = 0;
        area.width = w + margin_size;
        area.height = 24;
        if (idx != tab_selected) {
            gtk_paint_box (widget->style, widget->window, idx == tab_selected ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL, GTK_SHADOW_OUT, &area, widget, "button", x, idx == tab_selected ? 0 : 1, w+margin_size, 32);
            GdkColor *gdkfg = &widget->style->fg[0];
            float fg[3] = {(float)gdkfg->red/0xffff, (float)gdkfg->green/0xffff, (float)gdkfg->blue/0xffff};
            draw_set_fg_color (fg);
            const char *tab_title = deadbeef->plt_get_title (idx);
            draw_text (x + margin_size + 5, h/2-draw_get_font_size()/2, w - margin_size, 0, tab_title);
        }
        x += margin_size;
    }
    gdk_draw_line (backbuf, widget->style->dark_gc[GTK_STATE_NORMAL], 0, widget->allocation.height-1, widget->allocation.width, widget->allocation.height-1);
    // calc position for drawin selected tab
    x = -hscrollpos;
    for (idx = 0; idx < tab_selected; idx++) {
        x += widths[idx];
    }
    // draw selected
    {
        idx = tab_selected;
        w = widths[tab_selected] + margin_size;
        GdkRectangle area;
        area.x = x;
        area.y = 0;
        area.width = w;
        area.height = 24;
        gtk_paint_box (widget->style, widget->window, GTK_STATE_PRELIGHT, GTK_SHADOW_OUT, &area, widget, "button", x, idx == tab_selected ? 0 : 1, w, 32);
        GdkColor *gdkfg = &widget->style->fg[0];
        float fg[3] = {(float)gdkfg->red/0xffff, (float)gdkfg->green/0xffff, (float)gdkfg->blue/0xffff};
        draw_set_fg_color (fg);
        const char *tab_title = deadbeef->plt_get_title (idx);
        draw_text (x + margin_size + 5, h/2-draw_get_font_size()/2, w - margin_size, 0, tab_title);
    }
    if (need_draw_moving) {
        x = -hscrollpos;
        for (idx = 0; idx < 10; idx++) {
            w = widths[idx];
            if (idx == tab_dragging) {
                // draw empty slot
                if (x < widget->allocation.width) {
                    gtk_paint_box (widget->style, backbuf, GTK_STATE_ACTIVE, GTK_SHADOW_ETCHED_IN, NULL, widget, "button", x, 0, w, h);
                }
                x = tab_movepos;
                if (x >= widget->allocation.width) {
                    break;
                }
                if (w > 0) {
                    gtk_paint_box (widget->style, backbuf, GTK_STATE_SELECTED, GTK_SHADOW_OUT, NULL, widget, "button", x, 0, w, h);
                    GdkColor *gdkfg = &widget->style->fg[GTK_STATE_SELECTED];
                    float fg[3] = {(float)gdkfg->red/0xffff, (float)gdkfg->green/0xffff, (float)gdkfg->blue/0xffff};
                    draw_set_fg_color (fg);
                    //draw_text (x + 5, h/2-draw_get_font_size()/2, tab_width-10, 0, tab_title);
                }
                break;
            }
            x += w;
        }
    }
    draw_end ();
}

static int
get_tab_under_cursor (int x) {
    int idx;
    int cnt = deadbeef->plt_get_count ();
    int fw = 0;
    int tab_selected = deadbeef->plt_get_curr ();
    for (idx = 0; idx < cnt; idx++) {
        const char *title = deadbeef->plt_get_title (idx);
        int w = 0;
        int h = 0;
        draw_get_text_extents (title, strlen (title), &w, &h);
        w += margin_size + 10;
        if (tab_selected == idx) {
            w += margin_size;
        }
        fw += w;
        if (fw > x) {
            return idx;
        }
    }
    return -1;
}

gboolean
on_tabbar_button_press_event           (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{
    tab_clicked = get_tab_under_cursor (event->x);
    if (event->button == 1)
    {
        if (tab_clicked != -1) {
            deadbeef->plt_set_curr (tab_clicked);
        }
    }
    else if (event->button == 3) {
        GtkWidget *menu = create_plmenu ();
        gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, widget, 0, gtk_get_current_event_time());
    }
    return FALSE;
}


gboolean
on_tabbar_button_release_event         (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{

  return FALSE;
}


gboolean
on_tabbar_configure_event              (GtkWidget       *widget,
                                        GdkEventConfigure *event,
                                        gpointer         user_data)
{

  return FALSE;
}


gboolean
on_tabbar_expose_event                 (GtkWidget       *widget,
                                        GdkEventExpose  *event,
                                        gpointer         user_data)
{
    tabbar_draw (widget);
    return FALSE;
}


gboolean
on_tabbar_motion_notify_event          (GtkWidget       *widget,
                                        GdkEventMotion  *event,
                                        gpointer         user_data)
{
  return FALSE;
}

void
on_rename_playlist1_activate           (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
    GtkWidget *dlg = create_editplaylistdlg ();
    int res = gtk_dialog_run (GTK_DIALOG (dlg));
    if (res == GTK_RESPONSE_OK) {
        GtkWidget *e = lookup_widget (dlg, "title");
        const char *text = gtk_entry_get_text (GTK_ENTRY (e));
        deadbeef->plt_set_title (tab_clicked, text);
        extern GtkWidget *mainwin;
        tabbar_draw (lookup_widget (mainwin, "tabbar"));
    }
    gtk_widget_destroy (dlg);
}


void
on_remove_playlist1_activate           (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
    if (tab_clicked != -1) {
        deadbeef->plt_remove (tab_clicked);
    }
}


void
on_add_new_playlist1_activate          (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
    int cnt = deadbeef->plt_get_count ();
    int i;
    int idx = 0;
    for (;;) {
        char name[100];
        if (!idx) {
            strcpy (name, "New Playlist");
        }
        else {
            snprintf (name, sizeof (name), "New Playlist (%d)", idx);
        }
        for (i = 0; i < cnt; i++) {
            const char *t = deadbeef->plt_get_title (i);
            if (!strcasecmp (t, name)) {
                break;
            }
        }
        if (i == cnt) {
            deadbeef->plt_add (cnt, name);
            break;
        }
        idx++;
    }
}


void
on_load_playlist1_activate             (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}


void
on_save_playlist1_activate             (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}


void
on_save_all_playlists1_activate        (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}