Skip to content

Latest commit

 

History

History
1477 lines (1257 loc) · 43.3 KB

gui_ncurses.c

File metadata and controls

1477 lines (1257 loc) · 43.3 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>
Mar 3, 2008
Mar 3, 2008
24
25
#include <ncursesw/curses.h>
#include <locale.h>
26
27
28
29
30
31
32
33
34
// 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
35
36
// offers, so rolling our own isn't too painful (well, compared to massive
// head trauma, I guess).
37
38
39
40
//
// 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
41
42
43
44
45
46
47
48
// !!! 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.
49
50
51
52
53
typedef enum
{
MOJOCOLOR_BACKGROUND=1,
MOJOCOLOR_BORDERTOP,
MOJOCOLOR_BORDERBOTTOM,
May 26, 2007
May 26, 2007
54
MOJOCOLOR_BORDERTITLE,
55
MOJOCOLOR_TEXT,
May 27, 2007
May 27, 2007
56
MOJOCOLOR_TEXTENTRY,
57
58
MOJOCOLOR_BUTTONHOVER,
MOJOCOLOR_BUTTONNORMAL,
May 26, 2007
May 26, 2007
59
MOJOCOLOR_BUTTONBORDER,
May 20, 2007
May 20, 2007
60
61
MOJOCOLOR_TODO,
MOJOCOLOR_DONE,
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
} 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
84
static char *lastProgressType = NULL;
May 20, 2007
May 20, 2007
85
static char *lastComponent = NULL;
Jan 24, 2008
Jan 24, 2008
86
static boolean lastCanCancel = false;
May 20, 2007
May 20, 2007
87
88
static uint32 percentTicks = 0;
static char *title = NULL;
May 20, 2007
May 20, 2007
89
static MojoBox *progressBox = NULL;
May 20, 2007
May 20, 2007
90
91
92
93
94
static void drawButton(MojoBox *mojobox, int button)
{
const boolean hover = (mojobox->hoverover == button);
May 26, 2007
May 26, 2007
95
int borderattr = 0;
96
97
98
99
100
WINDOW *win = mojobox->buttons[button];
const char *str = mojobox->buttontext[button];
int w, h;
getmaxyx(win, h, w);
May 26, 2007
May 26, 2007
101
if (!hover)
102
wbkgdset(win, COLOR_PAIR(MOJOCOLOR_BUTTONNORMAL));
May 26, 2007
May 26, 2007
103
104
105
106
107
else
{
borderattr = COLOR_PAIR(MOJOCOLOR_BUTTONBORDER) | A_BOLD;
wbkgdset(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER));
} // else
108
May 26, 2007
May 26, 2007
109
werase(win);
110
wmove(win, 0, 0);
May 26, 2007
May 26, 2007
111
waddch(win, borderattr | '<');
112
wmove(win, 0, w-1);
May 26, 2007
May 26, 2007
113
waddch(win, borderattr | '>');
114
wmove(win, 0, 2);
May 26, 2007
May 26, 2007
115
116
117
118
119
120
121
122
123
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
124
125
126
127
128
129
} // drawButton
static void drawText(MojoBox *mojobox)
{
int i;
May 26, 2007
May 26, 2007
130
const int tcount = mojobox->textlinecount;
May 19, 2007
May 19, 2007
131
132
133
134
int pos = mojobox->textpos;
int w, h;
WINDOW *win = mojobox->textwin;
getmaxyx(win, h, w);
May 26, 2007
May 26, 2007
135
May 26, 2007
May 26, 2007
136
werase(mojobox->textwin);
May 26, 2007
May 26, 2007
137
for (i = 0; (pos < tcount) && (i < h); i++, pos++)
May 19, 2007
May 19, 2007
138
mvwaddstr(win, i, 0, mojobox->textlines[pos]);
May 26, 2007
May 26, 2007
139
140
141
142
143
144
145
146
147
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
148
149
150
} // drawText
May 20, 2007
May 20, 2007
151
152
153
154
155
static void drawBackground(WINDOW *win)
{
wclear(win);
if (title != NULL)
{
May 26, 2007
May 26, 2007
156
157
int w, h;
getmaxyx(win, h, w);
May 20, 2007
May 20, 2007
158
159
wattron(win, COLOR_PAIR(MOJOCOLOR_BACKGROUND) | A_BOLD);
mvwaddstr(win, 0, 0, title);
May 26, 2007
May 26, 2007
160
mvwhline(win, 1, 1, ACS_HLINE, w-2);
May 20, 2007
May 20, 2007
161
162
163
164
165
wattroff(win, COLOR_PAIR(MOJOCOLOR_BACKGROUND) | A_BOLD);
} // if
} // drawBackground
Jan 24, 2008
Jan 24, 2008
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
181
msg = xstrdup(_("[Make the window wider!]"));
Jan 24, 2008
Jan 24, 2008
182
else if (scrh < 10) // too short
Mar 2, 2008
Mar 2, 2008
183
msg = xstrdup(_("[Make the window taller!]"));
Jan 24, 2008
Jan 24, 2008
184
185
186
else
break; // we're good, get out.
Mar 3, 2008
Mar 3, 2008
187
len = utf8len(msg);
Jan 24, 2008
Jan 24, 2008
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
210
211
212
213
214
215
216
217
218
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
219
220
int x = 0;
int y = 0;
221
222
223
224
225
int h = 0;
int w = 0;
int texth = 0;
int i;
Jan 24, 2008
Jan 24, 2008
226
227
confirmTerminalSize(); // blocks until window is large enough to continue.
228
getmaxyx(stdscr, scrh, scrw);
May 20, 2007
May 20, 2007
229
scrh--; // -1 to save the title at the top of the screen...
230
Mar 2, 2008
Mar 2, 2008
231
retval = (MojoBox *) xmalloc(sizeof (MojoBox));
232
233
234
retval->hidecursor = hidecursor;
retval->ndelay = ndelay;
retval->cursval = ((hidecursor) ? curs_set(0) : ERR);
Mar 2, 2008
Mar 2, 2008
235
236
retval->title = xstrdup(title);
retval->text = xstrdup(text);
237
retval->buttoncount = bcount;
Mar 2, 2008
Mar 2, 2008
238
239
retval->buttons = (WINDOW **) xmalloc(sizeof (WINDOW*) * bcount);
retval->buttontext = (char **) xmalloc(sizeof (char*) * bcount);
240
241
for (i = 0; i < bcount; i++)
Mar 2, 2008
Mar 2, 2008
242
retval->buttontext[i] = xstrdup(buttons[i]);
243
Mar 3, 2008
Mar 3, 2008
244
245
retval->textlines = splitText(retval->text, scrw-4,
&retval->textlinecount, &w);
246
Mar 3, 2008
Mar 3, 2008
247
len = utf8len(title);
248
249
250
251
252
253
254
255
256
257
258
259
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
260
buttonsw += utf8len(buttons[i]) + 5; // '<', ' ', ' ', '>', ' '
261
262
263
264
265
266
267
268
269
270
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
271
272
if (h > scrh-2)
h = scrh-2;
273
May 26, 2007
May 26, 2007
274
275
x = (scrw - w) / 2;
y = ((scrh - h) / 2) + 1;
Jan 24, 2008
Jan 24, 2008
276
277
278
279
280
281
282
// 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
283
win = retval->mainwin = newwin(h, w, y, x);
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
302
len = utf8len(retval->title);
303
wmove(win, 0, ((w-len)/2)-1);
May 26, 2007
May 26, 2007
304
305
wattron(win, COLOR_PAIR(MOJOCOLOR_BORDERTITLE) | A_BOLD);
waddch(win, ' ');
306
307
waddstr(win, retval->title);
wmove(win, 0, ((w-len)/2)+len);
May 26, 2007
May 26, 2007
308
309
waddch(win, ' ');
wattroff(win, COLOR_PAIR(MOJOCOLOR_BORDERTITLE) | A_BOLD);
310
311
312
if (bcount > 0)
{
May 26, 2007
May 26, 2007
313
314
const int buttony = (y + h) - 2;
int buttonx = (x + w) - ((w - buttonsw) / 2);
315
316
317
318
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
319
len = utf8len(buttons[i]) + 4;
May 19, 2007
May 19, 2007
320
321
322
buttonx -= len+1;
win = retval->buttons[i] = newwin(1, len, buttony, buttonx);
keypad(win, TRUE);
323
324
325
326
327
328
329
nodelay(win, ndelay);
} // for
} // if
texth = h-2;
if (bcount > 0)
texth -= 2;
May 26, 2007
May 26, 2007
330
win = retval->textwin = newwin(texth, w-4, y+1, x+2);
331
332
333
334
335
keypad(win, TRUE);
nodelay(win, ndelay);
wbkgdset(win, COLOR_PAIR(MOJOCOLOR_TEXT));
drawText(retval);
May 20, 2007
May 20, 2007
336
drawBackground(stdscr);
May 26, 2007
May 26, 2007
337
338
339
wnoutrefresh(stdscr);
wnoutrefresh(retval->mainwin);
wnoutrefresh(retval->textwin);
340
341
342
for (i = 0; i < bcount; i++)
{
drawButton(retval, i);
May 26, 2007
May 26, 2007
343
wnoutrefresh(retval->buttons[i]);
344
345
} // for
May 26, 2007
May 26, 2007
346
347
doupdate(); // push it all to the screen.
348
349
350
351
352
353
354
355
356
357
358
359
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
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
395
static boolean justResized = false;
May 26, 2007
May 26, 2007
396
MEVENT mevt;
May 27, 2007
May 27, 2007
397
int i;
May 19, 2007
May 19, 2007
398
int w, h;
399
400
401
402
MojoBox *mojobox = *_mojobox;
if (mojobox == NULL)
return -2;
May 20, 2007
May 20, 2007
403
404
405
406
407
408
409
if (justResized) // !!! FIXME: this is a kludge.
{
justResized = false;
if (ch == ERR)
return -1;
} // if
410
411
412
413
414
415
416
417
418
switch (ch)
{
case ERR:
return -2;
case '\r':
case '\n':
case KEY_ENTER:
case ' ':
Jan 24, 2008
Jan 24, 2008
419
return (mojobox->buttoncount <= 0) ? -1 : mojobox->hoverover;
420
421
422
423
case '\e':
return mojobox->buttoncount-1;
May 19, 2007
May 19, 2007
424
425
426
427
428
429
430
431
432
433
434
435
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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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;
510
511
512
513
case KEY_RESIZE:
mojobox = makeBox(mojobox->title, mojobox->text,
mojobox->buttontext, mojobox->buttoncount,
mojobox->ndelay, mojobox->hidecursor);
May 19, 2007
May 19, 2007
514
mojobox->cursval = (*_mojobox)->cursval; // keep this sane.
May 20, 2007
May 20, 2007
515
mojobox->hoverover = (*_mojobox)->hoverover;
516
freeBox(*_mojobox, false);
May 19, 2007
May 19, 2007
517
518
if (mojobox->hidecursor)
curs_set(0); // make sure this stays sane.
519
*_mojobox = mojobox;
May 20, 2007
May 20, 2007
520
justResized = true; // !!! FIXME: kludge.
521
return -1;
May 26, 2007
May 26, 2007
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
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;
540
541
542
543
544
545
} // switch
return -1;
} // upkeepBox
Nov 21, 2007
Nov 21, 2007
546
static uint8 MojoGui_ncurses_priority(boolean istty)
547
{
Nov 21, 2007
Nov 21, 2007
548
549
550
if (!istty)
return MOJOGUI_PRIORITY_NEVER_TRY; // need a terminal for this!
else if (getenv("DISPLAY") != NULL)
551
552
553
554
555
556
557
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
558
setlocale(LC_CTYPE, ""); // !!! FIXME: we assume you have a UTF-8 terminal.
559
560
if (initscr() == NULL)
{
Mar 2, 2008
Mar 2, 2008
561
logInfo("ncurses: initscr() failed, use another UI.");
562
563
564
565
566
567
568
return false;
} // if
cbreak();
keypad(stdscr, TRUE);
noecho();
start_color();
May 26, 2007
May 26, 2007
569
mousemask(BUTTON1_CLICKED, NULL);
May 20, 2007
May 20, 2007
570
init_pair(MOJOCOLOR_BACKGROUND, COLOR_CYAN, COLOR_BLUE);
571
572
init_pair(MOJOCOLOR_BORDERTOP, COLOR_WHITE, COLOR_WHITE);
init_pair(MOJOCOLOR_BORDERBOTTOM, COLOR_BLACK, COLOR_WHITE);
May 26, 2007
May 26, 2007
573
init_pair(MOJOCOLOR_BORDERTITLE, COLOR_YELLOW, COLOR_WHITE);
574
init_pair(MOJOCOLOR_TEXT, COLOR_BLACK, COLOR_WHITE);
May 27, 2007
May 27, 2007
575
init_pair(MOJOCOLOR_TEXTENTRY, COLOR_WHITE, COLOR_BLUE);
May 26, 2007
May 26, 2007
576
init_pair(MOJOCOLOR_BUTTONHOVER, COLOR_YELLOW, COLOR_BLUE);
577
init_pair(MOJOCOLOR_BUTTONNORMAL, COLOR_BLACK, COLOR_WHITE);
May 26, 2007
May 26, 2007
578
init_pair(MOJOCOLOR_BUTTONBORDER, COLOR_WHITE, COLOR_BLUE);
May 20, 2007
May 20, 2007
579
580
init_pair(MOJOCOLOR_DONE, COLOR_YELLOW, COLOR_RED);
init_pair(MOJOCOLOR_TODO, COLOR_CYAN, COLOR_BLUE);
May 20, 2007
May 20, 2007
581
582
583
584
585
586
587
588
589
590
591
592
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
593
594
freeBox(progressBox, false);
progressBox = NULL;
595
596
597
endwin();
delwin(stdscr); // not sure if this is safe, but valgrind said it leaks.
stdscr = NULL;
May 20, 2007
May 20, 2007
598
599
free(title);
title = NULL;
600
601
free(lastComponent);
lastComponent = NULL;
May 20, 2007
May 20, 2007
602
603
free(lastProgressType);
lastProgressType = NULL;
604
605
606
607
608
} // MojoGui_ncurses_deinit
static void MojoGui_ncurses_msgbox(const char *title, const char *text)
{
Mar 2, 2008
Mar 2, 2008
609
char *localized_ok = xstrdup(_("OK"));
610
611
612
613
614
615
616
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
617
618
static boolean MojoGui_ncurses_promptyn(const char *title, const char *text,
boolean defval)
619
{
Mar 2, 2008
Mar 2, 2008
620
621
char *localized_yes = xstrdup(_("Yes"));
char *localized_no = xstrdup(_("No"));
622
623
624
char *buttons[] = { localized_yes, localized_no };
MojoBox *mojobox = makeBox(title, text, buttons, 2, false, true);
int rc = 0;
Jul 2, 2007
Jul 2, 2007
625
626
627
628
629
630
631
632
633
634
635
// 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
636
637
638
639
640
641
642
643
644
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
645
646
const char *text,
boolean defval)
647
{
Mar 2, 2008
Mar 2, 2008
648
649
650
651
char *loc_yes = xstrdup(_("Yes"));
char *loc_no = xstrdup(_("No"));
char *loc_always = xstrdup(_("Always"));
char *loc_never = xstrdup(_("Never"));
652
653
654
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
655
656
657
658
659
660
661
662
663
664
665
// 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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
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
686
687
static boolean MojoGui_ncurses_start(const char *_title,
const MojoGuiSplash *splash)
688
{
May 20, 2007
May 20, 2007
689
free(title);
Mar 2, 2008
Mar 2, 2008
690
title = xstrdup(_title);
May 20, 2007
May 20, 2007
691
drawBackground(stdscr);
692
693
694
695
696
697
698
wrefresh(stdscr);
return true;
} // MojoGui_ncurses_start
static void MojoGui_ncurses_stop(void)
{
May 20, 2007
May 20, 2007
699
700
701
free(title);
title = NULL;
drawBackground(stdscr);
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
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
721
buttons[fwdbutton] = xstrdup(_("Next"));
722
723
724
725
726
} // if
if (can_back)
{
backbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
727
buttons[backbutton] = xstrdup(_("Back"));
728
729
} // if
Mar 2, 2008
Mar 2, 2008
730
buttons[bcount++] = xstrdup(_("Cancel"));
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
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
748
749
750
751
752
753
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
754
755
756
757
758
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
759
760
761
762
763
764
765
766
767
768
769
770
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
{
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
805
const char *desc = opts->description;
Mar 2, 2008
Mar 2, 2008
806
807
char *spacebuf = (char *) xmalloc(maxw + 1);
char *buf = (char *) xmalloc(maxw + 1);
May 20, 2007
May 20, 2007
808
int len = 0;
May 27, 2007
May 27, 2007
809
810
811
812
int spacing = level * 2;
if ((desc != NULL) && (*desc == '\0'))
desc = NULL;
May 20, 2007
May 20, 2007
813
814
815
816
817
818
819
820
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
821
822
823
824
{
if (desc != NULL)
len = snprintf(buf, maxw-2, "%s%s", spacebuf, desc);
} // if
May 20, 2007
May 20, 2007
825
826
827
828
else
{
(*line)++;
len = snprintf(buf, maxw-2, "%s[%c] %s", spacebuf,
May 27, 2007
May 27, 2007
829
opts->value ? 'X' : ' ', desc);
May 20, 2007
May 20, 2007
830
831
832
833
834
835
836
} // else
free(spacebuf);
if (len >= maxw-1)
strcpy(buf+(maxw-4), "..."); // !!! FIXME: Unicode issues!
May 27, 2007
May 27, 2007
837
838
839
if (len > 0)
{
const size_t newlen = strlen(*lines) + strlen(buf) + 2;
Mar 2, 2008
Mar 2, 2008
840
*lines = (char*) xrealloc(*lines, newlen);
May 27, 2007
May 27, 2007
841
842
843
strcat(*lines, buf);
strcat(*lines, "\n"); // I'm sorry, Joel Spolsky!
} // if
May 20, 2007
May 20, 2007
844
845
if ((opts->value) || (opts->is_group_parent))
May 27, 2007
May 27, 2007
846
847
848
849
850
851
852
{
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
853
854
855
856
857
build_options(opts->next_sibling, line, level, maxw, lines);
} // if
} // build_options
May 27, 2007
May 27, 2007
858
859
static int optionBox(const char *title, MojoGuiSetupOptions *opts,
boolean can_back, boolean can_fwd)
860
{
May 20, 2007
May 20, 2007
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
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
879
buttons[fwdbutton] = xstrdup(_("Next"));
May 20, 2007
May 20, 2007
880
881
882
883
884
} // if
if (can_back)
{
backbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
885
buttons[backbutton] = xstrdup(_("Back"));
May 20, 2007
May 20, 2007
886
887
888
} // if
lasthoverover = togglebutton = bcount++;
Mar 2, 2008
Mar 2, 2008
889
buttons[togglebutton] = xstrdup(_("Toggle"));
May 20, 2007
May 20, 2007
890
cancelbutton = bcount++;
Mar 2, 2008
Mar 2, 2008
891
buttons[cancelbutton] = xstrdup(_("Cancel"));
May 20, 2007
May 20, 2007
892
893
894
895
896
897
898
899
900
do
{
if (mojobox == NULL)
{
int y = 0;
int line = 0;
int maxw, maxh;
getmaxyx(stdscr, maxh, maxw);
Mar 2, 2008
Mar 2, 2008
901
char *text = xstrdup("");
May 27, 2007
May 27, 2007
902
build_options(opts, &line, 0, maxw-6, &text);
May 20, 2007
May 20, 2007
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
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
930
wattron(mojobox->textwin, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
931
932
mvwhline(mojobox->textwin, y, 0, ' ', maxw);
mvwaddstr(mojobox->textwin, y, 0, mojobox->textlines[selected]);
May 26, 2007
May 26, 2007
933
wattroff(mojobox->textwin, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
934
935
936
937
938
939
940
941
942
943
944
945
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
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
976
wattron(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
977
978
mvwhline(win, y, 0, ' ', maxw);
mvwaddstr(win, y, 0, mojobox->textlines[selected]);
May 26, 2007
May 26, 2007
979
wattroff(win, COLOR_PAIR(MOJOCOLOR_BUTTONHOVER) | A_BOLD);
May 20, 2007
May 20, 2007
980
981
982
983
984
985
986
987
988
989
990
991
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;
int maxw, maxh;
int y = ++selected - mojobox->textpos;
getmaxyx(win, maxh, maxw);
if (selected >= mojobox->textpos+maxh)
{
upkeepBox(&mojobox, ch); // upkeepBox does scrolling
y--;
} // if
else
{
wattron(win, COLOR_PAIR(MOJOCOLOR_TEXT));
mvwhline(win, y-1, 0, ' ', maxw);