summaryrefslogtreecommitdiff
path: root/src/libaudcore/preferences.h
blob: 8946b01782371cd84aa6f889afbb270b01e4b2f8 (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
/*
 * preferences.h
 * Copyright 2007-2014 Tomasz Moń, William Pitcock, 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.
 */

#ifndef LIBAUDCORE_PREFERENCES_H
#define LIBAUDCORE_PREFERENCES_H

#include <libaudcore/objects.h>

struct PreferencesWidget;

enum class FileSelectMode {
    File,
    Folder
};

struct ComboItem {
    const char * label;
    const char * str;
    int num;

    constexpr ComboItem (const char * label, const char * str) :
        label (label),
        str (str),
        num (-1) {}

    constexpr ComboItem (const char * label, int num) :
        label (label),
        str (nullptr),
        num (num) {}
};

struct WidgetVButton {
    void (* callback) ();
    const char * icon;
};

struct WidgetVRadio {
    int value;
};

struct WidgetVSpin {
    double min, max, step;
    const char * right_label; /* text right to widget */
};

struct WidgetVTable {
    ArrayRef<PreferencesWidget> widgets;
};

struct WidgetVFonts {
    const char * title;
};

struct WidgetVEntry {
    bool password;
};

struct WidgetVFileEntry {
    FileSelectMode mode;
};

struct WidgetVCombo {
    /* static init */
    ArrayRef<ComboItem> elems;

    /* runtime init */
    ArrayRef<ComboItem> (* fill) ();
};

struct WidgetVBox {
    ArrayRef<PreferencesWidget> widgets;

    bool horizontal;  /* false gives vertical, true gives horizontal alignment of child widgets */
    bool frame;       /* whether to draw frame around box */
};

struct NotebookTab {
    const char * name;
    ArrayRef<PreferencesWidget> widgets;
};

struct WidgetVNotebook {
    ArrayRef<NotebookTab> tabs;
};

struct WidgetVSeparator {
    bool horizontal;  /* false gives vertical, true gives horizontal separator */
};

union WidgetVariant {
    WidgetVButton button;
    WidgetVRadio radio_btn;
    WidgetVSpin spin_btn;
    WidgetVTable table;
    WidgetVFonts font_btn;
    WidgetVEntry entry;
    WidgetVFileEntry file_entry;
    WidgetVCombo combo;
    WidgetVBox box;
    WidgetVNotebook notebook;
    WidgetVSeparator separator;

    /* for custom GTK or Qt widgets */
    /* GtkWidget * (* populate) (); */
    /* QWidget * (* populate) (); */
    void * (* populate) ();

    constexpr WidgetVariant (WidgetVButton button) : button (button) {}
    constexpr WidgetVariant (WidgetVRadio radio) : radio_btn (radio) {}
    constexpr WidgetVariant (WidgetVSpin spin) : spin_btn (spin) {}
    constexpr WidgetVariant (WidgetVTable table) : table (table) {}
    constexpr WidgetVariant (WidgetVFonts fonts) : font_btn (fonts) {}
    constexpr WidgetVariant (WidgetVEntry entry) : entry (entry) {}
    constexpr WidgetVariant (WidgetVFileEntry file_entry) : file_entry (file_entry) {}
    constexpr WidgetVariant (WidgetVCombo combo) : combo (combo) {}
    constexpr WidgetVariant (WidgetVBox box) : box (box) {}
    constexpr WidgetVariant (WidgetVNotebook notebook) : notebook (notebook) {}
    constexpr WidgetVariant (WidgetVSeparator separator) : separator (separator) {}

    /* also serves as default constructor */
    constexpr WidgetVariant (void * (* populate) () = 0) : populate (populate) {}
};

struct WidgetConfig
{
    enum Type {
        None,
        Bool,
        Int,
        Float,
        String
    };

    Type type;

    /* pointer to immediate value */
    void * value;
    /* identifier for configuration value */
    const char * section, * name;
    /* called when value is changed  */
    void (* callback) ();
    /* widget updates when this hook is called */
    const char * hook;

    constexpr WidgetConfig () :
        type (None),
        value (nullptr),
        section (nullptr),
        name (nullptr),
        callback (nullptr),
        hook (nullptr) {}

    constexpr WidgetConfig (Type type, void * value, const char * section,
     const char * name, void (* callback) (), const char * hook) :
        type (type),
        value (value),
        section (section),
        name (name),
        callback (callback),
        hook (hook) {}

    bool get_bool () const;
    void set_bool (bool val) const;
    int get_int () const;
    void set_int (int val) const;
    double get_float () const;
    void set_float (double val) const;
    ::String get_string () const;
    void set_string (const char * val) const;
};

constexpr WidgetConfig WidgetBool (bool & value,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::Bool, (void *) & value, 0, 0, callback, hook); }
constexpr WidgetConfig WidgetInt (int & value,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::Int, (void *) & value, 0, 0, callback, hook); }
constexpr WidgetConfig WidgetFloat (double & value,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::Float, (void *) & value, 0, 0, callback, hook); }
constexpr WidgetConfig WidgetString (String & value,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::String, (void *) & value, 0, 0, callback, hook); }

constexpr WidgetConfig WidgetBool (const char * section, const char * name,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::Bool, 0, section, name, callback, hook); }
constexpr WidgetConfig WidgetInt (const char * section, const char * name,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::Int, 0, section, name, callback, hook); }
constexpr WidgetConfig WidgetFloat (const char * section, const char * name,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::Float, 0, section, name, callback, hook); }
constexpr WidgetConfig WidgetString (const char * section, const char * name,
 void (* callback) () = nullptr, const char * hook = nullptr)
    { return WidgetConfig (WidgetConfig::String, 0, section, name, callback, hook); }

struct PreferencesWidget
{
    enum Type {
        Label,
        Button,
        CheckButton,
        RadioButton,
        SpinButton,
        Entry,
        FileEntry,
        ComboBox,
        FontButton,
        Box,
        Table,
        Notebook,
        Separator,
        CustomGTK,
        CustomQt
    };

    Type type;
    const char * label;       /* widget title (for SPIN_BTN it's text left to widget) */
    bool child;

    WidgetConfig cfg;
    WidgetVariant data;
};

enum WidgetIsChild {
    WIDGET_NOT_CHILD,
    WIDGET_CHILD
};

constexpr PreferencesWidget WidgetLabel (const char * label,
 WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::Label, label, (child == WIDGET_CHILD)}; }

constexpr PreferencesWidget WidgetButton (const char * label,
 WidgetVButton button, WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::Button, label, (child == WIDGET_CHILD), {}, button}; }

constexpr PreferencesWidget WidgetCheck (const char * label,
 WidgetConfig cfg, WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::CheckButton, label,
       (child == WIDGET_CHILD), cfg}; }

constexpr PreferencesWidget WidgetRadio (const char * label,
 WidgetConfig cfg, WidgetVRadio radio, WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::RadioButton, label,
       (child == WIDGET_CHILD), cfg, radio}; }

constexpr PreferencesWidget WidgetSpin (const char * label,
 WidgetConfig cfg, WidgetVSpin spin, WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::SpinButton, label,
       (child == WIDGET_CHILD), cfg, spin}; }

constexpr PreferencesWidget WidgetEntry (const char * label,
 WidgetConfig cfg, WidgetVEntry entry = WidgetVEntry(),
 WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::Entry, label,
       (child == WIDGET_CHILD), cfg, entry}; }

constexpr PreferencesWidget WidgetFileEntry (const char * label,
 WidgetConfig cfg, WidgetVFileEntry file_entry = WidgetVFileEntry(),
 WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::FileEntry, label,
       (child == WIDGET_CHILD), cfg, file_entry}; }

constexpr PreferencesWidget WidgetCombo (const char * label,
 WidgetConfig cfg, WidgetVCombo combo, WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::ComboBox, label,
       (child == WIDGET_CHILD), cfg, combo}; }

constexpr PreferencesWidget WidgetFonts (const char * label,
 WidgetConfig cfg, WidgetVFonts fonts, WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::FontButton, label,
       (child == WIDGET_CHILD), cfg, fonts}; }

constexpr PreferencesWidget WidgetBox (WidgetVBox box, WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::Box, 0, (child == WIDGET_CHILD), {}, box}; }

constexpr PreferencesWidget WidgetTable (WidgetVTable table,
 WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::Table, 0, (child == WIDGET_CHILD), {}, table}; }

constexpr PreferencesWidget WidgetNotebook (WidgetVNotebook notebook)
    { return {PreferencesWidget::Notebook, 0, 0, {}, notebook}; }

constexpr PreferencesWidget WidgetSeparator (WidgetVSeparator separator = WidgetVSeparator ())
    { return {PreferencesWidget::Separator, 0, 0, {}, separator}; }

constexpr PreferencesWidget WidgetCustomGTK (void * (* populate) (),
 WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::CustomGTK, 0, (child == WIDGET_CHILD), {}, populate}; }

constexpr PreferencesWidget WidgetCustomQt (void * (* populate) (),
 WidgetIsChild child = WIDGET_NOT_CHILD)
    { return {PreferencesWidget::CustomQt, 0, (child == WIDGET_CHILD), {}, populate}; }

struct PluginPreferences {
    ArrayRef<PreferencesWidget> widgets;

    void (* init) ();
    void (* apply) ();
    void (* cleanup) ();
};

#endif /* LIBAUDCORE_PREFERENCES_H */