shithub: widget

Download patch

ref: 86177eda0338c6773955897d9b5105fe89c980a0
parent: b9f2b8c6a28c2d4744119c7254767a0f1b51ed2e
author: Tevo <[email protected]>
date: Sun Jan 10 12:15:17 EST 2021

Button relese message, stupid fixes, redrawwctl

--- a/cmd/factory/factory.c
+++ b/cmd/factory/factory.c
@@ -50,11 +50,18 @@
 	};
 
 	for(;;)
-	{
 		switch(alt(chans))
 		{
 		case MESSAGE:
-			print("got message for %d!\n", msg->what);
+			switch(msg->what)
+			{
+			case M_BUTTON_PRESSED:
+				print("button %d was pressed!\n", msg->sender->id);
+				break;
+			case M_BUTTON_RELEASED:
+				print("button %d was released!\n", msg->sender->id);
+				break;
+			}
 			free(msg);
 			break;
 		case KEYBOARD:
@@ -64,12 +71,9 @@
 		case RESIZE:
 			if(getwindow(display, Refnone) < 0)
 				sysfatal("getwindow: cannot resize: %r");
-			/* FIXME most users shouldn't need to call this directly */
-			redrawwidget(root, screen, screen->r);
+			redrawwctl(wctl);
 			break;
 		}
-		flushimage(display, 1);
-	}
 
 end:
 	closewidget(wctl);
--- a/libwidget/base.c
+++ b/libwidget/base.c
@@ -19,7 +19,7 @@
 }
 
 void
-widgetmain(Widgetctl* ctl)
+widgetmain(Widgetctl *ctl)
 {
 	Mouse mouse;
 	Rune rune;
@@ -37,6 +37,8 @@
 		MOUSE, KEYBOARD
 	};
 
+	threadsetname("widgetmain");
+
 	for(;;)
 	{
 		while(ctl->root == nil)
@@ -90,13 +92,13 @@
 	ctl->mouse = mouse;
 	ctl->root = root;
 	ctl->kbd = kbd;
-	ctl->c = chancreate(sizeof(Widgetmsg), 16);
+	ctl->c = chancreate(sizeof(Widgetmsg*), 16);
 	ctl->kbdc = chancreate(sizeof(Rune), 20);
 	ctl->mousec = chancreate(sizeof(Mouse), 16);
 	ctl->resizec = mouse->resizec;
 	ctl->flags = flags;
 
-	threadcreate((void(*)(void*))widgetmain, ctl, 16384);
+	ctl->wthread = threadcreate((void(*)(void*))widgetmain, ctl, 16384);
 
 	redrawwidget(root, img, img->r);
 	flushimage(img->display, 1);
@@ -107,6 +109,13 @@
 void
 closewidget(Widgetctl *ctl)
 {
+	chanclose(ctl->c);
+	chanclose(ctl->kbdc);
+	chanclose(ctl->mousec);
+	chanclose(ctl->resizec);
+
+	threadint(ctl->wthread);
+
 	if(ctl->pflags & OURKBD)
 		closekeyboard(ctl->kbd);
 
@@ -113,11 +122,16 @@
 	if(ctl->pflags & OURMOUSE)
 		closemouse(ctl->mouse);
 
-	/* TODO cleanup chans, close threads, etc */
-
 	free(ctl);
 }
 
+void
+redrawwctl(Widgetctl *ctl)
+{
+	redrawwidget(ctl->root, ctl->image, ctl->image->r);
+	flushimage(ctl->image->display, 1);
+}
+
 /* TODO set clipr */
 Point
 redrawwidget(Widget *w, Image *dst, Rectangle r)
@@ -159,7 +173,7 @@
 }
 
 Widgetmsg*
-newmsg(Widget* w, u32int what)
+newmsg(Widget *w, u32int what)
 {
 	Widgetmsg *msg;
 
--- a/libwidget/base.h
+++ b/libwidget/base.h
@@ -28,6 +28,7 @@
 	Channel *kbdc;		/* chan(Rune)[20] */
 	Channel *mousec;	/* chan(Mouse)[16] */
 	Channel *resizec;
+	/* NOTE user retains ownership of the root widget */
 	Widget *root;
 
 	Keyboardctl *kbd;
@@ -39,6 +40,7 @@
 
 	/* user shouln't care about anything below this point */
 	int pflags;
+	int wthread;
 };
 
 enum /* flags */
@@ -67,8 +69,10 @@
 int nextid(void);
 
 Point redrawwidget(Widget*, Image*, Rectangle);
+void redrawwctl(Widgetctl*);
 
 int kbdevent(Widget*, Image*, Rectangle, Rune, Channel* /*(Widgetmsg*)*/);
 int mouseevent(Widget*, Image*, Rectangle, Mouse, Channel* /*(Widgetmsg*)*/);
 
 void freewidget(Widget*);
+
--- a/libwidget/button.c
+++ b/libwidget/button.c
@@ -60,11 +60,8 @@
 	btn = (Button*)w;
 	if((pressed = m.buttons & 1) != btn->pressed)
 	{
-		if(pressed)
-		{
-			msg = newmsg(btn, M_BUTTON_PRESSED);
-			send(chan, &msg);
-		}
+		msg = newmsg(btn, pressed ? M_BUTTON_PRESSED : M_BUTTON_RELEASED);
+		send(chan, &msg);
 		btn->pressed = pressed;
 		btnredraw(btn, dst, rect);
 		return 1;
@@ -97,7 +94,7 @@
 	if(darkblue == nil)
 		darkblue = allocimagemix(display, DPurpleblue, DPalebluegreen);
 
-	btn = emalloc(sizeof(*btn));
+	btn = emallocz(sizeof(*btn), 1);
 	wdefaults(btn);
 	btn->bg			= lightblue;
 	btn->fg			= darkblue;
--- a/libwidget/button.h
+++ b/libwidget/button.h
@@ -1,3 +1,4 @@
+/*** Button ***/
 
 typedef struct Button Button;
 
@@ -16,6 +17,7 @@
 
 enum
 {
-	M_BUTTON_PRESSED = C2I('b', 't', 'n', 'p')
+	M_BUTTON_PRESSED	= C2I('b', 't', 'n', 'p'),
+	M_BUTTON_RELEASED	= C2I('b', 't', 'n', 'r')
 };
 
--- a/libwidget/mkfile
+++ b/libwidget/mkfile
@@ -15,7 +15,8 @@
 	button.h
 
 HFILES=\
-	/sys/include/$HDR
+	/sys/include/$HDR	\
+	w-internal.h
 
 </sys/src/cmd/mksyslib
 
--- a/libwidget/textbox.c
+++ b/libwidget/textbox.c
@@ -9,7 +9,7 @@
 #include <widget.h>
 #include "w-internal.h"
 
-static char* tbkind = "Textbox";
+static char *tbkind = "Textbox";
 
 int
 istextbox(Widget *w)
--- a/libwidget/textbox.h
+++ b/libwidget/textbox.h
@@ -1,3 +1,4 @@
+/*** Textbox ***/
 
 typedef struct Textbox Textbox;
 
@@ -15,3 +16,4 @@
 int istextbox(Widget*);
 
 Textbox* newtextbox(int selectable, int editable, Font*, char *content);
+
--- a/libwidget/w-internal.h
+++ b/libwidget/w-internal.h
@@ -10,6 +10,18 @@
 	return p;
 }
 
+static void*
+emallocz(ulong sz, int clr)
+{
+	void *p;
+
+	p = mallocz(sz, clr);
+	if(p == nil)
+		werror("mallocz: %r");
+	setmalloctag(p, getcallerpc(&sz));
+	return p;
+}
+
 /* Widgetctl->flags */
 enum
 {