Debian Codegraph @

src/ui/dialogs.d

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL.
3 */
4
5 module ui.dialogs;
6 import derelict.sdl2.sdl;
7 import main;
8 import com.fb;
9 import com.util;
10 import com.session;
11 private import ct.base;
12 import ui.help;
13 import ui.ui;
14 import ui.input;
15 import std.algorithm;
16 import std.string;
17 import std.file;
18 import std.utf;
19 import std.array;
20 import std.stdio;
21 import std.process;
•22 import std.algorithm : sort;
23
24 abstract class QueryDialogBase(T) : Window {
25 string query;
26 ubyte[1] byt;
27 alias void delegate(T) Callback;
28 Callback callback;
29 protected int frameWidth;
30 this(string s, Callback fp) {
31 super(Rectangle(0, 0, 1));
32 query = s;
33 callback = fp;
34 frameWidth = cast(int)query.length;
35 }
36
37 override void update() {
38 int x = cast(int)(screen.width / 2 - (frameWidth + 6)/2);
39 int y = cast(int)(screen.height / 2 - 11);
40 drawFrame(Rectangle(x, y, 5, frameWidth + 9));
41 screen.cprint(x + 4, y + 2, 15, 0, query);
42 input.setCoord(cast(int)(x + 4 + query.length), cast(int)( y + 2));
43 }
44
45 override void activate() {
46 return;
47 }
48 }
49
50 class QueryDialog : QueryDialogBase!int {
51 const int maxValue;
52 this(string s,Callback fp, int m) {
53 super(s, fp);
54 input = new InputBoundedByte(byt);
55 maxValue = m;
56 }
57
58 override int keypress(Keyinfo key) {
59 if(key.mods & KMOD_ALT) return OK;
60 int r = input.keypress(key);
61 if(r == WRAP &&
62 (cast(InputBoundedByte)input).value >= maxValue) {
63 input.setOutput(cast(ubyte[])[maxValue - 1]);
64 return OK;
65 }
66 else if(r == RETURN) {
67 input.nibble = 0;
68 callback(input.toInt());
69 }
70 else if(r == CANCEL) { // no callback
71 }
72 else r = OK;
73 if(input.value >= maxValue)
74 input.setOutput(cast(ubyte[])[maxValue - 1]);
75 return r;
76 }
77 }
78
79 class ConfirmationDialog : QueryDialogBase!int {
80 this(string title, Callback cb, string keys, int defaultkey) {
81 super(title, cb);
82 input = new InputSingleChar(byt, keys, defaultkey);
83 }
84
85 this(string title, Callback cb) {
86 this(title, cb, "yn", 1);
87 }
88
89 override int keypress(Keyinfo key) {
90 if(key.mods & KMOD_ALT) return OK;
91 int r = input.keypress(key);
92 if(r == CANCEL)
93 return CANCEL; // just close dialog
94 if(r == RETURN) { // received legal key
95 callback(input.toInt);
96 return RETURN;
97 }
98 return OK;
99 }
100 }
101
102 class StringDialog : QueryDialogBase!string {
103 int inputLength;
104 this(string query,Callback fp, string inp, int length) {
105 super(query, fp);
106 input = new InputString(inp, length);
107 this.inputLength = length;
108 frameWidth = cast(int)query.length + inputLength;
109 }
110
111 override int keypress(Keyinfo key) {
112 if(key.mods & KMOD_ALT) return OK;
113 int r = input.keypress(key);
114 if(r == CANCEL)
115 return CANCEL; // just close dialog
116 if(r == RETURN) {
117 input.nibble = 0;
118 callback((cast(InputString)input).toString(false));
119 return RETURN;
120 }
121 return OK;
122 }
123 }
124
125 class HelpDialog : Window {
126 enum MAX_LINE_LENGTH = 80;
127 string[][] pages;
128 const string title;
129 int numpages;
130 int page = 1;
131 int txt_x;
132
133 this(Rectangle a, ContextHelp ctx) {
134 super(a);
135 pages.length = ctx.text.length;
136 foreach(i,page; ctx.text) {
137 pages[i] = std.string.splitLines(page);
138 }
139 numpages = cast(int)pages.length; // deprecate
140 title = ctx.title;
141 txt_x = area.x + (area.width / 2 - MAX_LINE_LENGTH / 2);
142 }
143
144 override void update() {
145 int ypos = area.y + 2;
146 drawFrame(area);
147 screen.cprint(area.x + 2, area.y, 1, 0, format(" %s %d/%d (press SPACE for more) ", title,
148 page,numpages));
149
150 screen.cprint(area.x + 1, area.y + 2, 1, 0,
151 std.array.replicate(" ", area.width-3));
152
153 foreach(line; pages[page-1]) {
154 screen.cprint(area.x+1, ypos, 1, 0, std.array.replicate(" ", area.width-3));
155 screen.fprint(txt_x, ypos, "`0f" ~ line);
156 ypos++;
157 }
158 for(; ypos < 36; ypos++) {
159 screen.cprint(area.x+1, ypos, 1, 0, std.array.replicate(" ", area.width-2));
160 }
161
162 }
163
164 override int keypress(Keyinfo key) {
165 int k = key.key;
166 if(k == SDLK_SPACE ||
167 k == SDLK_PLUS ||
168 k == SDLK_RIGHT ||
169 k == SDLK_PAGEDOWN)
170 if(page++ >= numpages) page = 1;
171 if(key.unicode == SDLK_RETURN ||
172 key.unicode == SDLK_ESCAPE) return RETURN;
173 return OK;
174 }
175 }
176
177 class DebugDialog : Window {
178 Sequence seq;
179
180 this(Sequence s) {
181 super(Rectangle(screen.width / 2 - 24,
182 screen.height / 2 - 10,
183 20, 55));
184 seq = s;
185 }
186
187 this(Rectangle a) {
188 super(a);
189 }
190
191 override void update() {
192 assert(seq !is null);
193 ubyte[] data = seq.compact();
194 data.length = 256;
195 int y,pos;
196 string str;
197 drawFrame(area);
198 for(y=0;y<area.h-2;y++) {
199 screen.fprint(area.x+1,area.y+1+y,
200 std.array.replicate(" ", area.width-2));
201 }
202
203 for(y=0; y<16; y++) {
204 str = format("%02X:",y*16);
205 for(int i = 0; i < 16 ; i++) {
206 str ~= format("%02X ", data[pos++]);
207 }
208 screen.fprint(area.x+2,area.y+y+2,"`0f" ~ str);
209 }
210 }
211
212 override int keypress(Keyinfo key) {
213 switch(key.unicode) {
214 case SDLK_SPACE:
215 com.util.hexdump(seq.compact(),16);
216 break;
217 case 0:
218 break;
219 default:
220 return RETURN;
221 }
222 return OK;
223 }
224 }
225
226 class AboutDialog : Window {
227 string LOGO =
228 " ___ ___ ___
229 ______/ /____________________________________\\ \\__\\ \\_______________
230 / ___/ / -__/ -__/__ --/ -__| ___\\ \\ \\ __\\ __\\ -__\\ _\\
231 /_____/__/__/_____/_____/_____/______|______\\_____\\_____\\_____\\_____\\___\\/
232 \\_____\\__\\__\\_____\\_____\\_____\\______|______/_____/_____/_____/_____/___/
233
234 ";
235
236 this(Rectangle a) {
237 super(a);
238 }
239
240 override void update() {
241 string[] logo = std.string.splitLines(LOGO);
242 int y;
243
244 drawFrame(area);
245 y = area.y + 1;
246 foreach(line; logo) {
247
248 screen.cprint(area.x + 1, y, 1, 0,
249 std.array.replicate(" ", area.width-2));
250
251 screen.fprint(area.x + 1, y, "`01" ~ line.center(area.width-2));
252 y++;
253 }
254 screen.cprint(area.x + 1, y++,15, 0,"(C) 2009-17 Abaddon + contributors".center(area.width-2));
255 screen.cprint(area.x + 1, y++,15, 0,"reSID engine by Dag Lem".center(area.width-2));
256 screen.cprint(area.x + 1, y++,15, 0,"Released under GNU GPL".center(area.width-2));
257 screen.fprint(area.x + 1, y++," ".center(area.width-2));
258 }
259
260 override int keypress(Keyinfo key) {
261 if(key.mods) return OK;
262 auto k = key.key;
263 if(k == SDLK_ESCAPE ||
264 k == SDLK_SPACE ||
265 k == SDLK_RETURN) return RETURN;
266 return OK;
267 }
268 }
269
270 class FileSelector : Window {
271 struct FileSelPos {
272 int offset, pos;
273 void reset() { offset = pos = 0; }
274 }
275
276 struct File {
277 string name;
278 int exists, isdir;
279 }
280
281 FileSelPos fpos;
282 private File[] filelist;
283 string directory;
284 alias area filearea;
285
286 this(Rectangle a) {
287 super(a);
288 directory = getcwd();
289 refresh();
290 }
291
292 override void refresh() {
293 if(!exists(directory)) {
294 UI.statusline.display("Directory not found!");
295 }
296 else {
297 chdir(directory);
298 getdir(directory);
299 if(num >= filelist.length)
300 cursorEnd();
301 }
302 }
303
304 void reset() {
305 fpos.offset = fpos.pos = 0;
306 }
307
308 override void update() {
309 int y, i;
310 for(y = area.y, i = 0; i < area.height; y++,i++) {
311 int ofs = fpos.offset + i;
312 string fs = null;
313 int col = 15;
314 if(ofs < filelist.length) {
315 File f = filelist[ofs];
316 auto ind = 1+f.name.lastIndexOf(DIR_SEPARATOR);
317 if( ofs < 2 || (f.exists && f.isdir) ) {
318 col = 13;
319 }
320 fs = fstr(format(" %s", f.name[ind..$].leftJustify(area.width-2)));
321 }
322 screen.cprint(area.x+5,y,col,0,fs);
323 }
324 }
325
326 void blink() {
327 int y = area.y + fpos.pos;
328 auto ind = 1 + filelist[num].name.lastIndexOf(DIR_SEPARATOR);
329 screen.fprint(area.x+5,y,fstr("`b1 " ~ filelist[num].name[ind..$].leftJustify(area.width-3)) ~ " ");
330 }
331
332 int fileHandler() {
333 if(isDir(selected)) {
334 string s;
335 if(selected == ".." ) {
336 int i = cast(int) directory.lastIndexOf(DIR_SEPARATOR);
337 if(i >= 0) {
338 s = directory[0..i];
339 if(s.lastIndexOf(DIR_SEPARATOR) < 0) {
340 s ~= DIR_SEPARATOR;
341 }
342 directory = s;
343 }
344 }
345 else if(selected != ".") {
346 directory = cast(string)(selected.dup);
347 }
348 reset();
349 refresh();
350 return OK;
351 }
352 return RETURN;
353 }
354
355 override int keypress(Keyinfo key) {
356 switch(key.raw)
357 {
358 case SDLK_UP:
359 step(-1);
360 return WRAP;
361 case SDLK_DOWN:
362 step(1);
363 return WRAP;
364 case SDLK_PAGEUP:
365 step(-area.height);
366 return WRAP;
367 case SDLK_PAGEDOWN:
368 step(area.height);
369 return WRAP;
370 case SDLK_HOME:
371 reset();
372 return WRAP;
373 case SDLK_END:
374 cursorEnd();
375 return WRAP;
376 default:
377 break;
378 }
379 return OK;
380 }
381
382 char[][] listdir(string udir) {
383 char[][] ret;
384 auto app = appender(ret);
385 foreach (DirEntry e; dirEntries(udir, SpanMode.shallow)){
386 app.put( e.name.dup );
387 }
388
389 return app.data;
390 }
391
392 void getdir(string udir) {
393 char[][] dir;
394 char[][] dirs, files;
395 dir = listdir(udir);
396 dirs.length = dir.length+2;
397 files.length = dir.length;
398
399 int idxd, idxf;
400
401 foreach(i, d; dir) {
402 char[] first = d[0..1];
403 // skip hidden / temp files
404 if(first == "." || first == "#")
405 continue;
406 try {
407 if(d.isDir()) {
408 dirs[idxd++] = d;
409 } else {
410 files[idxf++] = d;
411 }
412 }
413 catch(FileException) {
414 // may occur if entry "d" does not
415 // exist or is a dangling symlink
416 continue;
417 }
418 }
419
420 dirs.length = idxd;
421 dirs.sort;
422
423 files.length = idxf;
424 files.sort;
425
426 string[] all = cast(string[])(dirs ~ files);
427
428 filelist.length = all.length + 2;
429 filelist[0] = File(".", true, true);
430 filelist[1] = File("..",true, true);
431 for(int i = 0; i < all.length; i++) {
432 filelist[i+2] = File(all[i], all[i].exists(), all[i].isDir());
433 }
434 }
435
436 // move the cursor to last entry & set scroll window pos
437 void cursorEnd() {
438 if(filelist.length >= area.height) {
439 fpos.offset = cast(int)(filelist.length - area.height);
440 fpos.pos = cast(int)(area.height-1);
441 }
442 else {
443 fpos.offset = 0;
444 fpos.pos = cast(int)(filelist.length - 1);
445 }
446 }
447
448 void step(int st) {
449 fpos.pos += st;
450 if(fpos.pos >= filearea.height) {
451 int r = fpos.pos-filearea.height;
452 fpos.offset += r+1;
453 fpos.pos -= r+1;
454 if(num >= filelist.length && filelist.length > filearea.height) {
455 fpos.offset = cast(int)(filelist.length - filearea.height);
456 fpos.pos = cast(int)(filearea.height-1);
457 }
458 }
459 else if(fpos.pos < 0) {
460 int r = -fpos.pos;
461 fpos.offset -= r;
462 if(fpos.offset < 0) fpos.offset=0;
463 fpos.pos += r;
464 }
465 if(num >= filelist.length)
466 cursorEnd();
467 }
468
469 @property string selected() { return filelist[num].name; }
470 alias selected getSelected;
471
472 private:
473
474 @property int num() { return fpos.offset + fpos.pos; }
475 string fstr(string fs) {
476 if(fs.length > (area.width))
477 fs.length = area.width;
478 return fs;
479 }
480 }
481
482 // wraps inputstring
483 class DialogString : Window {
484 this(Rectangle a) {
485 this(a, 50);
486 }
487
488 this(Rectangle a, int len) {
489 input = new InputString("", len);
490 input.setCoord(a.x, a.y);
491 super(a);
492 }
493
494 override string toString() { return toString(false); }
495
496 string toString(bool p) { return (cast(InputString)input).toString(p); }
497
498 void setString(string s) {
499 (cast(InputString)input).setOutput(s);
500 }
501 alias setString setOutputString;
502
503 override void update() {
504 input.update();
505 }
506
507 override int keypress(Keyinfo key) { input.keypress(key); return OK; }
508 }
509
510 class FileSelectorDialog : WindowSwitcher {
511 alias void delegate(string) CB;
512 const CB callback;
513 alias callback processFileCallback;
514 private DialogString sfile, sdir;
515 FileSelector fsel;
516 private string header;
517 private char[][] filelist;
518 Rectangle filearea;
519
520 this(Rectangle a, string h, CB cb) {
521 header = h;
522 if(a == Rectangle.init) {
523 int dialog_width = screen.width - 32;
524 int dialog_height = screen.height - 10;
525 int dialog_x = screen.width / 2 - dialog_width / 2;
526 int dialog_y = screen.height / 2 - dialog_height / 2;
527 a = Rectangle(dialog_x, dialog_y, dialog_height, dialog_width);
528 }
529 filearea = Rectangle(a.x + 5, a.y + 2, a.height - 6, a.width - 10);
530 fsel = new FileSelector(Rectangle(a.x + 5, a.y + 2, a.height - 6,
531 a.width - 18));
532 sfile = new DialogString(Rectangle(a.x+3+11, a.y+a.height-2), 50);
533 sdir = new DialogString(Rectangle(a.x+3+11, a.y+a.height-3), 50);
534 sdir.setString(getcwd());
535 super(a, [cast(Window)fsel, sdir, sfile]);
536 activateWindow(0);
537 callback = cb;
538 }
539
540 void setFilename(string s) {
541 sfile.setString(cast(string)s.dup);
542 }
543
544 void setDirectory(string s) {
545 fsel.directory = cast(string)s.dup;
546 sdir.setString(cast(string)s.dup);
547 }
548
549 @property string filename() {
550 return sfile.toString();
551 }
552
553 @property string fullname() {
554 return getcwd() ~ DIR_SEPARATOR ~ sfile.toString();
555 }
556
557 @property string directory() {
558 return fsel.directory;
559 }
560
561 override void activate() {
562 refresh();
563 fsel.refresh();
564 }
565
566 override void refresh() {
567 update();
568 }
569
570 override void update() {
571 int x,y,i;
572
573 for(y = area.y; y < area.y+area.height; y++) {
574 screen.cprint(area.x, y, 1, 0, std.array.replicate(" ",area.width));
575 }
576 drawFrame(area);
577 x = area.x + 3;
578 y = area.y + 2;
579 screen.cprint(x,area.y,1,0," " ~ header ~ " ");
580
581 screen.fprint(x,area.y+area.height-3,format("`0fDirectory: `0d%s",sdir.toString()));
582
583 string f = sfile.toString();
584 int ind = cast(int) (1+f.lastIndexOf(DIR_SEPARATOR));
585 screen.fprint(x,area.y+area.height-2,format("`0f Filename: `0d%s",f[ind..$]));
586
587 activeWindow.update();
588 if(activeWindow == fsel) {
589 fsel.blink();
590 } else {
591 fsel.update();
592 }
593 input = activeWindow.input;
594 }
595
596 override int keypress(Keyinfo key) {
597 if(key.mods && !key.mods & KMOD_SHIFT) return OK;
598 switch(key.raw)
599 {
600 case SDLK_TAB:
601 return super.keypress(key);
602 case SDLK_ESCAPE:
603 return RETURN;
604 case SDLK_RETURN:
605 return returnPressed(callback);
606 default:
607 int r = activeWindow.keypress(key);
608 if(r == WRAP){
609 int ind = cast(int) (1 + fsel.getSelected().lastIndexOf(DIR_SEPARATOR));
610 sfile.setString(cast(string)(fsel.getSelected()[ind..$]));
611 }
612 break;
613 }
614 return OK;
615 }
616
617 protected int returnPressed(CB cb) {
618 if(activeWindow == fsel) {
619 int r = fsel.fileHandler();
620 if(r == RETURN)
621 cb(cast(string)(fsel.selected));
622 sdir.setString(getcwd());
623 return r;
624 }
625 else if(activeWindow == sfile) { // pressed RETURN in file dialog
626 //string filename = getcwd() ~ DIR_SEPARATOR ~ sfile.toString();
627 cb(fullname);
628 return RETURN;
629 }
630 else {
631 fsel.directory = sdir.toString();
632 fsel.reset();
633 fsel.refresh();
634 }
635 return OK;
636 }
637 }
638
639 class LoadFileDialog : FileSelectorDialog {
640 CB cbimport;
641
642 this(Rectangle a, CB cbload, CB cbimp) {
643 super(a, "Load Song", cbload);
644 cbimport = cbimp;
645 }
646
647 override protected int returnPressed(CB cb) {
648 if(activeWindow != fsel && activeWindow != sfile)
649 return super.returnPressed(cb);
650
651 if(std.file.exists(fullname) && !std.file.isDir(fullname)
652 && shouldUpgrade(fullname)) {
653 mainui.activateDialog(new ConfirmationDialog("Upgrade to latest player (Y/n)? ",
654 &confirmCallback, "yn", 0));
655 return OK;
656 }
657 else return super.returnPressed(cb);
658 }
659
660 private bool shouldUpgrade(string fn) {
661 try {
662 Song newsong = new Song();
663 newsong.open(fn);
664 return SONG_REVISION > newsong.ver;
665 }
666 catch(Exception e) { return false; }
667 return true;
668 }
669
670 private void confirmCallback(int param) {
671 auto cb = param ? callback : cbimport;
672
673 if(activeWindow == fsel) {
674 int r = fsel.fileHandler();
675 if(r == RETURN)
676 cb(cast(string)(fsel.selected));
677 sdir.setString(getcwd());
678 }
679 else if(activeWindow == sfile) { // pressed RETURN in file dialog
680 string filename = getcwd() ~ DIR_SEPARATOR ~ sfile.toString();
681 cb(filename);
682 }
683 }
684 }
685
686 class SaveFileDialog : FileSelectorDialog {
687 this(Rectangle a, CB cb) {
688 super(a, "Save Song", cb);
689 activateWindow(2);
690 }
691
692 override protected int returnPressed(CB cb) {
693 if(activeWindow != fsel && activeWindow != sfile)
694 return super.returnPressed(cb);
695
696 if(std.file.exists(fullname) && !std.file.isDir(fullname)) {
697 mainui.activateDialog(new ConfirmationDialog("Overwrite destination file (y/N)? ",
698 &confirmCallback));
699 return OK;
700 }
701 else return super.returnPressed(cb);
702 }
703
704 private void confirmCallback(int param) {
705 if(param != 0) return;
706
707 // copypasted from super.returnPressed, ugh...
708 // need to implement getSelectedFilename to cut repetition
709
710 if(activeWindow == fsel) {
711 int r = fsel.fileHandler();
712 if(r == RETURN)
713 processFileCallback(cast(string)(fsel.selected));
714 sdir.setString(getcwd());
715 }
716 else if(activeWindow == sfile) { // pressed RETURN in file dialog
717 string filename = getcwd() ~ DIR_SEPARATOR ~ sfile.toString();
718 processFileCallback(filename);
719 }
720 }
721 }