Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bunch more work on GUI options stuff.
  • Loading branch information
icculus committed Dec 27, 2006
1 parent 86074bf commit 637dd05
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 80 deletions.
1 change: 1 addition & 0 deletions gui.h
Expand Up @@ -29,6 +29,7 @@ struct MojoGuiSetupOptions
boolean value;
boolean is_group_parent;
uint64 size;
int opaque; // GUI drivers shouldn't touch this.
MojoGuiSetupOptions *next_sibling;
MojoGuiSetupOptions *child;
};
Expand Down
208 changes: 172 additions & 36 deletions gui/gui_stdio.c
Expand Up @@ -13,58 +13,101 @@ CREATE_MOJOGUI_ENTRY_POINT(stdio)

#include <ctype.h>

static int read_stdin(char *buf, int len)
{
if (fgets(buf, len, stdin) == NULL)
return -1;

len = strlen(buf) - 1;
while ( (len >= 0) && ((buf[len] == '\n') || (buf[len] == '\r')) )
buf[len--] = '\0';

return len+1;
} // read_stdin


static int readstr(const char *prompt, char *buf, int len, boolean back)
{
int retval = 0;
const char *backstr = entry->xstrdup(entry->_("back"));

if (prompt != NULL)
printf("%s\n", prompt);

if (back)
{
printf(entry->_("Type '%s' to go back."), backstr);
printf("\n");
} // if

printf(entry->_("Press enter to continue."), backstr);
printf("\n");
printf(entry->_("> "));
fflush(stdout);

if ((retval = read_stdin(buf, len)) >= 0)
{
if (strcmp(buf, backstr) == 0)
retval = -1;
} // if

free((void *) backstr);
return retval;
} // print_prompt


static uint8 MojoGui_stdio_priority(void)
{
return MOJOGUI_PRIORITY_TRY_LAST;
}
return MOJOGUI_PRIORITY_TRY_LAST; // always a last resort.
} // MojoGui_stdio_priority


static boolean MojoGui_stdio_init(void)
{
return true;
}
return true; // always succeeds.
} // MojoGui_stdio_init


static void MojoGui_stdio_deinit(void)
{
// no-op
}
} // MojoGui_stdio_deinit


static void MojoGui_stdio_msgbox(const char *title, const char *text)
{
char buf[128];
printf(entry->_("NOTICE: %s\n[hit enter]"), text);
fflush(stdout);
getchar();
read_stdin(buf, sizeof (buf));
} // MojoGui_stdio_msgbox


static boolean MojoGui_stdio_promptyn(const char *title, const char *text)
{
if (feof(stdin))
return 0;
else
boolean retval = false;
if (!feof(stdin))
{
const char *localized_no = entry->_("N");
const char *localized_yes = entry->_("Y");
const char *localized_no = entry->xstrdup(entry->_("N"));
const char *localized_yes = entry->xstrdup(entry->_("Y"));
boolean getout = false;
char buf[128];
size_t len = 0;
while (1)
while (!getout)
{
printf(entry->_("%s\n[y/n]: "), text);
fflush(stdout);

if (fgets(buf, sizeof (buf), stdin) == NULL)
return 0;

len = strlen(buf) - 1;
while ( (len >= 0) && ((buf[len] == '\n') || (buf[len] == '\r')) )
buf[len--] = '\0';

if (strcasecmp(buf, localized_no) == 0)
return 0;
if (read_stdin(buf, sizeof (buf)) < 0)
getout = true;
else if (strcasecmp(buf, localized_no) == 0)
getout = true;
else if (strcasecmp(buf, localized_yes) == 0)
return 1;
retval = getout = true;
} // while
} // else
free((void *) localized_no);
free((void *) localized_yes);
} // if

return 0;
return retval;
} // MojoGui_stdio_promptyn

static boolean MojoGui_stdio_start(const char *title, const char *splash)
Expand All @@ -81,32 +124,91 @@ static void MojoGui_stdio_stop(void)


static boolean MojoGui_stdio_readme(const char *name, const uint8 *data,
size_t len, boolean can_go_back,
size_t datalen, boolean can_go_back,
boolean can_go_forward)
{
printf("%s\n%s\n", name, (const char *) data);
return true;
boolean getout = false;
boolean retval = false;
int len = 0;
char buf[128];

while (!getout)
{
printf("%s\n%s\n", name, (const char *) data);
if ((len = readstr(NULL, buf, sizeof (buf), can_go_back)) < 0)
getout = true;
else if (len == 0)
getout = retval = true;
} // while

return retval;
} // MojoGui_stdio_readme


static void toggle_option(MojoGuiSetupOptions *parent,
MojoGuiSetupOptions *opts, int *line, int target)
{
if ((opts != NULL) && (target > *line))
{
if (!opts->is_group_parent)
{
if (++(*line) == target)
{
const boolean toggled = ((opts->value) ? false : true);

// "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; // we found it, bail.
} // if
} // if

if (opts->value) // if option is toggled on, descend to children.
toggle_option(opts, opts->child, line, target);
toggle_option(parent, opts->next_sibling, line, target);
} // if
} // toggle_option


static void print_options(MojoGuiSetupOptions *opts, int *line, int level)
{
if (opts != NULL)
{
int i;
int spacing = 1;
if (opts->is_group_parent)
spacing += 10;
spacing += 7;
else
{
(*line)++;
printf("%2d [%c]", *line, opts->value ? 'X' : ' ');
} // else

for (i = 0; i < (level + spacing); i++)
for (i = 0; i < ((level*2) + spacing); i++)
putchar(' ');

puts(opts->description);

(*line)++;
print_options(opts->child, line, level+1);
if (opts->value)
print_options(opts->child, line, level+1);
print_options(opts->next_sibling, line, level);
} // if
} // print_options
Expand All @@ -115,9 +217,43 @@ static void print_options(MojoGuiSetupOptions *opts, int *line, int level)
static boolean MojoGui_stdio_options(MojoGuiSetupOptions *opts,
boolean can_go_back, boolean can_go_forward)
{
int line = 0;
print_options(opts, &line, 0);
return true;
const char *prompt = entry->xstrdup(entry->_("Choose number to change."));
const char *inst_opts_str = entry->xstrdup(entry->_("Install options:"));
boolean retval = false;
boolean getout = false;
char buf[128];
int len = 0;

while (!getout)
{
int line = 0;

printf("\n\n");
printf(inst_opts_str);
printf("\n");
print_options(opts, &line, 1);
printf("\n");

if ((len = readstr(prompt, buf, sizeof (buf), can_go_back)) < 0)
getout = true;
else if (len == 0)
retval = getout = true;
else
{
char *endptr = NULL;
int target = (int) strtol(buf, &endptr, 10);
if (*endptr == '\0') // complete string was a valid number?
{
line = 0;
toggle_option(NULL, opts, &line, target);
} // if
} // else
} // while

free((void *) inst_opts_str);
free((void *) prompt);

return retval;
} // MojoGui_stdio_options

// end of gui_stdio.c ...
Expand Down
13 changes: 10 additions & 3 deletions lua/config.lua
Expand Up @@ -72,7 +72,7 @@ Setup.Package
Setup.Option
{
value = true,
required = false,
required = true,
disabled = false,
size = "600M",
description = "Base Install",
Expand All @@ -97,18 +97,25 @@ Setup.Package
description = "Language",
Setup.Option
{
value = string.match(MojoSetup.locale, "^en_") ~= nil,
value = true, --string.match(MojoSetup.locale, "^en_") ~= nil,
size = "10M",
description = "English",
Setup.File { source="Lang/English.zip" },
},
Setup.Option
{
value = string.match(MojoSetup.locale, "^fr_") ~= nil,
value = true, --string.match(MojoSetup.locale, "^fr_") ~= nil,
size = "10M",
description = "French",
Setup.File { source="Lang/French.zip" },
},
Setup.Option
{
value = true, --string.match(MojoSetup.locale, "^de_") ~= nil,
size = "10M",
description = "German",
Setup.File { source="Lang/German.zip" },
},
},
},
}
Expand Down
20 changes: 20 additions & 0 deletions lua/localization.lua
Expand Up @@ -56,6 +56,26 @@ MojoSetup.localization = {
["No"] = {
};

["Press enter to continue."] = {
};

["Choose number to change."] = {
};

["Type '%s' to go back."] = {
};

-- This is the string used for the '%s' in the above string.
["back"] = {
};

-- This is the prompt in the stdio driver when user input is expected.
["> "] = {
};

["Install options:"] = {
};

["Nothing to do!"] = {
};

Expand Down
9 changes: 5 additions & 4 deletions lua/mojosetup_init.lua
Expand Up @@ -225,10 +225,11 @@ end

local function reform_schema_table(tab)
for k,v in pairs(tab) do
local typestr = type(k)
if (typestr == "number") and (v._type_ ~= nil) then
local ktype = type(k)
local vtype = type(v)
if (ktype == "number") and (vtype == "table") and (v._type_ ~= nil) then
-- add element to proper named array.
typestr = v._type_
local typestr = v._type_
v._type_ = nil
MojoSetup.logdebug("schema: reforming '" .. typestr .. "', '" .. k .. "'")
if tab[typestr] == nil then
Expand All @@ -237,7 +238,7 @@ local function reform_schema_table(tab)
table.insert(tab[typestr], v)
end
tab[k] = nil
elseif typestr == "table" then
elseif vtype == "table" then
tab[k] = reform_schema_table(v)
end
end
Expand Down
7 changes: 5 additions & 2 deletions lua/mojosetup_mainline.lua
Expand Up @@ -87,7 +87,10 @@ local function do_install(install)
-- (options) becomes an upvalue in this function.
stages[#stages+1] = function(thisstage, maxstage)
-- This does some complex stuff with a hierarchy of tables in C.
return MojoSetup.gui.options(options, thisstage, maxstage)
--return MojoSetup.gui.options(options, thisstage, maxstage)
local rc = MojoSetup.gui.options(options, thisstage, maxstage)
MojoSetup.dumptable("options", options)
return rc
end
end

Expand Down Expand Up @@ -120,7 +123,7 @@ local function do_install(install)
i = i + 1
else
if i == 1 then
MojoSetup.logWarning("Stepped back over start of stages")
MojoSetup.logwarning("Stepped back over start of stages")
MojoSetup.fatal(_("Internal error"))
else
i = i - 1
Expand Down

0 comments on commit 637dd05

Please sign in to comment.