]> git.ipfire.org Git - thirdparty/newt.git/blobdiff - button.c
install python modules to purelib and platlib
[thirdparty/newt.git] / button.c
index f1a35930b415ceda64675e0c0977b77a6e45da13..8eb5e5a9021f86046671c258cec48804df92eb56 100644 (file)
--- a/button.c
+++ b/button.c
@@ -27,13 +27,25 @@ static struct componentOps buttonOps = {
     newtDefaultMappedHandler,
 } ;
 
+/* 
+ * return NULL on malloc error.
+ *  FIXME: all createButton calls should check for error
+ */
 static newtComponent createButton(int left, int row, const char * text, int compact) {
     newtComponent co;
     struct button * bu;
+    int width = wstrlen(text,-1);
 
     co = malloc(sizeof(*co));
+    if (co == NULL) 
+       return NULL;
     bu = malloc(sizeof(struct button));
+    if (bu == NULL)  {
+       free (co);
+       return NULL;
+    }
     co->data = bu;
+    co->destroyCallback = NULL;
 
     bu->text = strdup(text);
     bu->compact = compact;
@@ -41,10 +53,10 @@ static newtComponent createButton(int left, int row, const char * text, int comp
 
     if (bu->compact) {
        co->height = 1;
-       co->width = strlen(text) + 3;
+       co->width = width + 3;
     } else {
        co->height = 4;
-       co->width = strlen(text) + 5;
+       co->width = width + 5;
     }
 
     co->top = row;
@@ -55,6 +67,7 @@ static newtComponent createButton(int left, int row, const char * text, int comp
     newtGotorc(co->top, co->left);
 
     return co;
+
 }
 
 newtComponent newtCompactButton(int left, int row, const char * text) {
@@ -92,10 +105,12 @@ static void buttonDrawIt(newtComponent co, int active, int pushed) {
     SLsmg_set_color(NEWT_COLORSET_BUTTON);
 
     if (bu->compact) {
-       if (active) 
+       if (!active)
            SLsmg_set_color(NEWT_COLORSET_COMPACTBUTTON);
-       else
+       else if (SLtt_Use_Ansi_Colors)
            SLsmg_set_color(NEWT_COLORSET_BUTTON);
+       else
+           SLsmg_set_color(NEWT_COLORSET_ACTBUTTON);
        newtGotorc(co->top+ pushed, co->left + 1 + pushed);
        SLsmg_write_char('<');
        SLsmg_write_string(bu->text);
@@ -114,6 +129,8 @@ static void buttonDrawIt(newtComponent co, int active, int pushed) {
 
        buttonDrawText(co, active, pushed);
     }
+    /* put cursor at beginning of text for better accessibility */
+    newtGotorc(co->top + (bu->compact ? 0 : 1) + pushed, co->left + 1 + pushed + 1);
 }
 
 static void buttonDrawText(newtComponent co, int active, int pushed) {
@@ -137,6 +154,8 @@ static struct eventResult buttonEvent(newtComponent co,
     struct eventResult er;
     struct button * bu = co->data;
 
+    er.result = ER_IGNORED;
+
     if (ev.when == EV_NORMAL) {
        switch (ev.event) {
          case EV_FOCUS:
@@ -162,12 +181,28 @@ static struct eventResult buttonEvent(newtComponent co,
                }
 
                er.result = ER_EXITFORM;
-           } else 
+           } else
                er.result = ER_IGNORED;
            break;
+         case EV_MOUSE:
+             if (ev.u.mouse.type == MOUSE_BUTTON_DOWN &&
+                 co->top <= ev.u.mouse.y &&
+                 co->top + co->height - !bu->compact > ev.u.mouse.y &&
+                 co->left <= ev.u.mouse.x &&
+                 co->left + co->width - !bu->compact > ev.u.mouse.x) {
+                 if (!bu->compact) {
+                     buttonDrawIt(co, 1, 1);
+                     newtRefresh();
+                     newtDelay(150000);
+                     buttonDrawIt(co, 1, 0);
+                     newtRefresh();
+                     newtDelay(150000);
+                 }
+                 er.result = ER_EXITFORM;
+             }
+           break;
        }
-    } else 
-       er.result = ER_IGNORED;
+    }
 
     return er;
 }