home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ARM Club 1
/
ARM_CLUB_CD.iso
/
contents
/
education
/
a
/
biology1
/
!Biology1
/
c
/
Choices
next >
Wrap
Text File
|
1991-12-03
|
5KB
|
184 lines
/* Choices.c */
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "wimp.h"
#include "wimpt.h"
#include "data.h"
#include "input.h"
#include "keys.h"
#include "texticon.h"
#include "utils.h"
extern int correct_ans_num;
extern int standard_line;
extern subtopic *test;
typedef struct
{ wimp_i icon;
char *string;
} text_icon;
typedef struct
{ struct choice *next;
struct text *choice;
} choice;
struct iconlist
{ struct iconlist *next;
wimp_i handle;
char *text;
};
/* Capitalizes 1st char, lower case the rest & returns str */
char *set_case (char *str)
{ char *ptr = str;
while (*ptr && !isalpha(*ptr))
ptr++;
*ptr = toupper (*ptr);
while (*ptr)
{ ptr++;
*ptr = tolower (*ptr);
}
return (str);
}
int ct_lines (text t)
{ int i, no;
for (i=0, no=1; t[i] != '\0'; i++)
if (t[i] == '\n')
no++;
return (no);
}
int max_wide (text t, int lines)
{ int i, j, max;
if (lines == 1)
return (strlen (t));
for (i = max = 0; i < lines; i++)
{ for (j = 0; t[j] != '\0'; j++)
{ if (t[j] == '\n')
max = max (j, max);
}
}
return (max);
}
void make_icon_ch (text_icon *res, wimp_w w, int line, text t,
int lines, int width, BOOL force_redraw)
/* Some dodgy 'hardwiring' in here */
{ wimp_icreate ic;
res->string = t;
ic.w = w;
ic.i.box.x0 = 4 + (standard_line - width) * 8 ; /* centred */
ic.i.box.x1 = ic.i.box.x0 + (width * 8 + 4) * 2;
ic.i.box.y1 = - line * 8 * 4;
ic.i.box.y0 = ic.i.box.y1 - lines * 40 - 12;
ic.i.flags = wimp_ITEXT | wimp_IBORDER | wimp_IFILLED |
wimp_INDIRECT | wimp_IVCENTRE | wimp_IHCENTRE |
wimp_IBTYPE * wimp_BCLICKDEBOUNCE |
wimp_IFORECOL * 7 |
15 << 28; /* wimp_IBACKCOL * 15 signed const overflow */
ic.i.data.indirecttext.buffer = res->string;
ic.i.data.indirecttext.validstring = "L40";
ic.i.data.indirecttext.bufflen = strlen (res->string) + 1;
wimpt_noerr (texticon_create_icon (&ic, &res->icon));
if (force_redraw)
{ wimp_redrawstr r;
r.w = w;
memcpy (&r.box, &ic.i.box, sizeof (wimp_box));
wimpt_noerr (wimp_force_redraw (&r));
}
}
void *prompt_choice (wimp_w wnd, int line, question *curr_quest,
int quest_no, int test_type)
{ int ct, limit, range, ch_line;
int max_lines, max_width, width, lines, text_width[5];
int chosen[5];
BOOL choice_displayed[10];
text_icon choice_item[5];
text choice_str;
text temp_ans;
wimp_i ch_icon[5];
for (ct = 0; ct < 10; choice_displayed[ct++] = FALSE)
;
if ( test_type == 3)
{ range = 10;
limit = 5;
choice_displayed[quest_no] = TRUE; /* flag current answer as displayed */
}
else
{ range = curr_quest->no_choice;
limit = range + 1;
}
correct_ans_num = rand () % limit;
if (test_type == 7)
{ temp_ans = malloc (strlen (curr_quest->ans) + 1);
strcpy (temp_ans, curr_quest->ans);
set_case (curr_quest->ans);
}
max_lines = ct_lines (curr_quest->ans);
max_width = max_wide (curr_quest->ans, max_lines);
text_width[correct_ans_num] = max_width;
for (ct = 0; ct < limit; ct++)
{ if (ct != correct_ans_num)
{ do {
chosen[ct] = rand () % range;
} while (choice_displayed[chosen[ct]]);
choice_displayed[chosen[ct]] = TRUE;
if (range == 10)
choice_str = test->question[chosen[ct]].ans;
else
choice_str = curr_quest->choice[chosen[ct]];
lines = ct_lines (choice_str);
max_lines = max (lines, max_lines);
width = max_wide (choice_str, lines);
max_width = max (width, max_width);
text_width[ct] = width;
}
}
if (max_width > 25)
{ for (ct = 0; ct < limit; text_width[ct++] = max_width)
;
}
for (ct = 0, ch_line = line; ct < limit; ct++)
{ if (ct == correct_ans_num)
{ make_icon_ch (&choice_item[ct], wnd, ch_line, curr_quest->ans,
max_lines, text_width[ct], FALSE);
}
else
{ if (range == 10)
choice_str = test->question[chosen[ct]].ans;
else
choice_str = curr_quest->choice[chosen[ct]];
make_icon_ch (&choice_item[ct], wnd, ch_line, choice_str,
max_lines, text_width[ct], FALSE);
}
ch_line += 3;
ch_icon[ct] = choice_item[ct].icon;
input_icon (wnd, ch_icon[ct], NULL, -100+ct);
}
if (test_type == 7)
{ strcpy (curr_quest->ans, temp_ans); /* restore value */
free (temp_ans);
}
return (concat_choice_wakeup (wnd, ch_icon, limit));
}