libui/examples/datetime/main.c

129 lines
2.5 KiB
C
Raw Normal View History

2018-05-13 12:00:42 -05:00
#include <stdio.h>
2018-02-20 00:39:44 -06:00
#include <stdlib.h>
2018-05-13 12:00:42 -05:00
#include <string.h>
2018-02-20 00:39:44 -06:00
#include <time.h>
#include "../../ui.h"
uiDateTimePicker *dtboth, *dtdate, *dttime;
2018-05-05 23:21:25 -05:00
const char *timeFormat(uiDateTimePicker *d)
2018-02-20 00:39:44 -06:00
{
const char *fmt;
if (d == dtboth)
fmt = "%c";
else if (d == dtdate)
fmt = "%x";
else if (d == dttime)
fmt = "%X";
else
fmt = "";
return fmt;
}
void onChanged(uiDateTimePicker *d, void *data)
{
struct tm time;
char buf[64];
uiDateTimePickerTime(d, &time);
2018-05-05 23:21:25 -05:00
strftime(buf, sizeof (buf), timeFormat(d), &time);
2018-02-20 00:39:44 -06:00
uiLabelSetText(uiLabel(data), buf);
}
void onClicked(uiButton *b, void *data)
{
intptr_t now;
time_t t;
struct tm tmbuf;
now = (intptr_t) data;
t = 0;
if (now)
t = time(NULL);
tmbuf = *localtime(&t);
if (now) {
uiDateTimePickerSetTime(dtdate, &tmbuf);
uiDateTimePickerSetTime(dttime, &tmbuf);
2018-05-05 23:21:25 -05:00
} else
2018-02-20 00:39:44 -06:00
uiDateTimePickerSetTime(dtboth, &tmbuf);
}
int onClosing(uiWindow *w, void *data)
{
uiQuit();
return 1;
}
int main(void)
{
uiInitOptions o;
const char *err;
2018-02-20 00:39:44 -06:00
uiWindow *w;
uiGrid *g;
uiLabel *l;
uiButton *b;
memset(&o, 0, sizeof (uiInitOptions));
err = uiInit(&o);
if (err != NULL) {
fprintf(stderr, "error initializing ui: %s\n", err);
uiFreeInitError(err);
return 1;
}
2018-02-20 00:39:44 -06:00
w = uiNewWindow("Date / Time", 320, 240, 0);
uiWindowSetMargined(w, 1);
g = uiNewGrid();
uiGridSetPadded(g, 1);
uiWindowSetChild(w, uiControl(g));
dtboth = uiNewDateTimePicker();
dtdate = uiNewDatePicker();
dttime = uiNewTimePicker();
uiGridAppend(g, uiControl(dtboth),
0, 0, 2, 1,
1, uiAlignFill, 0, uiAlignFill);
uiGridAppend(g, uiControl(dtdate),
0, 1, 1, 1,
1, uiAlignFill, 0, uiAlignFill);
uiGridAppend(g, uiControl(dttime),
1, 1, 1, 1,
1, uiAlignFill, 0, uiAlignFill);
l = uiNewLabel("");
uiGridAppend(g, uiControl(l),
2018-02-21 23:26:43 -06:00
0, 2, 2, 1,
2018-02-20 00:39:44 -06:00
1, uiAlignCenter, 0, uiAlignFill);
uiDateTimePickerOnChanged(dtboth, onChanged, l);
l = uiNewLabel("");
uiGridAppend(g, uiControl(l),
2018-02-21 23:26:43 -06:00
0, 3, 1, 1,
2018-02-20 00:39:44 -06:00
1, uiAlignCenter, 0, uiAlignFill);
uiDateTimePickerOnChanged(dtdate, onChanged, l);
l = uiNewLabel("");
uiGridAppend(g, uiControl(l),
2018-02-21 23:26:43 -06:00
1, 3, 1, 1,
2018-02-20 00:39:44 -06:00
1, uiAlignCenter, 0, uiAlignFill);
uiDateTimePickerOnChanged(dttime, onChanged, l);
b = uiNewButton("Now");
uiButtonOnClicked(b, onClicked, (void *) 1);
uiGridAppend(g, uiControl(b),
2018-02-21 23:26:43 -06:00
0, 4, 1, 1,
1, uiAlignFill, 1, uiAlignEnd);
2018-02-20 00:39:44 -06:00
b = uiNewButton("Unix epoch");
uiButtonOnClicked(b, onClicked, (void *) 0);
uiGridAppend(g, uiControl(b),
2018-02-21 23:26:43 -06:00
1, 4, 1, 1,
1, uiAlignFill, 1, uiAlignEnd);
2018-02-20 00:39:44 -06:00
uiWindowOnClosing(w, onClosing, NULL);
uiControlShow(uiControl(w));
uiMain();
return 0;
}