Debian Codegraph @

src/ui/dialogs.d

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