swk

static widget kit
git clone git://git.suckless.org/swk
Log | Files | Refs | README | LICENSE

ui.c (3924B)


      1 /* See LICENSE file for copyright and license details. */
      2 // ascii-art user interface //
      3 #if 0
      4 	{HelloWorld
      5 	'Press a button'
      6 	----------------
      7 	(save password)
      8 	(store options)
      9 	[Ok] [Cancel]
     10 	================
     11 	'This is new'
     12 	<image.png>
     13 	}
     14 
     15 SwkBox *b = swk_ui_get(w, "ok");
     16 #endif
     17 
     18 #include <swk.h>
     19 #include <stdio.h>
     20 #include <string.h>
     21 #include <stdlib.h>
     22 
     23 // TODO: Rename to swk_win_ swk_window_ ?
     24 void
     25 swk_ui_free(SwkWindow *w) {
     26 	// leaks in box->text ?
     27 	free(w->boxes[1]);
     28 	free(w->title);
     29 	free(w);
     30 }
     31 
     32 SwkWindow *
     33 swk_ui(const char *text) {
     34 	SwkWindow *w = (SwkWindow*)malloc(sizeof(SwkWindow));
     35 	int sz, stri = 0, mode = 0;
     36 	int count = 0;
     37 	char str[128];
     38 	const char *ptr = text;
     39 
     40 	if(!w) return NULL;
     41 	memset(w, 0, sizeof(SwkWindow));
     42 
     43 	// TODO: count widgets and allocate stuff
     44 	for(sz=0; ptr && *ptr; ptr++) {
     45 		// TODO
     46 		switch(*ptr) {
     47 		case '\n':
     48 		case '[':
     49 		case '{':
     50 		case '\'':
     51 			sz++;
     52 			sz++;
     53 		default:
     54 			break;
     55 		}
     56 	}
     57 	printf("WINDETS=%d\n", sz);
     58 
     59 	w->box = w->boxes[0] = (SwkBox*)malloc(128*sizeof(SwkBox)); // Use sz after counting
     60 	w->boxes[1] = NULL;
     61 	memset(w->box, 0, 128*sizeof(SwkBox));
     62 
     63 	while(text && *text) {
     64 		switch(mode) {
     65 		case '\'':
     66 			if ((*text=='\''&&str[stri-1]!='\\') || *text=='\n') {
     67 				printf("label(%s)\n", str);
     68 				stri = mode = 0;
     69 				w->boxes[w->col][count].cb = swk_label;
     70 				w->boxes[w->col][count].text = strdup (str);
     71 				count++;
     72 			} else {
     73 				str[stri++] = *text;
     74 				str[stri] = 0;
     75 			}
     76 			break;
     77 		case '<':
     78 			if ((*text=='>'&&str[stri-1]!='\\') || *text=='\n') {
     79 				printf("image(%s)\n", str);
     80 				stri = mode = 0;
     81 				w->boxes[w->col][count].cb = swk_image;
     82 				w->boxes[w->col][count].text = strdup (str);
     83 				count++;
     84 			} else {
     85 				str[stri++] = *text;
     86 				str[stri] = 0;
     87 			}
     88 			break;
     89 		case '*':
     90 			if (*text=='\n') {
     91 				w->boxes[w->col][count].cb = swk_filler;
     92 				count++;
     93 				mode = 0;
     94 			}
     95 			break;
     96 		case '=':
     97 			if (*text=='\n') {
     98 				SwkBox b = SWK_BOX_NEWLINE(-1);
     99 				w->boxes[w->col][count] = b;
    100 				count++;
    101 				mode = 0;
    102 			}
    103 			break;
    104 		case '-':
    105 			if (*text=='\n') {
    106 				w->boxes[w->col][count].cb = swk_separator;
    107 				count++;
    108 				mode = 0;
    109 			}
    110 			break;
    111 		case '(':
    112 			if ((*text==')'&&str[stri-1]!='\\') || *text=='\n') {
    113 				printf("option(%s)\n", str);
    114 				stri = mode = 0;
    115 				w->boxes[w->col][count].cb = swk_option;
    116 				w->boxes[w->col][count].text = strdup (str);
    117 				count++;
    118 			} else {
    119 				str[stri++] = *text;
    120 				str[stri] = 0;
    121 			}
    122 			break;
    123 		case '$':
    124 			if ((*text=='$'&&str[stri-1]!='\\') || *text=='\n') {
    125 				stri = mode = 0;
    126 				if (*str=='*') {
    127 					printf("pass(%s)\n", str);
    128 					w->boxes[w->col][count].cb = swk_password;
    129 					w->boxes[w->col][count].text = "";
    130 				} else {
    131 					printf("entry(%s)\n", str);
    132 					w->boxes[w->col][count].cb = swk_entry;
    133 					w->boxes[w->col][count].text = strdup (str);
    134 				}
    135 				count++;
    136 			} else {
    137 				str[stri++] = *text;
    138 				str[stri] = 0;
    139 			}
    140 			break;
    141 		case '[':
    142 			if ((*text==']'&&str[stri-1]!='\\') || *text=='\n') {
    143 				printf("button(%s)\n", str);
    144 				stri = mode = 0;
    145 				w->boxes[w->col][count].cb = swk_button;
    146 				w->boxes[w->col][count].text = strdup (str);
    147 				count++;
    148 			} else {
    149 				str[stri++] = *text;
    150 				str[stri] = 0;
    151 			}
    152 			break;
    153 		case '{':
    154 			if (*text=='}' || *text=='\n') {
    155 				printf("WINDOW TITLE(%s)\n", str);
    156 				stri = mode = 0;
    157 				w->title = strdup(str);
    158 			} else {
    159 				str[stri++] = *text;
    160 				str[stri] = 0;
    161 			}
    162 			break;
    163 		default:
    164 			if (*text=='\n') {
    165 				SwkBox b = SWK_BOX_NEWLINE(1);
    166 				w->boxes[w->col][count] = b;
    167 				count++;
    168 			} else {
    169 				mode = *text;
    170 				stri = 0;
    171 				str[0] = 0;
    172 			}
    173 			break;
    174 		}
    175 		text++;
    176 	}
    177 	swk_use(w);
    178 	return w;
    179 }
    180 
    181 #define UI \
    182 	"{Hello World}\n" \
    183 	"'This is a label'\n" \
    184 	"-----------------\n" \
    185 	"\n\n" \
    186 	"$$\n" \
    187 	"=================\n" \
    188 	"[yes][no]\n"
    189 
    190 static SwkWindow *w = NULL;
    191 
    192 int main() {
    193 	w = swk_ui(UI);
    194 	if(!w||!swk_use(w))
    195 		return 1;
    196 	swk_loop(w);
    197 	return 0;
    198 }