dialog_prefs.html 44.7 KB
Newer Older
pokergaming's avatar
pokergaming committed
1
<!DOCTYPE html>
2
<html id="prefs_html_element">
pokergaming's avatar
pokergaming committed
3
  <head>
4
    <link id="page_style" rel="stylesheet" type="text/css" href="css/default.css">
pokergaming's avatar
pokergaming committed
5
  </head>
6
  <body class="dialog_body prefs_body">
7
8
9
10
11
12
13
14
15
    <div class="container noselect prefs_container">
      <div class="prefs_tab_group">
        <input type="radio"
               name="prefs_radio_group"
               id="audio_tab_radio"
               class="tab1 prefs_tab"
               checked="checked" />
        <label for="audio_tab_radio" data-i18n="[title]prefs.heading.audio_tt">
          <span data-i18n="prefs.heading.audio"></span>
16
        </label>
17
18
19
20
21
22
23

        <input type="radio"
               name="prefs_radio_group"
               id="midi_tab_radio"
               class="tab2 prefs_tab"/>
        <label for="midi_tab_radio" data-i18n="[title]prefs.heading.midi_tt">
          <span data-i18n="prefs.heading.midi"></span>
24
        </label>
25
26
27
28
29
30
31

        <input type="radio"
               name="prefs_radio_group"
               id="gui_tab_radio"
               class="tab3 prefs_tab"/>
        <label for="gui_tab_radio" data-i18n="[title]prefs.heading.gui_tt">
          <span data-i18n="prefs.heading.gui"></span>
32
        </label>
33

34
35
36
37
38
39
40
41
        <input type="radio"
               name="prefs_radio_group"
               id="startup_tab_radio"
               class="tab4 prefs_tab"/>
        <label for="startup_tab_radio" data-i18n="[title]prefs.heading.startup_tt">
          <span data-i18n="prefs.heading.startup"></span>
        </label>

42
        <div class="tab1">
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
          <table class="tab_settings">
            <tr data-i18n="[title]prefs.audio.api_tt">
              <td>
                <span data-i18n="prefs.audio.api"></span>
              </td>
              <td>
                <select id="audio_api" onchange="change_api(this);">
                </select>
              </td>
            </tr>
            <tr data-i18n="[title]prefs.audio.sr_tt">
              <td>
                <span data-i18n="prefs.audio.sr"></span>
              </td>
              <td>
                <input type="text"
                       id="rate"
                       name="rate"
                       onchange="attr_change(this);">
              </td>
            </tr>
            <tr data-i18n="[title]prefs.audio.blocksize_tt">
              <td>
                <span data-i18n="prefs.audio.blocksize"></span>
              </td>
              <td>
                <select id="blocksize"
                        onchange="attr_change(this);">
                  <option value="64">64</option>
                  <option value="128">128</option>
                  <option value="256">256</option>
                  <option value="512">512</option>
                  <option value="1024">1024</option>
                  <option value="2048">2048</option>
                </select>
              </td>
            <tr data-i18n="[title]prefs.audio.advance_tt">
              <td>
                <span data-i18n="prefs.audio.advance"></span>
              </td>
              <td>
                <input type="text"
                       id="advance"
                       name="advance"
                       onchange="attr_change(this);">
              </td>
            </tr>
            <tr id="callback_container" class="hidden"
                data-i18n="[title]prefs.audio.callback_tt">
              <td>
93
                <span data-i18n="prefs.audio.callback"></span>
94
95
              </td>
              <td>
96
                <input type="checkbox" id="callback" name="callback">
97
98
99
100
101
              </td>
            </tr>
            <tr id="multi_dev_container" class="hidden"
                data-i18n="[title]prefs.audio.callback_tt">
              <td>
102
                <span data-i18n="prefs.audio.callback"></span>
103
104
              </td>
              <td>
105
                <input type="checkbox" id="multi-dev" name="multi-dev">
106
107
108
              </td>
            </tr>
          </table>
109
          <table class="tab_settings">
110
            <tr>
111
112
              <td data-i18n="[title]prefs.audio.input_title_tt">
                <span data-i18n="prefs.audio.input_title"></span>
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
              </td>
              <td>
                <span data-i18n="prefs.audio.channels"></span>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in1" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans1"
                   name="inchans1"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in2" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans2"
                   name="inchans2"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in3" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans3"
                   name="inchans3"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in4" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans4"
                   name="inchans4"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
171
172
              <td data-i18n="[title]prefs.audio.output_title_tt">
                <span data-i18n="prefs.audio.output_title"></span>
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
              </td>
              <td>
                <span data-i18n="prefs.audio.channels"></span>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out1" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans1"
                   name="outchans1"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out2" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans2"
                   name="outchans2"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out3" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans3"
                   name="outchans3"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out4" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans4"
                   name="outchans4"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
          </table>
231
232
        </div>

233
        <div class="tab2">
234
          <div class="tab_settings midi_api_container">
235
236
237
238
239
240
            <label data-i18n="[title]prefs.midi.api_tt">
              <span data-i18n="prefs.midi.api"></span>
              <select id="midi_api" onchange="change_api(this);">
              </select>
            </label>
          </div>
241
242
243
244
245
246
          <div class="tab_settings alsa_midi">
            <label data-i18n="[title]prefs.midi.alsa_in_ports_tt">
              <span data-i18n="prefs.midi.alsa_in_ports"></span>
              <input type="text"
                     id="alsa_in_ports"
                     name="alsa_in_ports"
247
                     onchange="alsa_in_ports_change(this);">
248
249
250
251
252
253
            </label>
            <label data-i18n="[title]prefs.midi.alsa_out_ports_tt">
              <span data-i18n="prefs.midi.alsa_out_ports"></span>
              <input type="text"
                     id="alsa_out_ports"
                     name="alsa_out_ports"
254
                     onchange="alsa_out_ports_change(this);">
255
256
            </label>
          </div>
257

258
259
260
261
          <div class="tab_settings midi_devices">
            <span data-i18n="[title]prefs.midi.input_title_tt">
              <span data-i18n="prefs.midi.input_title"></span>
            </span>
262
263
            <table class="tab_settings">
              <tr>
264
                <td>1</td><td>
265
266
                  <select id="midi_in1" onchange="dev_change(this);"></select>
                </td>
267
                <td align="right">6</td><td>
268
269
270
271
                  <select id="midi_in6" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
272
                <td>2</td><td>
273
274
                  <select id="midi_in2" onchange="dev_change(this);"></select>
                </td>
275
                <td align="right">7</td><td>
276
277
278
279
                  <select id="midi_in7" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
280
                <td>3</td><td>
281
282
                  <select id="midi_in3" onchange="dev_change(this);"></select>
                </td>
283
                <td align="right">8</td><td>
284
285
286
287
                  <select id="midi_in8" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
288
                <td>4</td><td>
289
290
                  <select id="midi_in4" onchange="dev_change(this);"></select>
                </td>
291
                <td align="right">9</td><td>
292
293
294
295
                  <select id="midi_in9" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
296
                <td>5</td><td>
297
298
                  <select id="midi_in5" onchange="dev_change(this);"></select>
                </td>
299
                <td align="right">10</td><td>
300
301
302
303
                  <select id="midi_in10" onchange="dev_change(this);"></select>
                </td>
              </tr>
            </table>
304
            <br/>
305
306
307
            <span data-i18n="[title]prefs.midi.output_title_tt">
              <span data-i18n="prefs.midi.output_title"></span>
            </span>
308
309
            <table class="tab_settings">
              <tr>
310
                <td>1</td><td>
311
312
                  <select id="midi_out1" onchange="dev_change(this);"></select>
                </td>
313
                <td align="right">6</td><td>
314
315
316
317
                  <select id="midi_out6" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
318
                <td>2</td><td>
319
320
                  <select id="midi_out2" onchange="dev_change(this);"></select>
                </td>
321
                <td align="right">7</td><td>
322
323
324
325
                  <select id="midi_out7" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
326
                <td>3</td><td>
327
328
                  <select id="midi_out3" onchange="dev_change(this);"></select>
                </td>
329
                <td align="right">8</td><td>
330
331
332
333
                  <select id="midi_out8" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
334
                <td>4</td><td>
335
336
                  <select id="midi_out4" onchange="dev_change(this);"></select>
                </td>
337
                <td align="right">9</td><td>
338
339
340
341
                  <select id="midi_out9" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
342
                <td>5</td><td>
343
344
                  <select id="midi_out5" onchange="dev_change(this);"></select>
                </td>
345
                <td align="right">10</td><td>
346
347
348
349
                  <select id="midi_out10" onchange="dev_change(this);"></select>
                </td>
              </tr>
            </table>
350
          </div>
351
        </div>
352

353
        <div class="tab3">
354
          <div class="tab_settings">
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
            <label data-i18n="[title]prefs.gui.presets.gui_preset_tt">
              <span data-i18n="prefs.gui.presets.gui_preset"></span>
            </label>
            <select id="gui_preset" onchange="gui_preset_change(this);">
              <option data-i18n="prefs.gui.presets.default" value="default">
              </option>
              <option data-i18n="prefs.gui.presets.inverted" value="inverted">
              </option>
              <option data-i18n="prefs.gui.presets.vanilla" value="vanilla">
              </option>
              <option data-i18n="prefs.gui.presets.vanilla_inverted" value="vanilla_inverted">
              </option>
              <option data-i18n="prefs.gui.presets.extended" value="extended">
              </option>
              <option data-i18n="prefs.gui.presets.c64" value="c64">
              </option>
              <option data-i18n="prefs.gui.presets.subdued" value="subdued">
              </option>
              <option data-i18n="prefs.gui.presets.strongbad" value="strongbad">
Jonathan Wilkes's avatar
Jonathan Wilkes committed
374
375
              </option>
              <option data-i18n="prefs.gui.presets.solarized" value="solarized">
376
377
378
              </option>
              <option data-i18n="prefs.gui.presets.solarized_inverted"
                      value="solarized_inverted">
379
              </option>
380
381
              <option data-i18n="prefs.gui.presets.footgun" value="footgun">
              </option>
382
            </select>
383
384
385
386
387
            <br/>
            <label data-i18n="[title]prefs.gui.zoom.save_zoom_tt">
              <input type="checkbox" id="save_zoom" name="save_zoom">
              <span data-i18n="prefs.gui.zoom.save_zoom"></span>
            </label>
388
389
            <br/><br/>
            <span data-i18n="prefs.gui.browser.browser_title"></span>
390
391
392
393
394
395
396
397
398
399
            <br/>
            <label data-i18n="[title]prefs.gui.browser.browser_doc_tt">
              <input type="checkbox" id="browser_doc" name="browser_doc">
              <span data-i18n="prefs.gui.browser.browser_doc"></span>
            </label>
            <br/>
            <label data-i18n="[title]prefs.gui.browser.browser_path_tt">
              <input type="checkbox" id="browser_path" name="browser_path">
              <span data-i18n="prefs.gui.browser.browser_path"></span>
            </label>
400
401
402
403
404
            <br/>
            <label data-i18n="[title]prefs.gui.browser.browser_init_tt">
              <input type="checkbox" id="browser_init" name="browser_init">
              <span data-i18n="prefs.gui.browser.browser_init"></span>
            </label>
405
          </div>
406
        </div>
407
408
409
410
411
412

        <div class="tab4">
          <div class="tab_settings">
            <label data-i18n="[title]prefs.startup.paths_tt">
              <span data-i18n="prefs.startup.paths"></span>
            </label>
413
            <div style="height:24vh; width:89vw; background:white; border: 1px solid #bbb; overflow-y:auto; overflow-x:auto; padding:0px;">
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
              <table id="startup_paths" style="width:100%; background:white;">
              </table>
            </div>
            <div class="submit_buttons">
              <button type="button" onClick="startup_path_new()" data-i18n="[title]prefs.startup.new_tt">
                <span data-i18n="prefs.startup.new"></span>
              </button>
              <button type="button" id="startup_path_edit" onClick="startup_path_edit()" data-i18n="[title]prefs.startup.edit_tt">
                <span data-i18n="prefs.startup.edit"></span>
              </button>
              <button type="button" id="startup_path_delete" onClick="startup_path_delete()" data-i18n="[title]prefs.startup.del_tt">
                <span data-i18n="prefs.startup.del"></span>
              </button>
            </div>
            <label data-i18n="[title]prefs.startup.libs_tt">
              <span data-i18n="prefs.startup.libs"></span>
            </label>
431
            <div style="height:24vh; width:89vw; background:white; border: 1px solid #bbb; overflow-y:auto; overflow-x:auto; padding:0px;">
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
              <table id="startup_libs" style="width:100%; background:white;">
              </table>
            </div>
            <div class="submit_buttons">
              <button type="button" onClick="startup_lib_new()" data-i18n="[title]prefs.startup.new_tt">
                <span data-i18n="prefs.startup.new"></span>
              </button>
              <button type="button" id="startup_lib_edit" onClick="startup_lib_edit()" data-i18n="[title]prefs.startup.edit_tt">
                <span data-i18n="prefs.startup.edit"></span>
              </button>
              <button type="button" id="startup_lib_delete" onClick="startup_lib_delete()" data-i18n="[title]prefs.startup.del_tt">
                <span data-i18n="prefs.startup.del"></span>
              </button>
            </div>
            <label data-i18n="[title]prefs.startup.flags_tt">
              <span data-i18n="prefs.startup.flags"></span>
448
              <br/>
449
450
              <input type="text"
                     id="startup_flags"
451
452
                     name="startup_flags"
                     style="width:89vw">
453
454
455
            </label>
          </div>
        </div>
456
      </div>
457

458
459
460
461
      <div class="submit_buttons prefs_buttons">
        <button type="button" onClick="ok()" data-i18n="[title]prefs.ok_tt">
          <span data-i18n="prefs.ok"></span>
        </button>
462
        <button type="button" onClick="apply(false)" data-i18n="[title]prefs.apply_tt">
463
464
465
466
467
        <span data-i18n="prefs.apply"></span>
        </button>
        <button type="button" onClick="cancel()" data-i18n="[title]prefs.close_tt">
        <span data-i18n="prefs.close"></span>
        </button>
468
      </div>
469
    </div>
pokergaming's avatar
pokergaming committed
470
  <script>
471
"use strict";
472
var gui = require("nw.gui");
473
var pdgui = require("./pdgui.js");
pokergaming's avatar
pokergaming committed
474

475
476
// For translations
var l = pdgui.get_local_string;
pokergaming's avatar
pokergaming committed
477

478
// Gui presets
479
pdgui.skin.apply(window);
pokergaming's avatar
pokergaming committed
480

481
var pd_object_callback;
pokergaming's avatar
pokergaming committed
482

483
484
var pd_audio_attrs;
var pd_midi_attrs;
pokergaming's avatar
pokergaming committed
485

486
function ok() {
487
    apply(true);
488
489
    cancel();
}
pokergaming's avatar
pokergaming committed
490

491
492
493
function gui_preset_change(elem) {
    pdgui.skin.set(elem.value);
}
pokergaming's avatar
pokergaming committed
494

495
496
497
498
function get_gui_preset() {
    return document.getElementById("gui_preset").selectedOptions[0].value;
}

499
500
function get_bool_elem(elem) {
    return +document.getElementById(elem).checked;
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// startup config data

// XXXTODO: We should maybe include these in the dialog, but the startup tab
// is already crowded enough as it is, so currently we just record them in
// global variables and then resubmit them unchanged. Note that all of these
// options are rarely used by the average user, and they can also be entered
// using the startup flags field if needed.
var startup_use_stdpath = 1;
var startup_verbose = 0;
var startup_defeat_rt = 0;

function get_startup_flags() {
    return pdgui.encode_for_dialog(document.getElementById("startup_flags").value);
}

// Kludge alert: The user may create invalid empty entries in the paths and
// libs tables during editing. The easiest way to deal with these would be to
// just ignore them, but the table editor currently won't let us do that, so
// instead we flag them using the special text "N/A" in red. These invalid
// entries can be checked for with the valid_startup_item function below and
// are filtered out when submitting the data to Pd. If anyone has a better
// idea to deal with this corner case then please be my guest. -ag

function valid_startup_item(item)
{
    return item.textContent != "N/A" || item.style.color != "red";
}

function finalize_startup_entry(item)
{
    if (!item.textContent) {
        // flag empty entry as invalid
        item.textContent = "N/A";
        item.style.color = "red";
    }
}

function prepare_startup_entry(item)
{
    if (!valid_startup_item(item)) {
        // prepare an invalid entry for editing by turning it back into an
        // empty string
        item.textContent = "";
        item.style.color = "black";
    }
}

// retrieve the path and lib lists from the corresponding tables in the dialog
// and encode them in a form which can be safely transmitted via FUDI

function get_path_array() {
    var table = document.getElementById("startup_paths");
    var arr = [];
    if (table != null) {
        for (var i = 0; i < table.rows.length; i++) {
            var cell = table.rows[i].cells[0];
            // filter out empty items
            if (valid_startup_item(cell)) {
                arr.push(pdgui.encode_for_dialog(cell.textContent));
            }
        }
    }
    return arr;
}

function get_lib_array() {
    var table = document.getElementById("startup_libs");
    var arr = [];
    if (table != null) {
        for (var i = 0; i < table.rows.length; i++) {
            var cell = table.rows[i].cells[0];
            // filter out empty items
            if (valid_startup_item(cell)) {
                arr.push(pdgui.encode_for_dialog(cell.textContent));
            }
        }
    }
    return arr;
}

583
584
585
586
587
588
// callbacks for devices and/or their number of channels
function dev_change(elem) {
    var attrs, id, direction, index;
    // the same logic works for both channels and
    // devices-- we use the variable 'type' to
    // choose the parameter acoordingly
589
    var type, midi;
590
    id = elem.id;
591
592
    midi = id.slice(0, 4) === "midi" ? true : false;
    direction = id.replace("midi_", "").slice(0,2) === "in" ? "in" : "out";
593
    type = id.indexOf("chans") !== -1 ? "chans" : "devs";
594
595
596
    // This would need to change if there could ever be more than
    // 9 devices
    index = +(id.slice(-1)) - 1;
597
598
    attrs = get_attr("pd-" + direction + type,
        midi ?  pd_midi_attrs : pd_audio_attrs);
599
    attrs[index] = elem.value;
600
601
602

    disable_unused_chans("in");
    disable_unused_chans("out");
603
}
604

605
606
607
608
609
610
function attr_change(elem) {
    var attr, id;
    attr = pd_audio_attrs;
    id = elem.id;
    attr[attr.indexOf(id) + 1] = elem.value;
}
pokergaming's avatar
pokergaming committed
611

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
function alsa_ports_change(elem, type) {
    var attr, dummy, i, len;
    attr = pd_midi_attrs;
    dummy = [];
    len = Number(elem.value);
    if (len.isNan) {
        len = 0;
    }
    for (i = 0; i < len; i++) {
        dummy.push("dummy");
    }
    attr[attr.indexOf("midi-" + type + "dev-names") + 1] = dummy;
}

function alsa_in_ports_change(elem) {
    alsa_ports_change(elem, "in");
}

function alsa_out_ports_change(elem) {
    alsa_ports_change(elem, "out");
}

634
635
function get_input(name) {
    var val = document.getElementsByName(name)[0].value;
636
    return val === 0 ? "0" : val;
637
}
pokergaming's avatar
pokergaming committed
638

639
640
641
642
// get a value from the garray attr array
function get_array_value(name, attrs) {
    return attrs[attrs.indexOf(name) + 1];
}
pokergaming's avatar
pokergaming committed
643

644
645
646
// If dev is -1, just change it to 0. Not sure if the Pd audio
// backend requires this, but it's how it works currently.
function kludge_dev(type, attrs, index) {
647
    var dev = get_attr("pd-" + type + "devs", attrs)[index];
648
649
650
    if (dev < 0) { dev = 0; }
    return dev;
}
pokergaming's avatar
pokergaming committed
651

652
// If the device is -1 ('None'), make the number of channels negative.
653
// This is the way Pd's audio backend turns off the device. (It works
654
655
656
// this way so you can remember your number of channels even if
// you turn off the device.)
function kludge_chans(type, attrs, index) {
657
658
    var dev = get_attr("pd-" + type + "devs", attrs)[index],
        chans = get_attr("pd-" + type + "chans", attrs)[index];
659
660
661
    if (dev < 0 && chans >= 0) { chans *= -1; }
    return chans;
}
pokergaming's avatar
pokergaming committed
662

663
function apply(save_prefs) {
664
665
    var attrs = pd_audio_attrs,
        midi_use_alsa;
666
667

    // Audio dialog
668
    pdgui.pdsend(
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
        "pd audio-dialog",
        kludge_dev("in", attrs, 0),
        kludge_dev("in", attrs, 1),
        kludge_dev("in", attrs, 2),
        kludge_dev("in", attrs, 3),
        kludge_chans("in", attrs, 0),
        kludge_chans("in", attrs, 1),
        kludge_chans("in", attrs, 2),
        kludge_chans("in", attrs, 3),
        kludge_dev("out", attrs, 0),
        kludge_dev("out", attrs, 1),
        kludge_dev("out", attrs, 2),
        kludge_dev("out", attrs, 3),
        kludge_chans("out", attrs, 0),
        kludge_chans("out", attrs, 1),
        kludge_chans("out", attrs, 2),
        kludge_chans("out", attrs, 3),
        get_attr("rate", attrs),
        get_attr("advance", attrs),
        get_attr("cancallback", attrs),
        get_attr("blocksize", attrs)
690
    );
pokergaming's avatar
pokergaming committed
691

692
    attrs = pd_midi_attrs;
693
    midi_use_alsa = !!get_attr("use_alsa", attrs);
694
    // Midi dialog
695
    pdgui.pdsend(
696
697
698
699
700
        "pd midi-dialog",
        get_attr("pd-indevs", attrs)[0],
        get_attr("pd-indevs", attrs)[1],
        get_attr("pd-indevs", attrs)[2],
        get_attr("pd-indevs", attrs)[3],
701
702
703
704
705
706
        get_attr("pd-indevs", attrs)[4],
        get_attr("pd-indevs", attrs)[5],
        get_attr("pd-indevs", attrs)[6],
        get_attr("pd-indevs", attrs)[7],
        get_attr("pd-indevs", attrs)[8],
        get_attr("pd-indevs", attrs)[9],
707
708
709
710
        get_attr("pd-outdevs", attrs)[0],
        get_attr("pd-outdevs", attrs)[1],
        get_attr("pd-outdevs", attrs)[2],
        get_attr("pd-outdevs", attrs)[3],
711
712
713
714
715
716
        get_attr("pd-outdevs", attrs)[4],
        get_attr("pd-outdevs", attrs)[5],
        get_attr("pd-outdevs", attrs)[6],
        get_attr("pd-outdevs", attrs)[7],
        get_attr("pd-outdevs", attrs)[8],
        get_attr("pd-outdevs", attrs)[9],
717
718
        midi_use_alsa ? get_attr("midi-indev-names", attrs).length : 0,
        midi_use_alsa ? get_attr("midi-outdev-names", attrs).length : 0
719
    );
720

721
722
723
724
    // Send the gui prefs (currently just the name of the gui preset, the
    // status of the save-zoom toggle and various options related to the help
    // browser) to Pd
    pdgui.pdsend("pd gui-prefs", get_gui_preset(), get_bool_elem("save_zoom"), get_bool_elem("browser_doc"), get_bool_elem("browser_path"), get_bool_elem("browser_init"));
725

726
727
728
729
    // Send the startup config data to Pd
    pdgui.pdsend.apply(null, ["pd path-dialog", startup_use_stdpath, startup_verbose].concat(get_path_array()));
    pdgui.pdsend.apply(null, ["pd startup-dialog", startup_defeat_rt, get_startup_flags()].concat(get_lib_array()));

730
731
732
733
    if (save_prefs) {
        // save the prefs in Pd...
        pdgui.pdsend("pd save-preferences");
    }
734
}
pokergaming's avatar
pokergaming committed
735

736
737
function cancel() {
    var i, attrs, gfxstub;
738
739
740
741
742
743
    // There seems to be a bug in nwjs 0.13 beta3 that doesn't call the
    // "close" event below. This has the symptom of making it impossible
    // to click the "Ok" button and open the preferences dialog again after
    // that. To see if the bug is gone, try removing the next line and see
    // if you can open the Prefs dialog after clicking "Ok".
    pdgui.remove_dialogwin(pd_object_callback);
744
    window.close(true);
745
    //pdgui.pdsend(pd_object_callback, "cancel");
746
}
747

748
function change_api(elem) {
749
    var id = elem.getAttribute("id"),
750
        value = elem.value; // need js object property, DOM Attribute won't work
751
    if (id === "audio_api") {
752
        pdgui.pdsend("pd audio-setapi", value);
753
    } else {
754
        pdgui.pdsend("pd midi-setapi", value);
755
    }
756
}
757

758
759
760
761
762
763
764
function get_attr(name, attrs) {
    return attrs[attrs.indexOf(name) + 1];
}

function populate_apis(elem, apis, current_api) {
    var i, opt, api_select = elem;
    for (i = 0; i < apis.length; i += 2) {
765
        opt = document.createElement("option");
766
        opt.textContent = apis[i];
767
        opt.setAttribute("value", apis[i+1]);
768
        api_select.appendChild(opt);
pokergaming's avatar
pokergaming committed
769
    }
770
771
    api_select.value = current_api;
}
pokergaming's avatar
pokergaming committed
772

773
function populate_devs(type, attrs) {
774
    var devs = get_attr(type === "in" ? "sys-indevs" : "sys-outdevs", attrs);
775
    var i, j, opt, elem, chan_elem, chans;
776
    chans = get_attr("pd-" + type + "chans", attrs);
777
778
    for (i = 0; i < 4; i++) {
        elem = document.getElementById(type + (i+1));
779
        chan_elem = document.getElementById(type + "chans" + (i+1));
780
        chan_elem.value = chans[i];
pokergaming's avatar
pokergaming committed
781
        // if the user changed the API, we need to remove the old devs
782
783
        while (elem.firstChild) {
            elem.removeChild(elem.firstChild);
784
        }
pokergaming's avatar
pokergaming committed
785
        // make a dummy device named 'None' with value -1
786
        opt = document.createElement("option");
pokergaming's avatar
pokergaming committed
787
        opt.value = -1;
788
        opt.textContent = "None";
pokergaming's avatar
pokergaming committed
789
        elem.appendChild(opt);
790
        for (j = 0; j < devs.length; j++) {
791
            opt = document.createElement("option");
792
            opt.value = j;
793
794
795
796
            opt.textContent = devs[j];
            elem.appendChild(opt);
        }
    }
797
798
}

799
function populate_midi_devs(type, attrs) {
800
801
802
    var dev_names = get_attr(type === "in" ?
        "midi-indev-names" : "midi-outdev-names", attrs);
    var i, j, opt, elem;
803
    for (i = 0; i < 10; i++) {
804
        elem = document.getElementById("midi_" + type + (i+1));
805
806
807
808
        // if the user changed the API, we need to remove the old devs
        while (elem.firstChild) {
            elem.removeChild(elem.firstChild);
        }
809
        // make a dummy device named 'None' with value 0
810
        opt = document.createElement("option");
811
        opt.value = 0;
812
        opt.textContent = "None";
813
        elem.appendChild(opt);
814
        for (j = 0; j < dev_names.length; j++) {
815
            opt = document.createElement("option");
816
            opt.value = j+1;
817
            opt.textContent = dev_names[j];
818
819
820
821
822
            elem.appendChild(opt);
        }
    }
}

823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
function disable_unused_chans(type) {
    var i, chan_elem;
    for (i = 0; i < 4; i++) {
        chan_elem = document.getElementById(type + "chans" + (i+1));
        if (+document.getElementById(type + (i+1)).value === -1) {
            chan_elem.disabled = true;
        } else {
            chan_elem.disabled = false;
        }
    }
}

function disable_unused_devs(type, canmulti) {
    var i;
    if (+canmulti < 2) {
        for (i = 0; i < 3; i++) {
            document.getElementById(type + (i+2)).disabled = true;
        }
    } else {
        for (i = 0; i < 4; i++) {
            document.getElementById(type + (i+1)).disabled = false;
        }
    }
}

848
849
function audio_prefs_callback(attrs) {
    pd_audio_attrs = attrs;
850
    var api_select = document.getElementById("audio_api");
851
852
853
854
855
    var callback, i, j, elem, devs, opt;
    // We can get this callback multiple times while the dialog
    // is open.  This is because an API change requires a query
    // for new properties. So we only populate the api options
    // if they don't already exist
856
    if (api_select.getElementsByTagName("option").length < 1) {
857
858
        populate_apis(
            api_select,
859
860
            attrs[attrs.indexOf("audio-apis") + 1],
            attrs[attrs.indexOf("current-api") + 1]);
861
    }
862

863
864
865
    document.getElementById("rate").value = get_attr("rate", attrs);
    document.getElementById("blocksize").value = get_attr("blocksize", attrs);
    document.getElementById("advance").value = get_attr("advance", attrs);
866

867
868
    callback = get_attr("cancallback", attrs);
    //show the checkbox if the API allows
869
    if (callback !== -1) {
870
871
872
        document.getElementById("callback_container")
            .classList.remove("hidden");
        document.getElementById("callback").checked = !!callback;
873
    }
874

875
876
    populate_devs("in", attrs);
    populate_devs("out", attrs);
877

878
879
    set_devs("in", get_attr("pd-indevs", attrs));
    set_devs("out", get_attr("pd-outdevs", attrs));
pokergaming's avatar
pokergaming committed
880

881
882
    // Set chans after the devs (because they may
    // have the side-effect of setting the dev to 'None')
883
884
    set_chans("in", get_attr("pd-inchans", attrs));
    set_chans("out", get_attr("pd-outchans", attrs));
885

886
887
888
889
890
891
    disable_unused_chans("in");
    disable_unused_chans("out");

    disable_unused_devs("in", get_attr("canmulti", attrs));
    disable_unused_devs("out", get_attr("canmulti", attrs));

892
    pdgui.resize_window(pd_object_callback);
893
}
pokergaming's avatar
pokergaming committed
894

895
896
897
898
function set_devs(type, devs) {
    var i;
    for (i = 0; i < devs.length; i++) {
        document.getElementById(type + (i+1)).value = devs[i];
pokergaming's avatar
pokergaming committed
899
    }
900
}
pokergaming's avatar
pokergaming committed
901

902
903
904
function set_chans(type, chans_array) {
    var i;
    for (i = 0; i < chans_array.length; i++) {
905
        document.getElementById(type + "chans" + (i+1)).value = chans_array[i];
906
907
908
        // If no chans, set device to -1 (None)
        if (chans_array[i] < 1) {
            document.getElementById(type + (i+1)).value = -1;
pokergaming's avatar
pokergaming committed
909
        }
pokergaming's avatar
pokergaming committed
910
    }
911
}
pokergaming's avatar
pokergaming committed
912

913
914
function midi_prefs_callback(attrs) {
    pd_midi_attrs = attrs;
915
    var api_select = document.getElementById("midi_api");
916

917
    if (api_select.getElementsByTagName("option").length < 1) {
918
919
        populate_apis(
            api_select,
920
921
            get_attr("midi-apis", attrs),
            get_attr("current-api", attrs)
922
923
        );
    }
924
925
926
927
928
929
930
931
932
    // If we still haven't gotten any apis, just assume that the
    // only option is a generic "system" midi and hide the select widget.
    // This should really be taken care of in Pd-- I don't know why, for
    // example, it doesn't just send "MMIO" on Windows when that's the
    // only API available...
    if (api_select.getElementsByTagName("option").length < 1) {
        document.getElementsByClassName("midi_api_container")[0].
            style.setProperty("display", "none");
    }
933
934
935
936
937
938
939
940
941
    // Hide devs and only show the alsa in/out boxes. In the future
    // we need an additional user friendly alsa interface here-- one that
    // just lets the user pick a single input device and single output
    // device, like LMMS does.
    if (+get_attr("current-api", attrs) === 1) { // ALSA API
        document.getElementsByClassName("midi_devices")[0]
            .style.setProperty("display", "none");
        document.getElementsByClassName("alsa_midi")[0]
            .style.setProperty("display", "inherit");
942
943
944
945
946
        // For ALSA, we just set the number of ports we want
        // for input and output. The tcl/tk interface just reused
        // the first of the "indevs" for this value, so (for now)
        // we repeat that mistake here...
        document.getElementById("alsa_in_ports").value =
947
            get_attr("midi-indev-names", attrs).length;
948
        document.getElementById("alsa_out_ports").value =
949
            get_attr("midi-outdev-names", attrs).length;
950
951
952
953
954
955
956
    } else {
        document.getElementsByClassName("midi_devices")[0]
            .style.setProperty("display", "inherit");
        document.getElementsByClassName("alsa_midi")[0]
            .style.setProperty("display", "none");
        populate_midi_devs("in", attrs);
        populate_midi_devs("out", attrs);
957
958
        set_devs("midi_in", get_attr("pd-indevs", attrs));
        set_devs("midi_out", get_attr("pd-outdevs", attrs));
959
    }
960
    pdgui.resize_window(pd_object_callback);
961
}
962

963
function gui_prefs_callback(name, save_zoom, browser_doc, browser_path, browser_init) {
964
965
966
967
968
969
970
971
    var s = document.getElementById("gui_preset"),
        i;
    for (i = 0; i < s.options.length; i++) {
        if (s.options[i].value.toLowerCase() === name.toLowerCase()) {
            s.selectedIndex = i;
            break;
        }
    }
972
    document.getElementById("save_zoom").checked = !!save_zoom;
973
974
    document.getElementById("browser_doc").checked = !!browser_doc;
    document.getElementById("browser_path").checked = !!browser_path;
975
    document.getElementById("browser_init").checked = !!browser_init;
976
}
977

978
979
// startup settings

980
function path_prefs_callback(use_stdpath, verbose, path_array) {
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
    //pdgui.post("path callback: " + path_array.join("\n"));
    // XXXTODO
    startup_use_stdpath = use_stdpath;
    startup_verbose = verbose;
    var table = document.getElementById("startup_paths");
    if (table != null) {
        // Add the path_array elements to the table.
        for (var i = 0; i < path_array.length; i++) {
            var row = table.insertRow(i);
            var cell = row.insertCell(0);
            cell.textContent = path_array[i];
            // install the requisite handlers
            startup_paths_handlers(cell);
        }
    }
    selected_startup_path = -1;
    update_path_buttons();
998
999
1000
}

function lib_prefs_callback(defeat_rt, flag_string, lib_array) {
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
    //pdgui.post("lib callback: " + lib_array.join("\n"));
    // XXXTODO
    startup_defeat_rt = defeat_rt;
    document.getElementById("startup_flags").value = flag_string;
    var table = document.getElementById("startup_libs");
    if (table != null) {
        // Add the lib_array elements to the table.
        for (var i = 0; i < lib_array.length; i++) {
            var row = table.insertRow(i);
            var cell = row.insertCell(0);
            cell.textContent = lib_array[i];
            // install the requisite handlers
            startup_libs_handlers(cell);
        }
    }
    selected_startup_lib = -1;
    update_lib_buttons();
}

var selected_startup_lib = -1;
var selected_startup_path = -1;

function current_startup_lib()
{
    var table = document.getElementById("startup_libs");
    return table.rows[selected_startup_lib].cells[0];
}

function current_startup_path()
{
    var table = document.getElementById("startup_paths");
    return table.rows[selected_startup_path].cells[0];
}

function update_lib_buttons()
{
    var val = selected_startup_lib < 0;
    var edit_button = document.getElementById("startup_lib_edit");
    var del_button = document.getElementById("startup_lib_delete");
    edit_button.disabled = val;
    del_button.disabled = val;
}

function update_path_buttons()
{
    var val = selected_startup_path < 0;
    var edit_button = document.getElementById("startup_path_edit");
    var del_button = document.getElementById("startup_path_delete");
    edit_button.disabled = val;
    del_button.disabled = val;
}

function startup_libs_handlers(item)
{
    item.style = "white-space: nowrap";
    item.onclick = function () {
        startup_lib_click(this);
    };
    item.onblur = function () {
        startup_lib_blur(this);
    };
}

function startup_paths_handlers(item)
{
    item.style = "white-space: nowrap";
    item.onclick = function () {
        startup_path_click(this);
    };
    item.onblur = function () {
        startup_path_blur(this);
    };
}

function startup_lib_click(item) {
    var i = item.parentElement.rowIndex;
    if (i != selected_startup_lib) {
        if (selected_startup_lib >= 0) {
            current_startup_lib().style.background = "white";
            current_startup_lib().setAttribute("contenteditable", "false");
        }
        item.style.background = "#ddd";
        selected_startup_lib = i;
    } else if (item.getAttribute("contenteditable") != "true") {
        item.style.background = "white";
        item.contenteditable = "false";
        selected_startup_lib = -1;
    }
    update_lib_buttons();
}

function startup_lib_new() {
    var table = document.getElementById("startup_libs");
    var row = table.insertRow(selected_startup_lib+1);
    var cell = row.insertCell(0);
    startup_libs_handlers(cell);
    startup_lib_click(cell);
    startup_lib_edit();
}

function startup_lib_edit() {
    if (selected_startup_lib >= 0) {
        var item = current_startup_lib();
        prepare_startup_entry(item);
        item.setAttribute("contenteditable", "true");
        item.focus();
    }
}

function startup_lib_blur(item) {
    // Kludge alert: Apparently we can't just remove an empty row
    // here, so we simply flag the entry as invalid instead.
    finalize_startup_entry(item);
    item.setAttribute("contenteditable", "false");
}

function startup_lib_delete() {
    if (selected_startup_lib >= 0) {
        var table = document.getElementById("startup_libs");
        table.deleteRow(selected_startup_lib);
        if (selected_startup_lib < table.rows.length) {
            // make sure that the following row is selected, which is
            // convenient when deleting successive items
            var item = current_startup_lib();
            selected_startup_lib = -1;
            startup_lib_click(item);
        } else {
            selected_startup_lib = -1;
            if (table.rows.length > 0)
                startup_lib_click(table.rows[table.rows.length-1].cells[0]);
        }
    }
    update_lib_buttons();
}

function startup_path_click(item) {
    var i = item.parentElement.rowIndex;
    if (i != selected_startup_path) {
        if (selected_startup_path >= 0) {
            current_startup_path().style.background = "white";
            current_startup_path().setAttribute("contenteditable", "false");
        }
        item.style.background = "#ddd";
        selected_startup_path = i;
    } else if (item.getAttribute("contenteditable") != "true") {
        item.style.background = "white";
        item.contenteditable = "false";
        selected_startup_path = -1;
    }
    update_path_buttons();
}

function startup_path_new() {
    var table = document.getElementById("startup_paths");
    var row = table.insertRow(selected_startup_path+1);
    var cell = row.insertCell(0);
    startup_paths_handlers(cell);
    startup_path_click(cell);
    startup_path_edit();
}

function startup_path_edit() {
    if (selected_startup_path >= 0) {
        var item = current_startup_path();
        prepare_startup_entry(item);
        item.setAttribute("contenteditable", "true");
        item.focus();
    }
}

function startup_path_blur(item) {
    // Kludge alert: Apparently we can't just remove an empty row
    // here, so we simply flag the entry as invalid instead.
    finalize_startup_entry(item);
    item.setAttribute("contenteditable", "false");
}

function startup_path_delete() {
    if (selected_startup_path >= 0) {
        var table = document.getElementById("startup_paths");
        table.deleteRow(selected_startup_path);
        if (selected_startup_path < table.rows.length) {
            // make sure that the following row is selected, which is
            // convenient when deleting successive items
            var item = current_startup_path();
            selected_startup_path = -1;
            startup_path_click(item);
        } else {
            selected_startup_path = -1;
            if (table.rows.length > 0)
                startup_path_click(table.rows[table.rows.length-1].cells[0]);
        }
    }
    update_path_buttons();
1195
1196
}

1197
1198
1199
// This gets called from the nw_create_window function in index.html
// It provides us with our window id from the C side.  Once we have it
// we can create the menu and register event callbacks
1200
function register_window_id(gfxstub, attr_arrays) {
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
    pd_object_callback = gfxstub;
    add_events(gfxstub);
    translate_form();

    // We don't turn on rendering of the "container" div until
    // We've finished displaying all the spans and populating the
    // labels and form elements.  That makes it more efficient and
    // snappier, at least on older machines.  However, we still have
    // to asynchronously request the form values from Pd for audio
    // and MIDI...

    pdgui.pdsend("pd audio-properties"); // request audio pref attrs
    pdgui.pdsend("pd midi-properties");  // request midi pref attrs
1214
    pdgui.pdsend("pd gui-properties");   // request saved gui properties
1215
1216
    pdgui.pdsend("pd start-path-dialog"); // request default system paths
    pdgui.pdsend("pd start-startup-dialog"); // request default loaded libs
1217
    document.getElementsByClassName("container")[0].style.setProperty("display", "inline");
1218
}
pokergaming's avatar
pokergaming committed
1219
1220

function tr_text(id) {
1221
1222
    var elem = document.getElementById("iem.prop." + id);
    elem.textContent = l("iem.prop." + id);
pokergaming's avatar
pokergaming committed
1223
1224
1225
1226
}

// Stop-gap translator
function translate_form() {
1227
1228
1229
    var elements = document.querySelectorAll("[data-i18n]"),
        data,
        i;
pokergaming's avatar
pokergaming committed
1230
    for (i = 0; i < elements.length; i++) {
1231
        data = elements[i].dataset.i18n;
1232
        if (data.slice(0,7) === "[title]") {
pokergaming's avatar
pokergaming committed
1233
1234
1235
1236
1237
1238
1239
1240
1241
            elements[i].title = l(data.slice(7));
        } else {
            elements[i].textContent = l(data);
        }
    }
}

function populate_form(attr_array) {
    // First, let's put the translated text for the form labels:
1242
1243
    var i;
    for(i = 0; i < attr_array.length; i+=2) {
pokergaming's avatar
pokergaming committed
1244
1245
1246
1247
        // Unhide the span with the class with the same name as the id
        var prop_group = document.getElementsByClassName(attr_array[i])[0];
        if (prop_group !== undefined) {
            console.log("the thing here is " + attr_array[i]);
1248
            prop_group.classList.remove("hidden");
pokergaming's avatar
pokergaming committed
1249
        } else {
1250
            pdgui.post("Error: couldn't find iemgui prop group for " + attr_array[i]);
pokergaming's avatar
pokergaming committed
1251
1252
1253
1254
        }

        var elem = document.getElementsByName(attr_array[i]);
        if (elem.length > 0) {
1255
            if (elem[0].type === "checkbox") {
pokergaming's avatar
pokergaming committed
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
                // The attr here is a string, so we need to
                // force it to number, hence the "+" below
                elem[0].checked = +attr_array[i+1];
            } else {