swk

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

test.c (5023B)


      1 #include <stdio.h>
      2 #include <unistd.h>
      3 #include <swk.h>
      4 
      5 static int count = 3;
      6 static int pccount = 0;
      7 static char text[64];
      8 static char pctext[64];
      9 static SwkBox helloworld[];
     10 static SwkBox about[];
     11 static SwkBox *opt = NULL;
     12 static void mybutton(SwkEvent *e);
     13 static void mybutton_about(SwkEvent *e);
     14 static void mybutton_about_ok(SwkEvent *e);
     15 
     16 static void mybutton(SwkEvent *e) {
     17 	if(e->type == EClick) {
     18 		sprintf(text, "Do it again %d times", count);
     19 		helloworld[0].text = text;
     20 		if(opt == NULL)
     21 			printf("Option: none\n");
     22 		else printf("Option: %s\n", opt->text);
     23 		if(count-- == 0)
     24 			swk_exit(e->win);
     25 	}
     26 	swk_bigbutton(e);
     27 }
     28 
     29 static void myprogressbutton(SwkEvent *e) {
     30 	if(e->type == EClick) {
     31 		pccount+=6;
     32 		if(pccount > 100) {
     33 			pccount = 0;
     34 			e->win->boxes[e->win->col] = helloworld;
     35 			swk_update(e->win);
     36 		}
     37 		sprintf(pctext, "%d%%", pccount);
     38 		about[11].text = pctext;
     39 	}
     40 	swk_button(e);
     41 }
     42 
     43 static SwkBox about[] = {
     44 	SWK_BOX_NEWLINE(0),
     45 	{ .cb=swk_label, .text="About this program...", },
     46 	SWK_BOX_NEWLINE(1),
     47 	{ .cb=swk_separator },
     48 	 SWK_BOX_NEWLINE(-1),
     49 	//SWK_BOX_NEWLINE(2),
     50 	{ .cb=swk_label, .text="This is a test program for swk" },
     51 	SWK_BOX_NEWLINE(1),
     52 	{ .cb=swk_label, .text=" ... a simple widget kit " },
     53 	SWK_BOX_NEWLINE(1),
     54 	{ .cb=swk_label, .text="    ... from the suckless.org project" },
     55 	SWK_BOX_NEWLINE(2),
     56 	{ .cb=swk_progress, .text="0%", },
     57 	SWK_BOX_NEWLINE(2),
     58 	{ .cb=swk_filler },
     59 	{ .cb=myprogressbutton, .text="next", },
     60 	{ .cb=swk_filler },
     61 	//SWK_BOX_NEWLINE(3),
     62 	{ .cb=swk_filler },
     63 	{ .cb=mybutton_about_ok, .text="Ok" },
     64 	{ .cb=NULL }
     65 };
     66 
     67 static void animate(SwkWindow *w, int s) {
     68 	int i;
     69 	if(w->colpos<1)
     70 		w->colpos = 1;
     71 	if(s<0 && w->colpos>w->r.w)
     72 		w->colpos=w->r.w;
     73 	s*=(w->r.w/20);
     74 	for(i=0;i<100;i++) {
     75 		if (w->colpos<1) {
     76 			w->colpos=0;
     77 			break;
     78 		}
     79 		if (w->colpos>w->r.w) {
     80 			w->colpos=w->r.w;
     81 			break;
     82 		}
     83 		w->colpos+=s;
     84 		swk_update(w);
     85 		usleep(10000);
     86 	}
     87 	w->colpos=s>0?w->r.w:0;
     88 	w->col=(s>0)?0:1;
     89 	w->box=w->boxes[s>0?1:0];
     90 }
     91 static void mybutton_about_ok(SwkEvent *e) {
     92 	if(e->type == EClick) {
     93 		animate(e->win, -1);
     94 		e->win->boxes[0] = helloworld;
     95 		animate(e->win, 1);
     96 		swk_update(e->win);
     97 	}
     98 	swk_button(e);
     99 }
    100 
    101 static void mybutton_about(SwkEvent *e) {
    102 	if(e->type == EClick) {
    103 		animate(e->win, -1);
    104 		e->win->boxes[0] = about;
    105 		swk_update(e->win);
    106 	}
    107 	swk_button(e);
    108 }
    109 
    110 static void mybutton_shrink(SwkEvent *e) {
    111 	if(e->type == EClick) {
    112 		animate(e->win, 1);
    113 		swk_update(e->win);
    114 	}
    115 	swk_button(e);
    116 }
    117 
    118 static void mybutton_close(SwkEvent *e) {
    119 	if(e->type == EClick) {
    120 		e->win->boxes[0] = helloworld;
    121 		swk_update(e->win);
    122 	}
    123 	swk_button(e);
    124 }
    125 
    126 static SwkBox scrollwin[] = {
    127 	SWK_BOX_NEWLINE(0),
    128 	{ .cb=swk_label, .text="Scroll to change value", },
    129 	SWK_BOX_NEWLINE(1),
    130 	{ .cb=mybutton_close, .text="Close" },
    131 	{ .cb=swk_separator },
    132 	SWK_BOX_NEWLINE(2),
    133 	{ .cb=swk_label, .text="  /etc" },
    134 	SWK_BOX_NEWLINE(1),
    135 	{ .cb=swk_button, .text="/bin" },
    136 	SWK_BOX_NEWLINE(1),
    137 	{ .cb=swk_label, .text="  /sbin" },
    138 	SWK_BOX_NEWLINE(-1),
    139 	{ .cb=NULL }
    140 };
    141 
    142 static void mybutton_numscroll(SwkEvent *e) {
    143 	if(e->type == EClick) {
    144 		e->win->boxes[0] = scrollwin;
    145 		swk_update(e->win);
    146 	}
    147 	swk_button(e);
    148 }
    149 
    150 Text txt = {"Hello\nworld\n"};
    151 
    152 static SwkBox helloworld[] = {
    153 	{ .cb=swk_label, .text="Press a button", },
    154 	SWK_BOX_NEWLINE(1),
    155 	{ .cb=swk_separator },
    156 	SWK_BOX_NEWLINE(2),
    157 	{ .cb=swk_label, .text="Username:", },
    158 	{ .cb=swk_entry, .text="____", },
    159 	SWK_BOX_NEWLINE(1),
    160 	{ .cb=swk_label, .text="Password:", },
    161 	{ .cb=swk_password, .text="1234", },
    162 	SWK_BOX_NEWLINE(-1),
    163 	{ .cb=swk_label, .text="Click here ->" },
    164 	{ .cb=swk_sketch },
    165 	SWK_BOX_NEWLINE(2),
    166 	{ .cb=swk_label, .text="multiline:" },
    167 	{ .cb=swk_text, .text="Hello\nWorld\n", .data=&txt, .r.h=4, .r.w=10 },
    168 	{ .cb=swk_filler, },
    169 	SWK_BOX_NEWLINE(1),
    170 	{ .cb=swk_image, .text="image.png" },
    171 	{ .cb=swk_image, .text="image.png" },
    172 	{ .cb=swk_image, .text="image.png" },
    173 	{ .cb=swk_image, .text="image.png" },
    174 	{ .cb=swk_filler, },
    175 	SWK_BOX_NEWLINE(3),
    176 	{ .cb=mybutton, .text="yes" },
    177 	{ .cb=mybutton, .text="no" },
    178 	{ .cb=swk_filler, },
    179 	SWK_BOX_NEWLINE(2),
    180 	{ .cb=swk_option, .text="zoot" },
    181 	SWK_BOX_NEWLINE(1),
    182 	{ .cb=swk_option, .text="remember values", .data=&opt },
    183 //	{ .cb=swk_option, .text="null" },
    184 	SWK_BOX_NEWLINE(1),
    185 	{ .cb=swk_option, .text="pasta barata", .data=&opt },
    186 	SWK_BOX_NEWLINE(2),
    187 	{ .cb=swk_label, .text="--swktest", },
    188 	{ .cb=mybutton_about, .text="about" },
    189 	{ .cb=mybutton_numscroll, .text="num" },
    190 	{ .cb=NULL }
    191 };
    192 
    193 static SwkBox column[] = {
    194 	{ .cb=swk_label, .text="this is a column", },
    195 	SWK_BOX_NEWLINE(-1),
    196 	{ .cb=mybutton_shrink, .text="shrink", },
    197 	{ .cb=swk_label, .text="hide this column", },
    198 	{ .cb=NULL }
    199 };
    200 
    201 int
    202 main() {
    203 	SwkWindow w = {
    204 		.title="Hello World",
    205 		.boxes={ helloworld, column },
    206 		.box=helloworld+10,
    207 		//.r = { 0, 0, 320, 240 },
    208 /*
    209 	// TODO: application workflow
    210 	.ok=cb
    211 	.cancel=cb
    212 */
    213 	};
    214 	if(!swk_use(&w))
    215 		return 1;
    216 swk_scroll_down();
    217 swk_scroll_down();
    218 swk_scroll_down();
    219 swk_scroll_down();
    220 	swk_loop();
    221 	return 0;
    222 }