Skip to content

Latest commit

 

History

History
1487 lines (1266 loc) · 43.6 KB

gui_ncurses.c

File metadata and controls

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