Skip to content

Latest commit

 

History

History
1489 lines (1268 loc) · 43.7 KB

gui_ncurses.c

File metadata and controls

1489 lines (1268 loc) · 43.7 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#if !SUPPORT_GUI_NCURSES
#error Something is wrong in the build system.
#endif
#define BUILDING_EXTERNAL_PLUGIN 1
#include "gui.h"
MOJOGUI_PLUGIN(ncurses)
#if !GUI_STATIC_LINK_NCURSES
CREATE_MOJOGUI_ENTRY_POINT(ncurses)
#endif
#include <unistd.h>
#include <ctype.h>
Feb 28, 2010
Feb 28, 2010
24
25
26
27
// CMake searches for a whole bunch of different possible curses includes
#if defined(HAVE_NCURSESW_NCURSES_H)
#include <ncursesw/ncurses.h>
#elif defined(HAVE_NCURSESW_CURSES_H)
Mar 3, 2008
Mar 3, 2008
28
#include <ncursesw/curses.h>
Feb 28, 2010
Feb 28, 2010
29
30
#elif defined(HAVE_NCURSESW_H)
#include <ncursesw.h>
May 17, 2016
May 17, 2016
31
32
#elif defined(HAVE_CURSES_H)
#include <curses.h>
Feb 28, 2010
Feb 28, 2010
33
34
35
36
#else
#error ncurses gui enabled, but no known header file found
#endif
Mar 3, 2008
Mar 3, 2008
37
#include <locale.h>
38
39
40
41
42
43
44
45
46
// This was built to look roughly like dialog(1), but it's not nearly as
// robust. Also, I didn't use any of dialog's code, as it is GPL/LGPL,
// depending on what version you start with. There _is_ a libdialog, but
// it's never something installed on any systems, and I can't link it
// statically due to the license.
//
// ncurses is almost always installed as a shared library, though, so we'll
// just talk to it directly. Fortunately we don't need much of what dialog(1)
May 20, 2007
May 20, 2007
47
48
// offers, so rolling our own isn't too painful (well, compared to massive
// head trauma, I guess).
49
50
51
52
//
// Pradeep Padala's ncurses HOWTO was very helpful in teaching me curses
// quickly: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html
Mar 3, 2008
Mar 3, 2008
53
54
55
56
57
58
59
60
// !!! FIXME: this should all be UTF-8 and Unicode clean with ncursesw, but
// !!! FIXME: it relies on the terminal accepting UTF-8 output (we don't
// !!! FIXME: attempt to convert) and assumes all characters fit in one
// !!! FIXME: column, which they don't necessarily for some Asian languages,
// !!! FIXME: etc. I'm not sure how to properly figure out column width, if
// !!! FIXME: it's possible at all, but for that, you should probably
// !!! FIXME: go to a proper GUI plugin like GTK+ anyhow.
61
62
63
64
65
typedef enum
{
MOJOCOLOR_BACKGROUND=1,
MOJOCOLOR_BORDERTOP,
MOJOCOLOR_BORDERBOTTOM,
May 26, 2007
May 26, 2007
66
MOJOCOLOR_BORDERTITLE,
67
MOJOCOLOR_TEXT,
May 27, 2007
May 27, 2007
68
MOJOCOLOR_TEXTENTRY,
69
70
MOJOCOLOR_BUTTONHOVER,
MOJOCOLOR_BUTTONNORMAL,
May 26, 2007
May 26, 2007
71
MOJOCOLOR_BUTTONBORDER,
May 20, 2007
May 20, 2007
72
73
MOJOCOLOR_TODO,
MOJOCOLOR_DONE,
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
} MojoColor;
typedef struct
{
WINDOW *mainwin;
WINDOW *textwin;
WINDOW **buttons;
char *title;
char *text;
char **textlines;
char **buttontext;
int buttoncount;
int textlinecount;
int hoverover;
int textpos;
boolean hidecursor;
boolean ndelay;
int cursval;
} MojoBox;
May 20, 2007
May 20, 2007
96
static char *lastProgressType = NULL;
May 20, 2007
May 20, 2007
97
static char *lastComponent = NULL;
Jan 24, 2008
Jan 24, 2008
98
static boolean lastCanCancel = false;
May 20, 2007
May 20, 2007
99
100
static uint32 percentTicks = 0;
static char *title = NULL;
May 20, 2007
May 20, 2007
101
static MojoBox *progressBox = NULL;
May 20, 2007
May 20, 2007
102
103
104
105
106
static void drawButton(MojoBox *mojobox, int button)
{
const boolean hover = (mojobox->hoverover == button);
May 26, 2007
May 26, 2007
107
int borderattr = 0;
108
109
110
111
112
WINDOW *win = mojobox->buttons[button];
const char *str = mojobox->buttontext[button];
int w, h;
getmaxyx(win, h, w);
May 26, 2007
May 26, 2007
113
if (!hover)
114
wbkgdset(win, COLOR_PAIR(MOJOCOLOR_BUTTONNORMAL));
May 26, 2007
May 26, 2007
115
116
117
118
119
else
{
borderattr = COLOR_PAIR(MOJOCOLOR_BUTTONBORDER) | A_BOLD;
wbkgdset(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER));
} // else
120
May 26, 2007
May 26, 2007
121
werase(win);
122
wmove(win, 0, 0);
May 26, 2007
May 26, 2007
123
waddch(win, borderattr | '<');
124
wmove(win, 0, w-1);
May 26, 2007
May 26, 2007
125
waddch(win, borderattr | '>');
126
wmove(win, 0, 2);
May 26, 2007
May 26, 2007
127
128
129
130
131
132
133
134
135
if (!hover)
waddstr(win, str);
else
{
wattron(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
waddstr(win, str);
wattroff(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
} // else
136
137
138
139
140
141
} // drawButton
static void drawText(MojoBox *mojobox)
{
int i;
May 26, 2007
May 26, 2007
142
const int tcount = mojobox->textlinecount;
May 19, 2007
May 19, 2007
143
144
145
146
int pos = mojobox->textpos;
int w, h;
WINDOW *win = mojobox->textwin;
getmaxyx(win, h, w);
May 26, 2007
May 26, 2007
147
May 26, 2007
May 26, 2007
148
werase(mojobox->textwin);
May 26, 2007
May 26, 2007
149
for (i = 0; (pos < tcount) && (i < h); i++, pos++)
May 19, 2007
May 19, 2007
150
mvwaddstr(win, i, 0, mojobox->textlines[pos]);
May 26, 2007
May 26, 2007
151
152
153
154
155
156
157
158
159
if (tcount > h)
{
const int pct = (int) ((((double) pos) / ((double) tcount)) * 100.0);
win = mojobox->mainwin;
wattron(win, COLOR_PAIR(MOJOCOLOR_BORDERTITLE) | A_BOLD);
mvwprintw(win, h+1, w-5, "(%3d%%)", pct);
wattroff(win, COLOR_PAIR(MOJOCOLOR_BORDERTITLE) | A_BOLD);
} // if
160
161
162
} // drawText
May 20, 2007
May 20, 2007
163
164
165
166
167
static void drawBackground(WINDOW *win)
{
wclear(win);
if (title != NULL)
{
May 26, 2007
May 26, 2007
168
169
int w, h;
getmaxyx(win, h, w);
May 20, 2007
May 20, 2007
170
171
wattron(win, COLOR_PAIR(MOJOCOLOR_BACKGROUND) | A_BOLD);
mvwaddstr(win, 0, 0, title);
May 26, 2007
May 26, 2007
172
mvwhline(win, 1, 1, ACS_HLINE, w-2);
May 20, 2007
May 20, 2007
173
174
175
176
177
wattroff(win, COLOR_PAIR(MOJOCOLOR_BACKGROUND) | A_BOLD);
} // if
} // drawBackground
Jan 24, 2008
Jan 24, 2008
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
static void confirmTerminalSize(void)
{
int scrw = 0;
int scrh = 0;
char *msg = NULL;
int len = 0;
int x = 0;
int y = 0;
while (1) // loop until the window meets a minimum dimension requirement.
{
getmaxyx(stdscr, scrh, scrw);
scrh--; // -1 to save the title at the top of the screen...
if (scrw < 30) // too thin
Mar 2, 2008
Mar 2, 2008
193
msg = xstrdup(_("[Make the window wider!]"));
Jan 24, 2008
Jan 24, 2008
194
else if (scrh < 10) // too short
Mar 2, 2008
Mar 2, 2008
195
msg = xstrdup(_("[Make the window taller!]"));
Jan 24, 2008
Jan 24, 2008
196
197
198
else
break; // we're good, get out.
Mar 3, 2008
Mar 3, 2008
199
len = utf8len(msg);
Jan 24, 2008
Jan 24, 2008
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
y = scrh / 2;
x = ((scrw - len) / 2);
if (y < 0) y = 0;
if (x < 0) x = 0;
wclear(stdscr);
wmove(stdscr, y, x);
wrefresh(stdscr);
wmove(stdscr, y, x);
wattron(stdscr, COLOR_PAIR(MOJOCOLOR_BACKGROUND) | A_BOLD);
waddstr(stdscr, msg);
wattroff(stdscr, COLOR_PAIR(MOJOCOLOR_BACKGROUND) | A_BOLD);
nodelay(stdscr, 0);
wrefresh(stdscr);
free(msg);
while (wgetch(stdscr) != KEY_RESIZE) { /* no-op. */ }
} // while
} // confirmTerminalSize
222
223
224
225
226
227
228
229
230
static MojoBox *makeBox(const char *title, const char *text,
char **buttons, int bcount,
boolean ndelay, boolean hidecursor)
{
MojoBox *retval = NULL;
WINDOW *win = NULL;
int scrw, scrh;
int len = 0;
int buttonsw = 0;
May 26, 2007
May 26, 2007
231
232
int x = 0;
int y = 0;
233
234
235
236
237
int h = 0;
int w = 0;
int texth = 0;
int i;
Jan 24, 2008
Jan 24, 2008
238
239
confirmTerminalSize(); // blocks until window is large enough to continue.
240
getmaxyx(stdscr, scrh, scrw);
May 20, 2007
May 20, 2007
241
scrh--; // -1 to save the title at the top of the screen...
242
Mar 2, 2008
Mar 2, 2008
243
retval = (MojoBox *) xmalloc(sizeof (MojoBox));
244
245
246
retval->hidecursor = hidecursor;
retval->ndelay = ndelay;
retval->cursval = ((hidecursor) ? curs_set(0) : ERR);
Mar 2, 2008
Mar 2, 2008
247
248
retval->title = xstrdup(title);
retval->text = xstrdup(text);
249
retval->buttoncount = bcount;
Mar 2, 2008
Mar 2, 2008
250
251
retval->buttons = (WINDOW **) xmalloc(sizeof (WINDOW*) * bcount);
retval->buttontext = (char **) xmalloc(sizeof (char*) * bcount);
252
253
for (i = 0; i < bcount; i++)
Mar 2, 2008
Mar 2, 2008
254
retval->buttontext[i] = xstrdup(buttons[i]);
255
Mar 3, 2008
Mar 3, 2008
256
257
retval->textlines = splitText(retval->text, scrw-4,
&retval->textlinecount, &w);
258
Mar 3, 2008
Mar 3, 2008
259
len = utf8len(title);
260
261
262
263
264
265
266
267
268
269
270
271
if (len > scrw-4)
{
len = scrw-4;
strcpy(&retval->title[len-3], "..."); // !!! FIXME: not Unicode safe!
} // if
if (len > w)
w = len;
if (bcount > 0)
{
for (i = 0; i < bcount; i++)
Mar 3, 2008
Mar 3, 2008
272
buttonsw += utf8len(buttons[i]) + 5; // '<', ' ', ' ', '>', ' '
273
274
275
276
277
278
279
280
281
282
if (buttonsw > w)
w = buttonsw;
// !!! FIXME: what if these overflow the screen?
} // if
w += 4;
h = retval->textlinecount + 2;
if (bcount > 0)
h += 2;
May 26, 2007
May 26, 2007
283
284
if (h > scrh-2)
h = scrh-2;
285
May 26, 2007
May 26, 2007
286
287
x = (scrw - w) / 2;
y = ((scrh - h) / 2) + 1;
Jan 24, 2008
Jan 24, 2008
288
289
290
291
292
293
294
// can't have negative coordinates, so in case we survived the call to
// confirmTerminalSize() but still need more, just draw as much as
// possible from the top/left to fill the window.
if (x < 0) x = 0;
if (y < 0) y = 0;
May 26, 2007
May 26, 2007
295
win = retval->mainwin = newwin(h, w, y, x);
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
keypad(win, TRUE);
nodelay(win, ndelay);
wbkgdset(win, COLOR_PAIR(MOJOCOLOR_TEXT));
wclear(win);
waddch(win, ACS_ULCORNER | A_BOLD | COLOR_PAIR(MOJOCOLOR_BORDERTOP));
whline(win, ACS_HLINE | A_BOLD | COLOR_PAIR(MOJOCOLOR_BORDERTOP), w-2);
wmove(win, 0, w-1);
waddch(win, ACS_URCORNER | COLOR_PAIR(MOJOCOLOR_BORDERBOTTOM));
wmove(win, 1, 0);
wvline(win, ACS_VLINE | A_BOLD | COLOR_PAIR(MOJOCOLOR_BORDERTOP), h-2);
wmove(win, 1, w-1);
wvline(win, ACS_VLINE | COLOR_PAIR(MOJOCOLOR_BORDERBOTTOM), h-2);
wmove(win, h-1, 0);
waddch(win, ACS_LLCORNER | A_BOLD | COLOR_PAIR(MOJOCOLOR_BORDERTOP));
whline(win, ACS_HLINE | COLOR_PAIR(MOJOCOLOR_BORDERBOTTOM), w-2);
wmove(win, h-1, w-1);
waddch(win, ACS_LRCORNER | COLOR_PAIR(MOJOCOLOR_BORDERBOTTOM));
Mar 3, 2008
Mar 3, 2008
314
len = utf8len(retval->title);
315
wmove(win, 0, ((w-len)/2)-1);
May 26, 2007
May 26, 2007
316
317
wattron(win, COLOR_PAIR(MOJOCOLOR_BORDERTITLE) | A_BOLD);
waddch(win, ' ');
318
319
waddstr(win, retval->title);
wmove(win, 0, ((w-len)/2)+len);
May 26, 2007
May 26, 2007
320
321
waddch(win, ' ');
wattroff(win, COLOR_PAIR(MOJOCOLOR_BORDERTITLE) | A_BOLD);
322
323
324
if (bcount > 0)
{
May 26, 2007
May 26, 2007
325
326
const int buttony = (y + h) - 2;
int buttonx = (x + w) - ((w - buttonsw) / 2);
327
328
329
330
wmove(win, h-3, 1);
whline(win, ACS_HLINE | A_BOLD | COLOR_PAIR(MOJOCOLOR_BORDERTOP), w-2);
for (i = 0; i < bcount; i++)
{
Mar 3, 2008
Mar 3, 2008
331
len = utf8len(buttons[i]) + 4;
May 19, 2007
May 19, 2007
332
333
334
buttonx -= len+1;
win = retval->buttons[i] = newwin(1, len, buttony, buttonx);
keypad(win, TRUE);
335
336
337
338
339
340
341
nodelay(win, ndelay);
} // for
} // if
texth = h-2;
if (bcount > 0)
texth -= 2;
May 26, 2007
May 26, 2007
342
win = retval->textwin = newwin(texth, w-4, y+1, x+2);
343
344
345
346
347
keypad(win, TRUE);
nodelay(win, ndelay);
wbkgdset(win, COLOR_PAIR(MOJOCOLOR_TEXT));
drawText(retval);
May 20, 2007
May 20, 2007
348
drawBackground(stdscr);
May 26, 2007
May 26, 2007
349
350
351
wnoutrefresh(stdscr);
wnoutrefresh(retval->mainwin);
wnoutrefresh(retval->textwin);
352
353
354
for (i = 0; i < bcount; i++)
{
drawButton(retval, i);
May 26, 2007
May 26, 2007
355
wnoutrefresh(retval->buttons[i]);
356
357
} // for
May 26, 2007
May 26, 2007
358
359
doupdate(); // push it all to the screen.
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
return retval;
} // makeBox
static void freeBox(MojoBox *mojobox, boolean clearscreen)
{
if (mojobox != NULL)
{
int i;
const int bcount = mojobox->buttoncount;
const int tcount = mojobox->textlinecount;
if (mojobox->cursval != ERR)
curs_set(mojobox->cursval);
for (i = 0; i < bcount; i++)
{
free(mojobox->buttontext[i]);
delwin(mojobox->buttons[i]);
} // for
free(mojobox->buttontext);
free(mojobox->buttons);
delwin(mojobox->textwin);
delwin(mojobox->mainwin);
free(mojobox->title);
free(mojobox->text);
for (i = 0; i < tcount; i++)
free(mojobox->textlines[i]);
free(mojobox->textlines);
free(mojobox);
if (clearscreen)
{
wclear(stdscr);
wrefresh(stdscr);
} // if
} // if
} // freeBox
static int upkeepBox(MojoBox **_mojobox, int ch)
{
May 19, 2007
May 19, 2007
407
static boolean justResized = false;
May 26, 2007
May 26, 2007
408
MEVENT mevt;
May 27, 2007
May 27, 2007
409
int i;
May 19, 2007
May 19, 2007
410
int w, h;
411
412
413
414
MojoBox *mojobox = *_mojobox;
if (mojobox == NULL)
return -2;
May 20, 2007
May 20, 2007
415
416
417
418
419
420
421
if (justResized) // !!! FIXME: this is a kludge.
{
justResized = false;
if (ch == ERR)
return -1;
} // if
422
423
424
425
426
427
428
429
430
switch (ch)
{
case ERR:
return -2;
case '\r':
case '\n':
case KEY_ENTER:
case ' ':
Jan 24, 2008
Jan 24, 2008
431
return (mojobox->buttoncount <= 0) ? -1 : mojobox->hoverover;
432
433
434
435
case '\e':
return mojobox->buttoncount-1;
May 19, 2007
May 19, 2007
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
case KEY_UP:
if (mojobox->textpos > 0)
{
mojobox->textpos--;
drawText(mojobox);
wrefresh(mojobox->textwin);
} // if
return -1;
case KEY_DOWN:
getmaxyx(mojobox->textwin, h, w);
if (mojobox->textpos < (mojobox->textlinecount-h))
{
mojobox->textpos++;
drawText(mojobox);
wrefresh(mojobox->textwin);
} // if
return -1;
case KEY_PPAGE:
if (mojobox->textpos > 0)
{
getmaxyx(mojobox->textwin, h, w);
mojobox->textpos -= h;
if (mojobox->textpos < 0)
mojobox->textpos = 0;
drawText(mojobox);
wrefresh(mojobox->textwin);
} // if
return -1;
case KEY_NPAGE:
getmaxyx(mojobox->textwin, h, w);
if (mojobox->textpos < (mojobox->textlinecount-h))
{
mojobox->textpos += h;
if (mojobox->textpos > (mojobox->textlinecount-h))
mojobox->textpos = (mojobox->textlinecount-h);
drawText(mojobox);
wrefresh(mojobox->textwin);
} // if
return -1;
case KEY_LEFT:
if (mojobox->buttoncount > 1)
{
if (mojobox->hoverover < (mojobox->buttoncount-1))
{
mojobox->hoverover++;
drawButton(mojobox, mojobox->hoverover-1);
drawButton(mojobox, mojobox->hoverover);
wrefresh(mojobox->buttons[mojobox->hoverover-1]);
wrefresh(mojobox->buttons[mojobox->hoverover]);
} // if
} // if
return -1;
case KEY_RIGHT:
if (mojobox->buttoncount > 1)
{
if (mojobox->hoverover > 0)
{
mojobox->hoverover--;
drawButton(mojobox, mojobox->hoverover+1);
drawButton(mojobox, mojobox->hoverover);
wrefresh(mojobox->buttons[mojobox->hoverover+1]);
wrefresh(mojobox->buttons[mojobox->hoverover]);
} // if
} // if
return -1;
May 27, 2007
May 27, 2007
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
case 12: // ctrl-L...redraw everything on the screen.
redrawwin(stdscr);
wnoutrefresh(stdscr);
redrawwin(mojobox->mainwin);
wnoutrefresh(mojobox->mainwin);
redrawwin(mojobox->textwin);
wnoutrefresh(mojobox->textwin);
for (i = 0; i < mojobox->buttoncount; i++)
{
redrawwin(mojobox->buttons[i]);
wnoutrefresh(mojobox->buttons[i]);
} // for
doupdate(); // push it all to the screen.
return -1;
522
523
524
525
case KEY_RESIZE:
mojobox = makeBox(mojobox->title, mojobox->text,
mojobox->buttontext, mojobox->buttoncount,
mojobox->ndelay, mojobox->hidecursor);
May 19, 2007
May 19, 2007
526
mojobox->cursval = (*_mojobox)->cursval; // keep this sane.
May 20, 2007
May 20, 2007
527
mojobox->hoverover = (*_mojobox)->hoverover;
528
freeBox(*_mojobox, false);
May 19, 2007
May 19, 2007
529
530
if (mojobox->hidecursor)
curs_set(0); // make sure this stays sane.
531
*_mojobox = mojobox;
May 20, 2007
May 20, 2007
532
justResized = true; // !!! FIXME: kludge.
533
return -1;
May 26, 2007
May 26, 2007
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
case KEY_MOUSE:
if ((getmouse(&mevt) == OK) && (mevt.bstate & BUTTON1_CLICKED))
{
int i;
for (i = 0; i < mojobox->buttoncount; i++)
{
int x1, y1, x2, y2;
getbegyx(mojobox->buttons[i], y1, x1);
getmaxyx(mojobox->buttons[i], y2, x2);
x2 += x1;
y2 += y1;
if ( (mevt.x >= x1) && (mevt.x < x2) &&
(mevt.y >= y1) && (mevt.y < y2) )
return i;
} // for
} // if
return -1;
552
553
554
555
556
557
} // switch
return -1;
} // upkeepBox
Nov 21, 2007
Nov 21, 2007
558
static uint8 MojoGui_ncurses_priority(boolean istty)
559
{
Nov 21, 2007
Nov 21, 2007
560
561
562
if (!istty)
return MOJOGUI_PRIORITY_NEVER_TRY; // need a terminal for this!
else if (getenv("DISPLAY") != NULL)
563
564
565
566
567
568
569
return MOJOGUI_PRIORITY_TRY_LAST; // let graphical stuff go first.
return MOJOGUI_PRIORITY_TRY_NORMAL;
} // MojoGui_ncurses_priority
static boolean MojoGui_ncurses_init(void)
{
Mar 3, 2008
Mar 3, 2008
570
setlocale(LC_CTYPE, ""); // !!! FIXME: we assume you have a UTF-8 terminal.
571
572
if (initscr() == NULL)
{
Mar 2, 2008
Mar 2, 2008
573
logInfo("ncurses: initscr() failed, use another UI.");
574
575
576
577
578
579
580
return false;
} // if
cbreak();
keypad(stdscr, TRUE);
noecho();
start_color();
May 26, 2007
May 26, 2007
581
mousemask(BUTTON1_CLICKED, NULL);
May 20, 2007
May 20, 2007
582
init_pair(MOJOCOLOR_BACKGROUND, COLOR_CYAN, COLOR_BLUE);
583
584
init_pair(MOJOCOLOR_BORDERTOP, COLOR_WHITE, COLOR_WHITE);
init_pair(MOJOCOLOR_BORDERBOTTOM, COLOR_BLACK, COLOR_WHITE);
May 26, 2007
May 26, 2007
585
init_pair(MOJOCOLOR_BORDERTITLE, COLOR_YELLOW, COLOR_WHITE);
586
init_pair(MOJOCOLOR_TEXT, COLOR_BLACK, COLOR_WHITE);
May 27, 2007
May 27, 2007
587
init_pair(MOJOCOLOR_TEXTENTRY, COLOR_WHITE, COLOR_BLUE);
May 26, 2007
May 26, 2007
588
init_pair(MOJOCOLOR_BUTTONHOVER, COLOR_YELLOW, COLOR_BLUE);
589
init_pair(MOJOCOLOR_BUTTONNORMAL, COLOR_BLACK, COLOR_WHITE);
May 26, 2007
May 26, 2007
590
init_pair(MOJOCOLOR_BUTTONBORDER, COLOR_WHITE, COLOR_BLUE);
May 20, 2007
May 20, 2007
591
592
init_pair(MOJOCOLOR_DONE, COLOR_YELLOW, COLOR_RED);
init_pair(MOJOCOLOR_TODO, COLOR_CYAN, COLOR_BLUE);
May 20, 2007
May 20, 2007
593
594
595
596
597
598
599
600
601
602
603
604
wbkgdset(stdscr, COLOR_PAIR(MOJOCOLOR_BACKGROUND));
wclear(stdscr);
wrefresh(stdscr);
percentTicks = 0;
return true; // always succeeds.
} // MojoGui_ncurses_init
static void MojoGui_ncurses_deinit(void)
{
May 20, 2007
May 20, 2007
605
606
freeBox(progressBox, false);
progressBox = NULL;
607
608
609
endwin();
delwin(stdscr); // not sure if this is safe, but valgrind said it leaks.
stdscr = NULL;
May 20, 2007
May 20, 2007
610
611
free(title);
title = NULL;
612
613
free(lastComponent);
lastComponent = NULL;
May 20, 2007
May 20, 2007
614
615
free(lastProgressType);
lastProgressType = NULL;
616
617
618
619
620
} // MojoGui_ncurses_deinit
static void MojoGui_ncurses_msgbox(const char *title, const char *text)
{
Mar 2, 2008
Mar 2, 2008
621
char *localized_ok = xstrdup(_("OK"));
622
623
624
625
626
627
628
MojoBox *mojobox = makeBox(title, text, &localized_ok, 1, false, true);
while (upkeepBox(&mojobox, wgetch(mojobox->mainwin)) == -1) {}
freeBox(mojobox, true);
free(localized_ok);
} // MojoGui_ncurses_msgbox
Jul 2, 2007
Jul 2, 2007
629
630
static boolean MojoGui_ncurses_promptyn(const char *title, const char *text,
boolean defval)
631
{
Mar 2, 2008
Mar 2, 2008
632
633
char *localized_yes = xstrdup(_("Yes"));
char *localized_no = xstrdup(_("No"));
634
635
636
char *buttons[] = { localized_yes, localized_no };
MojoBox *mojobox = makeBox(title, text, buttons, 2, false, true);
int rc = 0;
Jul 2, 2007
Jul 2, 2007
637
638
639
640
641
642
643
644
645
646
647
// set the default to "no" instead of "yes"?
if (defval == false)
{
mojobox->hoverover = 1;
drawButton(mojobox, 0);
drawButton(mojobox, 1);
wrefresh(mojobox->buttons[0]);
wrefresh(mojobox->buttons[1]);
} // if
648
649
650
651
652
653
654
655
656
while ((rc = upkeepBox(&mojobox, wgetch(mojobox->mainwin))) == -1) {}
freeBox(mojobox, true);
free(localized_yes);
free(localized_no);
return (rc == 0);
} // MojoGui_ncurses_promptyn
static MojoGuiYNAN MojoGui_ncurses_promptynan(const char *title,
Jul 2, 2007
Jul 2, 2007
657
658
const char *text,
boolean defval)
659
{
Mar 2, 2008
Mar 2, 2008
660
661
662
663
char *loc_yes = xstrdup(_("Yes"));
char *loc_no = xstrdup(_("No"));
char *loc_always = xstrdup(_("Always"));
char *loc_never = xstrdup(_("Never"));
664
665
666
char *buttons[] = { loc_yes, loc_always, loc_never, loc_no };
MojoBox *mojobox = makeBox(title, text, buttons, 4, false, true);
int rc = 0;
Jul 2, 2007
Jul 2, 2007
667
668
669
670
671
672
673
674
675
676
677
// set the default to "no" instead of "yes"?
if (defval == false)
{
mojobox->hoverover = 3;
drawButton(mojobox, 0);
drawButton(mojobox, 3);
wrefresh(mojobox->buttons[0]);
wrefresh(mojobox->buttons[3]);
} // if
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
while ((rc = upkeepBox(&mojobox, wgetch(mojobox->mainwin))) == -1) {}
freeBox(mojobox, true);
free(loc_yes);
free(loc_no);
free(loc_always);
free(loc_never);
switch (rc)
{
case 0: return MOJOGUI_YES;
case 1: return MOJOGUI_ALWAYS;
case 2: return MOJOGUI_NEVER;
case 3: return MOJOGUI_NO;
} // switch
assert(false && "BUG: unhandled case in switch statement!");
return MOJOGUI_NO;
} // MojoGui_ncurses_promptynan
Nov 24, 2007
Nov 24, 2007
698
699
static boolean MojoGui_ncurses_start(const char *_title,
const MojoGuiSplash *splash)
700
{
May 20, 2007
May 20, 2007
701
free(title);
Mar 2, 2008
Mar 2, 2008
702
title = xstrdup(_title);
May 20, 2007
May 20, 2007
703
drawBackground(stdscr);
704
705
706
707
708
709
710
wrefresh(stdscr);
return true;
} // MojoGui_ncurses_start
static void MojoGui_ncurses_stop(void)
{
May 20, 2007
May 20, 2007
711
712
713
free(title);
title = NULL;
drawBackground(stdscr);
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
wrefresh(stdscr);
} // MojoGui_ncurses_stop
static int MojoGui_ncurses_readme(const char *name, const uint8 *data,
size_t datalen, boolean can_back,
boolean can_fwd)
{
MojoBox *mojobox = NULL;
char *buttons[3] = { NULL, NULL, NULL };
int bcount = 0;
int backbutton = -99;
int fwdbutton = -99;
int rc = 0;
int i = 0;
if (can_fwd)
{
fwdbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
733
buttons[fwdbutton] = xstrdup(_("Next"));
734
735
736
737
738
} // if
if (can_back)
{
backbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
739
buttons[backbutton] = xstrdup(_("Back"));
740
741
} // if
Mar 2, 2008
Mar 2, 2008
742
buttons[bcount++] = xstrdup(_("Cancel"));
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
mojobox = makeBox(name, (char *) data, buttons, bcount, false, true);
while ((rc = upkeepBox(&mojobox, wgetch(mojobox->mainwin))) == -1) {}
freeBox(mojobox, true);
for (i = 0; i < bcount; i++)
free(buttons[i]);
if (rc == backbutton)
return -1;
else if (rc == fwdbutton)
return 1;
return 0; // error? Cancel?
} // MojoGui_ncurses_readme
May 20, 2007
May 20, 2007
760
761
762
763
764
765
static int toggle_option(MojoGuiSetupOptions *parent,
MojoGuiSetupOptions *opts, int *line, int target)
{
int rc = -1;
if ((opts != NULL) && (target > *line))
{
May 27, 2007
May 27, 2007
766
767
768
769
770
const char *desc = opts->description;
boolean blanked = false;
blanked = ( (opts->is_group_parent) && ((!desc) || (!(*desc))) );
if ((!blanked) && (++(*line) == target))
May 20, 2007
May 20, 2007
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
{
const boolean toggled = ((opts->value) ? false : true);
if (opts->is_group_parent)
return 0;
// "radio buttons" in a group?
if ((parent) && (parent->is_group_parent))
{
if (toggled) // drop unless we weren't the current toggle.
{
// set all siblings to false...
MojoGuiSetupOptions *i = parent->child;
while (i != NULL)
{
i->value = false;
i = i->next_sibling;
} // while
opts->value = true; // reset us to be true.
} // if
} // if
else // individual "check box" was chosen.
{
opts->value = toggled;
} // else
return 1; // we found it, bail.
} // if
if (opts->value) // if option is toggled on, descend to children.
rc = toggle_option(opts, opts->child, line, target);
if (rc == -1)
rc = toggle_option(parent, opts->next_sibling, line, target);
} // if
return rc;
} // toggle_option
// This code is pretty scary.
static void build_options(MojoGuiSetupOptions *opts, int *line, int level,
int maxw, char **lines)
{
if (opts != NULL)
{
May 27, 2007
May 27, 2007
817
const char *desc = opts->description;
Mar 2, 2008
Mar 2, 2008
818
819
char *spacebuf = (char *) xmalloc(maxw + 1);
char *buf = (char *) xmalloc(maxw + 1);
May 20, 2007
May 20, 2007
820
int len = 0;
May 27, 2007
May 27, 2007
821
822
823
824
int spacing = level * 2;
if ((desc != NULL) && (*desc == '\0'))
desc = NULL;
May 20, 2007
May 20, 2007
825
826
827
828
829
830
831
832
if (spacing > (maxw-5))
spacing = 0; // oh well.
if (spacing > 0)
memset(spacebuf, ' ', spacing); // null-term'd by xmalloc().
if (opts->is_group_parent)
May 27, 2007
May 27, 2007
833
834
835
836
{
if (desc != NULL)
len = snprintf(buf, maxw-2, "%s%s", spacebuf, desc);
} // if
May 20, 2007
May 20, 2007
837
838
839
840
else
{
(*line)++;
len = snprintf(buf, maxw-2, "%s[%c] %s", spacebuf,
May 27, 2007
May 27, 2007
841
opts->value ? 'X' : ' ', desc);
May 20, 2007
May 20, 2007
842
843
844
845
846
847
848
} // else
free(spacebuf);
if (len >= maxw-1)
strcpy(buf+(maxw-4), "..."); // !!! FIXME: Unicode issues!
May 27, 2007
May 27, 2007
849
850
851
if (len > 0)
{
const size_t newlen = strlen(*lines) + strlen(buf) + 2;
Mar 2, 2008
Mar 2, 2008
852
*lines = (char*) xrealloc(*lines, newlen);
May 27, 2007
May 27, 2007
853
854
855
strcat(*lines, buf);
strcat(*lines, "\n"); // I'm sorry, Joel Spolsky!
} // if
May 20, 2007
May 20, 2007
856
857
if ((opts->value) || (opts->is_group_parent))
May 27, 2007
May 27, 2007
858
859
860
861
862
863
864
{
int newlev = level + 1;
if ((opts->is_group_parent) && (desc == NULL))
newlev--;
build_options(opts->child, line, newlev, maxw, lines);
} // if
May 20, 2007
May 20, 2007
865
866
867
868
869
build_options(opts->next_sibling, line, level, maxw, lines);
} // if
} // build_options
May 27, 2007
May 27, 2007
870
871
static int optionBox(const char *title, MojoGuiSetupOptions *opts,
boolean can_back, boolean can_fwd)
872
{
May 20, 2007
May 20, 2007
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
MojoBox *mojobox = NULL;
char *buttons[4] = { NULL, NULL, NULL, NULL };
boolean ignoreerr = false;
int lasthoverover = 0;
int lasttextpos = 0;
int bcount = 0;
int backbutton = -99;
int fwdbutton = -99;
int togglebutton = -99;
int cancelbutton = -99;
int selected = 0;
int ch = 0;
int rc = -1;
int i = 0;
if (can_fwd)
{
fwdbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
891
buttons[fwdbutton] = xstrdup(_("Next"));
May 20, 2007
May 20, 2007
892
893
894
895
896
} // if
if (can_back)
{
backbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
897
buttons[backbutton] = xstrdup(_("Back"));
May 20, 2007
May 20, 2007
898
899
900
} // if
lasthoverover = togglebutton = bcount++;
Mar 2, 2008
Mar 2, 2008
901
buttons[togglebutton] = xstrdup(_("Toggle"));
May 20, 2007
May 20, 2007
902
cancelbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
903
buttons[cancelbutton] = xstrdup(_("Cancel"));
May 20, 2007
May 20, 2007
904
905
906
907
908
909
910
911
912
do
{
if (mojobox == NULL)
{
int y = 0;
int line = 0;
int maxw, maxh;
getmaxyx(stdscr, maxh, maxw);
Mar 2, 2008
Mar 2, 2008
913
char *text = xstrdup("");
May 27, 2007
May 27, 2007
914
build_options(opts, &line, 0, maxw-6, &text);
May 20, 2007
May 20, 2007
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
mojobox = makeBox(title, text, buttons, bcount, false, true);
free(text);
getmaxyx(mojobox->textwin, maxh, maxw);
if (lasthoverover != mojobox->hoverover)
{
const int orighover = mojobox->hoverover;
mojobox->hoverover = lasthoverover;
drawButton(mojobox, orighover);
drawButton(mojobox, lasthoverover);
wrefresh(mojobox->buttons[orighover]);
wrefresh(mojobox->buttons[lasthoverover]);
} // if
if (lasttextpos != mojobox->textpos)
{
mojobox->textpos = lasttextpos;
drawText(mojobox);
} // if
if (selected >= (mojobox->textlinecount - 1))
selected = mojobox->textlinecount - 1;
if (selected >= mojobox->textpos+maxh)
selected = (mojobox->textpos+maxh) - 1;
y = selected - lasttextpos;
May 26, 2007
May 26, 2007
942
wattron(mojobox->textwin, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
943
944
mvwhline(mojobox->textwin, y, 0, ' ', maxw);
mvwaddstr(mojobox->textwin, y, 0, mojobox->textlines[selected]);
May 26, 2007
May 26, 2007
945
wattroff(mojobox->textwin, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
wrefresh(mojobox->textwin);
} // if
lasttextpos = mojobox->textpos;
lasthoverover = mojobox->hoverover;
ch = wgetch(mojobox->mainwin);
if (ignoreerr) // kludge.
{
ignoreerr = false;
if (ch == ERR)
continue;
} // if
if (ch == KEY_RESIZE)
{
freeBox(mojobox, false); // catch and rebuild without upkeepBox,
mojobox = NULL; // so we can rebuild the text ourself.
ignoreerr = true; // kludge.
} // if
else if (ch == KEY_UP)
{
if (selected > 0)
{
WINDOW *win = mojobox->textwin;
int maxw, maxh;
int y = --selected - mojobox->textpos;
getmaxyx(win, maxh, maxw);
if (selected < mojobox->textpos)
{
upkeepBox(&mojobox, ch); // upkeepBox does scrolling
y++;
} // if
else
{
wattron(win, COLOR_PAIR(MOJOCOLOR_TEXT));
mvwhline(win, y+1, 0, ' ', maxw);
mvwaddstr(win, y+1, 0, mojobox->textlines[selected+1]);
wattroff(win, COLOR_PAIR(MOJOCOLOR_TEXT));
} // else
May 26, 2007
May 26, 2007
988
wattron(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
989
990
mvwhline(win, y, 0, ' ', maxw);
mvwaddstr(win, y, 0, mojobox->textlines[selected]);
May 26, 2007
May 26, 2007
991
wattroff(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
992
993
994
995
996
997
998
999
1000
wrefresh(win);
} // if
} // else if
else if (ch == KEY_DOWN)
{
if (selected < (mojobox->textlinecount-1))
{
WINDOW *win = mojobox->textwin;