forked from len0rd/rockbox
Simplify code for set time and date screen
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15756 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
3e2c51d82c
commit
7567e99d61
1 changed files with 75 additions and 102 deletions
133
apps/screens.c
133
apps/screens.c
|
|
@ -80,6 +80,15 @@
|
||||||
#define SCROLLBAR_WIDTH 6
|
#define SCROLLBAR_WIDTH 6
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static int clamp_value_wrap(int value, int max, int min)
|
||||||
|
{
|
||||||
|
if (value > max)
|
||||||
|
return min;
|
||||||
|
if (value < min)
|
||||||
|
return max;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
void usb_screen(void)
|
void usb_screen(void)
|
||||||
{
|
{
|
||||||
#ifdef USB_NONE
|
#ifdef USB_NONE
|
||||||
|
|
@ -827,29 +836,26 @@ static void say_time(int cursorpos, const struct tm *tm)
|
||||||
|
|
||||||
#define INDEX_X 0
|
#define INDEX_X 0
|
||||||
#define INDEX_Y 1
|
#define INDEX_Y 1
|
||||||
#define INDEX_WIDTH 2
|
|
||||||
|
|
||||||
#define SEPARATOR ":"
|
#define SEPARATOR ":"
|
||||||
bool set_time_screen(const char* title, struct tm *tm)
|
bool set_time_screen(const char* title, struct tm *tm)
|
||||||
{
|
{
|
||||||
bool done = false;
|
bool done = false;
|
||||||
int button;
|
int button;
|
||||||
unsigned int i, s;
|
unsigned int i, j, s;
|
||||||
unsigned int cursorpos = 0;
|
int cursorpos = 0;
|
||||||
unsigned int lastcursorpos = 1;
|
|
||||||
unsigned int julianday;
|
unsigned int julianday;
|
||||||
unsigned int realyear;
|
unsigned int realyear;
|
||||||
unsigned int width;
|
unsigned int width;
|
||||||
unsigned int min = 0, steps = 0;
|
unsigned int min, max;
|
||||||
unsigned int statusbar_height = 0;
|
unsigned int statusbar_height = 0;
|
||||||
unsigned int separator_width, weekday_width;
|
unsigned int separator_width, weekday_width;
|
||||||
unsigned int line_height, prev_line_height;
|
unsigned int prev_line_height;
|
||||||
unsigned char daysinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
int daysinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||||
unsigned char buffer[20];
|
unsigned char buffer[20];
|
||||||
|
|
||||||
/* 6 possible cursor possitions, 3 values stored for each: x, y, width */
|
/* 6 possible cursor possitions, 2 values stored for each: x, y */
|
||||||
unsigned char cursor[6][3];
|
unsigned int cursor[6][2];
|
||||||
memset(cursor, 0, sizeof(cursor));
|
|
||||||
|
|
||||||
int *valptr = NULL;
|
int *valptr = NULL;
|
||||||
unsigned char *ptr[6];
|
unsigned char *ptr[6];
|
||||||
|
|
@ -857,6 +863,9 @@ bool set_time_screen(const char* title, struct tm *tm)
|
||||||
if(global_settings.statusbar)
|
if(global_settings.statusbar)
|
||||||
statusbar_height = STATUSBAR_HEIGHT;
|
statusbar_height = STATUSBAR_HEIGHT;
|
||||||
|
|
||||||
|
/* speak selection when screen is entered */
|
||||||
|
say_time(cursorpos, tm);
|
||||||
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
/* for easy acess in the drawing loop */
|
/* for easy acess in the drawing loop */
|
||||||
ptr[0] = buffer; /* hours */
|
ptr[0] = buffer; /* hours */
|
||||||
|
|
@ -878,11 +887,11 @@ bool set_time_screen(const char* title, struct tm *tm)
|
||||||
tm->tm_mday = daysinmonth[tm->tm_mon];
|
tm->tm_mday = daysinmonth[tm->tm_mon];
|
||||||
|
|
||||||
/* calculate day of week */
|
/* calculate day of week */
|
||||||
julianday = 0;
|
julianday = tm->tm_mday;
|
||||||
for(i = 0; (int)i < tm->tm_mon; i++) {
|
for(i = 0; (int)i < tm->tm_mon; i++) {
|
||||||
julianday += daysinmonth[i];
|
julianday += daysinmonth[i];
|
||||||
}
|
}
|
||||||
julianday += tm->tm_mday;
|
|
||||||
tm->tm_wday = (realyear + julianday + (realyear - 1) / 4 -
|
tm->tm_wday = (realyear + julianday + (realyear - 1) / 4 -
|
||||||
(realyear - 1) / 100 + (realyear - 1) / 400 + 7 - 1) % 7;
|
(realyear - 1) / 100 + (realyear - 1) / 400 + 7 - 1) % 7;
|
||||||
|
|
||||||
|
|
@ -916,49 +925,25 @@ bool set_time_screen(const char* title, struct tm *tm)
|
||||||
screens[s].getstringsize(title, NULL, &prev_line_height);
|
screens[s].getstringsize(title, NULL, &prev_line_height);
|
||||||
else
|
else
|
||||||
prev_line_height = 0;
|
prev_line_height = 0;
|
||||||
screens[s].getstringsize(buffer, NULL, &line_height);
|
|
||||||
screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
|
screens[s].getstringsize(SEPARATOR, &separator_width, NULL);
|
||||||
|
|
||||||
|
|
||||||
/* get width for each string except the last one and put them
|
|
||||||
in the cursor array */
|
|
||||||
for(i=0; i < 5; i++)
|
|
||||||
{
|
|
||||||
screens[s].getstringsize(ptr[i], &width, NULL);
|
|
||||||
cursor[i][INDEX_WIDTH] = width;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* hour */
|
|
||||||
/* cursor[0][INDEX_X] is already 0 because of the memset */
|
|
||||||
cursor[0][INDEX_Y] = prev_line_height + statusbar_height;
|
|
||||||
|
|
||||||
/* minute */
|
|
||||||
cursor[1][INDEX_X] = cursor[0][INDEX_WIDTH] + separator_width;
|
|
||||||
cursor[1][INDEX_Y] = prev_line_height + statusbar_height;
|
|
||||||
|
|
||||||
/* second */
|
|
||||||
cursor[2][INDEX_X] = cursor[0][INDEX_WIDTH] + separator_width +
|
|
||||||
cursor[1][INDEX_WIDTH] + separator_width;
|
|
||||||
cursor[2][INDEX_Y] = prev_line_height + statusbar_height;
|
|
||||||
|
|
||||||
/* weekday */
|
/* weekday */
|
||||||
screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday), &weekday_width, NULL);
|
screens[s].getstringsize(str(LANG_WEEKDAY_SUNDAY + tm->tm_wday), &weekday_width, NULL);
|
||||||
screens[s].getstringsize(" ", &separator_width, NULL);
|
screens[s].getstringsize(" ", &separator_width, NULL);
|
||||||
|
|
||||||
/* year */
|
for(i=0, j=0; i < 6; i++)
|
||||||
cursor[3][INDEX_X] = weekday_width + separator_width;
|
{
|
||||||
cursor[3][INDEX_Y] = cursor[0][INDEX_Y] + prev_line_height;
|
if(i==3) /* second row */
|
||||||
|
{
|
||||||
/* month */
|
j = weekday_width + separator_width;;
|
||||||
cursor[4][INDEX_X] = weekday_width + 2 * separator_width +
|
prev_line_height *= 2;
|
||||||
cursor[3][INDEX_WIDTH];
|
}
|
||||||
cursor[4][INDEX_Y] = cursor[0][INDEX_Y] + prev_line_height;
|
screens[s].getstringsize(ptr[i], &width, NULL);
|
||||||
|
cursor[i][INDEX_Y] = prev_line_height + statusbar_height;
|
||||||
/* day */
|
cursor[i][INDEX_X] = j;
|
||||||
cursor[5][INDEX_X] = weekday_width + 3 * separator_width +
|
j += width + separator_width;
|
||||||
cursor[3][INDEX_WIDTH] +
|
}
|
||||||
cursor[4][INDEX_WIDTH];
|
|
||||||
cursor[5][INDEX_Y] = cursor[0][INDEX_Y] + prev_line_height;
|
|
||||||
|
|
||||||
/* draw the screen */
|
/* draw the screen */
|
||||||
screens[s].set_drawmode(DRMODE_SOLID);
|
screens[s].set_drawmode(DRMODE_SOLID);
|
||||||
|
|
@ -968,23 +953,22 @@ bool set_time_screen(const char* title, struct tm *tm)
|
||||||
|
|
||||||
/* these are not selectable, so we draw them outside the loop */
|
/* these are not selectable, so we draw them outside the loop */
|
||||||
screens[s].putsxy(0, cursor[3][INDEX_Y], str(LANG_WEEKDAY_SUNDAY + tm->tm_wday)); /* name of the week day */
|
screens[s].putsxy(0, cursor[3][INDEX_Y], str(LANG_WEEKDAY_SUNDAY + tm->tm_wday)); /* name of the week day */
|
||||||
screens[s].putsxy(cursor[1][INDEX_X] - separator_width,
|
|
||||||
cursor[0][INDEX_Y], SEPARATOR);
|
|
||||||
screens[s].putsxy(cursor[2][INDEX_X] - separator_width,
|
|
||||||
cursor[0][INDEX_Y], SEPARATOR);
|
|
||||||
|
|
||||||
/* draw the selected item with drawmode set to
|
/* draw the selected item with drawmode set to
|
||||||
DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
|
DRMODE_SOLID|DRMODE_INVERSEVID, all other selectable
|
||||||
items with drawmode DRMODE_SOLID */
|
items with drawmode DRMODE_SOLID */
|
||||||
for(i=0; i<6; i++)
|
for(i=0; i<6; i++)
|
||||||
{
|
{
|
||||||
if (cursorpos == i)
|
if (cursorpos == (int)i)
|
||||||
screens[s].set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
|
screens[s].set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
|
||||||
else
|
|
||||||
screens[s].set_drawmode(DRMODE_SOLID);
|
|
||||||
|
|
||||||
screens[s].putsxy(cursor[i][INDEX_X],
|
screens[s].putsxy(cursor[i][INDEX_X],
|
||||||
cursor[i][INDEX_Y], ptr[i]);
|
cursor[i][INDEX_Y], ptr[i]);
|
||||||
|
|
||||||
|
screens[s].set_drawmode(DRMODE_SOLID);
|
||||||
|
|
||||||
|
screens[s].putsxy(cursor[i/4 +1][INDEX_X] - separator_width,
|
||||||
|
cursor[0][INDEX_Y], SEPARATOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print help text */
|
/* print help text */
|
||||||
|
|
@ -996,66 +980,55 @@ bool set_time_screen(const char* title, struct tm *tm)
|
||||||
}
|
}
|
||||||
gui_syncstatusbar_draw(&statusbars, true);
|
gui_syncstatusbar_draw(&statusbars, true);
|
||||||
|
|
||||||
|
/* set the most common numbers */
|
||||||
|
min = 0;
|
||||||
|
max = 59;
|
||||||
/* calculate the minimum and maximum for the number under cursor */
|
/* calculate the minimum and maximum for the number under cursor */
|
||||||
if(cursorpos!=lastcursorpos) {
|
|
||||||
lastcursorpos=cursorpos;
|
|
||||||
switch(cursorpos) {
|
switch(cursorpos) {
|
||||||
case 0: /* hour */
|
case 0: /* hour */
|
||||||
min = 0;
|
max = 23;
|
||||||
steps = 24;
|
|
||||||
valptr = &tm->tm_hour;
|
valptr = &tm->tm_hour;
|
||||||
break;
|
break;
|
||||||
case 1: /* minute */
|
case 1: /* minute */
|
||||||
min = 0;
|
|
||||||
steps = 60;
|
|
||||||
valptr = &tm->tm_min;
|
valptr = &tm->tm_min;
|
||||||
break;
|
break;
|
||||||
case 2: /* second */
|
case 2: /* second */
|
||||||
min = 0;
|
|
||||||
steps = 60;
|
|
||||||
valptr = &tm->tm_sec;
|
valptr = &tm->tm_sec;
|
||||||
break;
|
break;
|
||||||
case 3: /* year */
|
case 3: /* year */
|
||||||
min = 1;
|
min = 1;
|
||||||
steps = 200;
|
max = 200;
|
||||||
valptr = &tm->tm_year;
|
valptr = &tm->tm_year;
|
||||||
break;
|
break;
|
||||||
case 4: /* month */
|
case 4: /* month */
|
||||||
min = 0;
|
max = 11;
|
||||||
steps = 12;
|
|
||||||
valptr = &tm->tm_mon;
|
valptr = &tm->tm_mon;
|
||||||
break;
|
break;
|
||||||
case 5: /* day */
|
case 5: /* day */
|
||||||
min = 1;
|
min = 1;
|
||||||
steps = daysinmonth[tm->tm_mon];
|
max = daysinmonth[tm->tm_mon];
|
||||||
valptr = &tm->tm_mday;
|
valptr = &tm->tm_mday;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
say_time(cursorpos, tm);
|
|
||||||
}
|
|
||||||
|
|
||||||
button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
|
button = get_action(CONTEXT_SETTINGS_TIME, TIMEOUT_BLOCK);
|
||||||
switch ( button ) {
|
switch ( button ) {
|
||||||
case ACTION_STD_PREV:
|
case ACTION_STD_PREV:
|
||||||
cursorpos = (cursorpos + 6 - 1) % 6;
|
cursorpos = clamp_value_wrap(--cursorpos, 5, 0);
|
||||||
|
say_time(cursorpos, tm);
|
||||||
break;
|
break;
|
||||||
case ACTION_STD_NEXT:
|
case ACTION_STD_NEXT:
|
||||||
cursorpos = (cursorpos + 6 + 1) % 6;
|
cursorpos = clamp_value_wrap(++cursorpos, 5, 0);
|
||||||
|
say_time(cursorpos, tm);
|
||||||
break;
|
break;
|
||||||
case ACTION_SETTINGS_INC:
|
case ACTION_SETTINGS_INC:
|
||||||
case ACTION_SETTINGS_INCREPEAT:
|
case ACTION_SETTINGS_INCREPEAT:
|
||||||
*valptr = (*valptr + steps - min + 1) %
|
*valptr = clamp_value_wrap(++(*valptr), max, min);
|
||||||
steps + min;
|
|
||||||
if(*valptr == 0)
|
|
||||||
*valptr = min;
|
|
||||||
say_time(cursorpos, tm);
|
say_time(cursorpos, tm);
|
||||||
break;
|
break;
|
||||||
case ACTION_SETTINGS_DEC:
|
case ACTION_SETTINGS_DEC:
|
||||||
case ACTION_SETTINGS_DECREPEAT:
|
case ACTION_SETTINGS_DECREPEAT:
|
||||||
*valptr = (*valptr + steps - min - 1) %
|
*valptr = clamp_value_wrap(--(*valptr), max, min);
|
||||||
steps + min;
|
|
||||||
if(*valptr == 0)
|
|
||||||
*valptr = min;
|
|
||||||
say_time(cursorpos, tm);
|
say_time(cursorpos, tm);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue