summaryrefslogtreecommitdiff
path: root/src/libaudgui/file-opener.cc
blob: a51abb8b9cea4cfff27dd4feb2c74f8aef874989 (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
/*
 * file-opener.c
 * Copyright 2007-2013 Michael Färber and John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <gtk/gtk.h>

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

#include "internal.h"
#include "libaudgui.h"
#include "libaudgui-gtk.h"

static Index<PlaylistAddItem> get_files (GtkWidget * chooser)
{
    Index<PlaylistAddItem> index;
    GSList * list = gtk_file_chooser_get_uris ((GtkFileChooser *) chooser);

    for (GSList * node = list; node; node = node->next)
        index.append (String ((const char *) node->data));

    g_slist_free_full (list, g_free);
    return index;
}

static void open_cb (void * data)
{
    GtkWidget * chooser = (GtkWidget *) data;
    Index<PlaylistAddItem> files = get_files (chooser);
    gboolean open = GPOINTER_TO_INT (g_object_get_data ((GObject *) chooser, "do-open"));

    if (open)
        aud_drct_pl_open_list (std::move (files));
    else
        aud_drct_pl_add_list (std::move (files), -1);

    GtkWidget * toggle = (GtkWidget *) g_object_get_data ((GObject *) chooser, "toggle-button");
    if (gtk_toggle_button_get_active ((GtkToggleButton *) toggle))
        audgui_hide_filebrowser ();
}

static void destroy_cb (GtkWidget * chooser)
{
    char * path = gtk_file_chooser_get_current_folder ((GtkFileChooser *) chooser);
    if (path)
    {
        aud_set_str ("audgui", "filesel_path", path);
        g_free (path);
    }
}

static void toggled_cb (GtkToggleButton * toggle, void * option)
{
    aud_set_bool ("audgui", (const char *) option, gtk_toggle_button_get_active (toggle));
}

static GtkWidget * create_filebrowser (gboolean open)
{
    const char * window_title, * verb, * icon, * toggle_text, * option;

    if (open)
    {
        window_title = _("Open Files");
        verb = _("_Open");
        icon = "document-open";
        toggle_text = _("Close _dialog on open");
        option = "close_dialog_open";
    }
    else
    {
        window_title = _("Add Files");
        verb = _("_Add");
        icon = "list-add";
        toggle_text = _("Close _dialog on add");
        option = "close_dialog_add";
    }

    int dpi = audgui_get_dpi ();

    GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_type_hint ((GtkWindow *) window, GDK_WINDOW_TYPE_HINT_DIALOG);
    gtk_window_set_title ((GtkWindow *) window, window_title);
    gtk_window_set_default_size ((GtkWindow *) window, 7 * dpi, 5 * dpi);
    gtk_container_set_border_width ((GtkContainer *) window, 10);

    GtkWidget * vbox = gtk_vbox_new (false, 0);
    gtk_container_add ((GtkContainer *) window, vbox);

    GtkWidget * chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
    gtk_file_chooser_set_select_multiple ((GtkFileChooser *) chooser, true);

    String path = aud_get_str ("audgui", "filesel_path");
    if (path[0])
        gtk_file_chooser_set_current_folder ((GtkFileChooser *) chooser, path);

    gtk_box_pack_start ((GtkBox *) vbox, chooser, true, true, 3);

    GtkWidget * hbox = gtk_hbox_new (false, 0);
    gtk_box_pack_end ((GtkBox *) vbox, hbox, false, false, 3);

    GtkWidget * toggle = gtk_check_button_new_with_mnemonic (toggle_text);
    gtk_toggle_button_set_active ((GtkToggleButton *) toggle, aud_get_bool ("audgui", option));
    g_signal_connect (toggle, "toggled", (GCallback) toggled_cb, (void *) option);
    gtk_box_pack_start ((GtkBox *) hbox, toggle, true, true, 0);

    GtkWidget * bbox = gtk_hbutton_box_new ();
    gtk_button_box_set_layout ((GtkButtonBox *) bbox, GTK_BUTTONBOX_END);
    gtk_box_set_spacing ((GtkBox *) bbox, 6);
    gtk_box_pack_end ((GtkBox *) hbox, bbox, true, true, 0);

    GtkWidget * action_button = audgui_button_new (verb, icon, open_cb, chooser);
    GtkWidget * close_button = audgui_button_new (_("_Close"), "window-close",
     (AudguiCallback) audgui_hide_filebrowser, nullptr);

    gtk_container_add ((GtkContainer *) bbox, close_button);
    gtk_container_add ((GtkContainer *) bbox, action_button);

    gtk_widget_set_can_default (action_button, true);
    gtk_widget_grab_default (action_button);

    g_object_set_data ((GObject *) chooser, "toggle-button", toggle);
    g_object_set_data ((GObject *) chooser, "do-open", GINT_TO_POINTER (open));

    g_signal_connect (chooser, "file-activated", (GCallback) open_cb, nullptr);
    g_signal_connect (chooser, "destroy", (GCallback) destroy_cb, nullptr);

    audgui_destroy_on_escape (window);

    return window;
}

EXPORT void audgui_run_filebrowser (bool open)
{
    audgui_show_unique_window (AUDGUI_FILEBROWSER_WINDOW, create_filebrowser (open));
}

EXPORT void audgui_hide_filebrowser ()
{
    audgui_hide_unique_window (AUDGUI_FILEBROWSER_WINDOW);
}