]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
version 0.50.1
authorewt <ewt>
Sat, 11 Dec 1999 18:59:15 +0000 (18:59 +0000)
committerewt <ewt>
Sat, 11 Dec 1999 18:59:15 +0000 (18:59 +0000)
CHANGES
form.c
newt.h
newt.spec
test.c

diff --git a/CHANGES b/CHANGES
index b96b3e36857d753bf3e9e837157d4a1fa1ee95f2..36a8aa6933ad2f6a5f9a917a216c3fd583676fab 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,6 @@
+0.50 -> 0.51
+       - added newtFormSetTimer() (and test case, and python)
+
 0.40 -> 0.50
        - added CheckboxTree widget
        - vastly improved python bindings
diff --git a/form.c b/form.c
index 67ae70a1d2a44657df8d7546112d8d6021bddded..d4a966b7d5d409ec41f3f2fd54d56081fb80fa88 100644 (file)
--- a/form.c
+++ b/form.c
@@ -4,6 +4,7 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <sys/select.h>
+#include <sys/time.h>
 
 #ifdef USE_GPM
 #include <ctype.h>
@@ -395,6 +396,8 @@ struct form {
     int numFds;
     struct fdInfo * fds;
     int maxFd;
+    int timer;    /* in milliseconds */
+    struct timeval lastTimeout;
 };
 
 static void gotoComponent(struct form * form, int newComp);
@@ -494,6 +497,12 @@ void newtFormSetCurrent(newtComponent co, newtComponent subco) {
     gotoComponent(form, new);
 }
 
+void newtFormSetTimer(newtComponent co, int millisecs) {
+    struct form * form = co->data;
+
+    form->timer = millisecs;
+}
+
 void newtFormSetHeight(newtComponent co, int height) {
     struct form * form = co->data;
 
@@ -864,6 +873,7 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
     int key, i, max;
     int done = 0;
     fd_set readSet, writeSet;
+    struct timeval nextTimeout, now, timeout;
 #ifdef USE_GPM
     int x, y;
     Gpm_Connect conn;
@@ -887,6 +897,16 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
     } else
        gotoComponent(form, form->currComp);
 
+    /* Calculate when we next need to return with a timeout */
+    if (form->timer) {
+       if (!form->lastTimeout.tv_sec && !form->lastTimeout.tv_usec)
+           gettimeofday(&form->lastTimeout, NULL);
+
+        nextTimeout.tv_sec = form->lastTimeout.tv_sec + (form->timer / 1000);
+       nextTimeout.tv_usec = form->lastTimeout.tv_usec + 
+                               (form->timer % 1000) * 1000;
+    }
+
     while (!done) {
        newtRefresh();
 
@@ -909,9 +929,39 @@ void newtFormRun(newtComponent co, struct newtExitStruct * es) {
                FD_SET(form->fds[i].fd, &writeSet);
        }
 
-       i = select(max + 1, &readSet, &writeSet, NULL, NULL);
+       if (form->timer) {
+           gettimeofday(&now, 0);
+
+           if (now.tv_sec > nextTimeout.tv_sec) {
+               timeout.tv_sec = timeout.tv_usec = 0;
+           } else if (now.tv_sec == nextTimeout.tv_sec) {
+               timeout.tv_sec = 0;
+               if (now.tv_usec > nextTimeout.tv_usec)
+                   timeout.tv_usec = 0;
+               else
+                   timeout.tv_usec = nextTimeout.tv_usec - now.tv_usec;
+           } else if (now.tv_sec < nextTimeout.tv_sec) {
+               timeout.tv_sec = nextTimeout.tv_sec - now.tv_sec;
+               if (now.tv_usec > nextTimeout.tv_usec)
+                   timeout.tv_sec--,
+                   timeout.tv_usec = nextTimeout.tv_usec + 1000000 -
+                                       now.tv_usec;
+               else 
+                   timeout.tv_usec = nextTimeout.tv_usec - now.tv_usec;
+           }
+       } else {
+           timeout.tv_sec = timeout.tv_usec = 0;
+       }
+
+       i = select(max + 1, &readSet, &writeSet, NULL, 
+                       form->timer ? &timeout : NULL);
        if (i < 0) continue;    /* ?? What should we do here? */
 
+       if (i == 0) {
+           done = 1;
+           es->reason = NEWT_EXIT_TIMEOUT;
+           gettimeofday(&form->lastTimeout, NULL);
+       } else
 #ifdef USE_GPM
        if (gpm_fd > 0 && FD_ISSET(gpm_fd, &readSet)) {
            Gpm_GetEvent(&event);
diff --git a/newt.h b/newt.h
index b58ba0d736e64b5bfa0a44bd0195b841136747d0..4511df39d1ca970255c80b5db4297a26f99c1ff5 100644 (file)
--- a/newt.h
+++ b/newt.h
@@ -189,7 +189,8 @@ char * newtReflowText(char * text, int width, int flexDown, int flexUp,
                      int * actualWidth, int * actualHeight);
 
 struct newtExitStruct {
-    enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT, NEWT_EXIT_FDREADY } reason;
+    enum { NEWT_EXIT_HOTKEY, NEWT_EXIT_COMPONENT, NEWT_EXIT_FDREADY,
+          NEWT_EXIT_TIMEOUT } reason;
     union {
        int key;
        newtComponent co;
@@ -197,6 +198,7 @@ struct newtExitStruct {
 } ;
 
 newtComponent newtForm(newtComponent vertBar, const char * help, int flags);
+void newtFormSetTimer(newtComponent form, int millisecs);
 void newtFormWatchFd(newtComponent form, int fd, int fdFlags);
 void newtFormSetSize(newtComponent co);
 newtComponent newtFormGetCurrent(newtComponent co);
index b2dc71909f2b354e14ec8832928e1fc3dd0944c3..70879f5296e2c8954f7c98e859dc2ebdfa2e4d14 100644 (file)
--- a/newt.spec
+++ b/newt.spec
@@ -1,6 +1,6 @@
 Summary: A development library for text mode user interfaces.
 Name: newt
-%define version 0.50
+%define version 0.50.1
 Version: %{version}
 Release: 14
 Copyright: LGPL
diff --git a/test.c b/test.c
index e88a279ce8222e8e73e3589ffb85b42f1110144d..1455b98c4dffd0dba6edc3fee6c21ef7b62c4d6f 100644 (file)
--- a/test.c
+++ b/test.c
@@ -30,7 +30,7 @@ void suspend(void * d) {
 
 int main(void) {
     newtComponent b1, b2, r1, r2, r3, e2, e3, l1, l2, l3, scale;
-    newtComponent lb, t, rsf, answer;
+    newtComponent lb, t, rsf, answer, timeLabel;
     newtComponent cs[10];
     newtComponent f, chklist, e1;
     struct callbackInfo cbis[3];
@@ -39,6 +39,9 @@ int main(void) {
     void ** selectedList;
     int i, numsel;
     char buf[20];
+    const char * spinner = "-\\|/\\|/";
+    const char * spinState;
+    struct newtExitStruct es;
 
     newtInit();
     newtCls();
@@ -104,21 +107,30 @@ int main(void) {
     newtListboxInsertEntry(lb, "Eleventh", (void *) 11, (void *) 10);
     newtListboxDeleteEntry(lb, (void *) 11);
 
+    spinState = spinner;
+    timeLabel = newtLabel(45, 8, "Spinner: -");
+
     t = newtTextbox(45, 10, 17, 5, NEWT_FLAG_WRAP);
     newtTextboxSetText(t, "This is some text does it look okay?\nThis should be alone.\nThis shouldn't be printed");
 
-    newtFormAddComponents(f, lb, t, NULL);
+    newtFormAddComponents(f, lb, timeLabel, t, NULL);
     newtRefresh();
+    newtFormSetTimer(f, 200);
 
     do {
-       answer = newtRunForm(f);
+       newtFormRun(f, &es);
 
-       if (answer == b2) {
+       if (es.reason == NEWT_EXIT_COMPONENT && es.u.co == b2) {
            newtScaleSet(scale, atoi(scaleVal));
            newtRefresh();
            answer = NULL;
+       } else if (es.reason == NEWT_EXIT_TIMEOUT) {
+           spinState++;
+           if (!*spinState) spinState = spinner;
+           sprintf(buf, "Spinner: %c", *spinState);
+           newtLabelSetText(timeLabel, buf);
        }
-    } while (!answer);
+    } while (es.reason != NEWT_EXIT_COMPONENT);
 
     scaleVal = strdup(scaleVal);
     enr2 = strdup(enr2);