Add uiTimer() API and example

This commit is contained in:
cody271 2017-08-19 15:32:52 -07:00
parent f3fcf9918b
commit 27b8ce1db3
6 changed files with 85 additions and 1 deletions

View File

@ -243,3 +243,7 @@ void uiQueueMain(void (*f)(void *data), void *data)
// the signature of f matches dispatch_function_t
dispatch_async_f(dispatch_get_main_queue(), data, f);
}
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
{
}

View File

@ -42,9 +42,15 @@ _add_example(drawtext
${_EXAMPLE_RESOURCES_RC}
)
_add_example(timer
timer/main.c
${_EXAMPLE_RESOURCES_RC}
)
add_custom_target(examples
DEPENDS
controlgallery
histogram
cpp-multithread
drawtext)
drawtext
timer)

64
examples/timer/main.c Normal file
View File

@ -0,0 +1,64 @@
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "../../ui.h"
uiMultilineEntry *e;
int sayTime(void *data)
{
time_t t;
char *s;
t = time(NULL);
s = ctime(&t);
uiMultilineEntryAppend(e, s);
return 1;
}
int onClosing(uiWindow *w, void *data)
{
uiQuit();
return 1;
}
void saySomething(uiButton *b, void *data)
{
uiMultilineEntryAppend(e, "Saying something\n");
}
int main(void)
{
uiInitOptions o;
uiWindow *w;
uiBox *b;
uiButton *btn;
memset(&o, 0, sizeof (uiInitOptions));
if (uiInit(&o) != NULL)
abort();
w = uiNewWindow("Hello", 320, 240, 0);
uiWindowSetMargined(w, 1);
b = uiNewVerticalBox();
uiBoxSetPadded(b, 1);
uiWindowSetChild(w, uiControl(b));
e = uiNewMultilineEntry();
uiMultilineEntrySetReadOnly(e, 1);
btn = uiNewButton("Say Something");
uiButtonOnClicked(btn, saySomething, NULL);
uiBoxAppend(b, uiControl(btn), 0);
uiBoxAppend(b, uiControl(e), 1);
uiTimer(1000, sayTime, NULL);
uiWindowOnClosing(w, onClosing, NULL);
uiControlShow(uiControl(w));
uiMain();
return 0;
}

2
ui.h
View File

@ -62,6 +62,8 @@ _UI_EXTERN void uiQuit(void);
_UI_EXTERN void uiQueueMain(void (*f)(void *data), void *data);
_UI_EXTERN void uiTimer(int milliseconds, int (*f)(void *data), void *data);
_UI_EXTERN void uiOnShouldQuit(int (*f)(void *data), void *data);
_UI_EXTERN void uiFreeText(char *text);

View File

@ -106,3 +106,7 @@ void uiQueueMain(void (*f)(void *data), void *data)
q->data = data;
gdk_threads_add_idle(doqueued, q);
}
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
{
}

View File

@ -128,3 +128,7 @@ void uiQueueMain(void (*f)(void *data), void *data)
// LONGTERM this is likely not safe to call across threads (allocates memory)
logLastError(L"error queueing function to run on main thread");
}
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
{
}