From 9d08ff3ae06b2d9f5b68b49f651dcfe65144f4d2 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 6 Mar 2013 22:28:46 +0000
Subject: [PATCH] [Add] New rectangle widget. More widget creation checking.

---
 src/toolkit.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++-----
 src/toolkit.h |  5 ++++
 2 files changed, 69 insertions(+), 6 deletions(-)

diff --git a/src/toolkit.c b/src/toolkit.c
index deac402..7de1b27 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -12,7 +12,8 @@ typedef enum WidgetType_ {
   WIDGET_BUTTON,
   WIDGET_TEXT,
   WIDGET_IMAGE,
-  WIDGET_LIST
+  WIDGET_LIST,
+	WIDGET_RECT
 } WidgetType;
 
 typedef enum WidgetStatus_ {
@@ -55,6 +56,11 @@ typedef struct Widget_ {
 			int pos;				// Current topmost option (in view).
 			void (*fptr) (char*); // Modify callback.
     } lst;
+		// Widget rect.
+		struct {
+			glColour* colour; // Background colour.
+			int border;				// Border.
+		} rct;
   } dat;
 } Widget;
 
@@ -101,6 +107,7 @@ static void toolkit_renderButton(Widget* btn, double bx, double by);
 static void toolkit_renderText(Widget* txt, double bx, double by);
 static void toolkit_renderImage(Widget* img, double bx, double by);
 static void toolkit_renderList(Widget* lst, double bx, double by);
+static void toolkit_renderRect(Widget* rct, double bx, double by);
 static void toolkit_drawOutline(double x, double y, double w,
 			double h, double b, glColour* c, glColour* lc);
 static void toolkit_drawRect(double x, double y, double w, double h,
@@ -151,7 +158,8 @@ void window_addText(const unsigned int wid, const int x, const int y,
   else wgt->x = (double)x;
   if(y < 0) wgt->y = wdw->h + y - h;
   else wgt->y = (double) y;
-  wgt->dat.txt.colour = colour;
+	if(colour == NULL) wgt->dat.txt.colour = &cBlack;
+	else wgt->dat.txt.colour = colour;
   wgt->dat.txt.centered = centered;
   if(string) wgt->dat.txt.text = strdup(string);
 	else wgt->dat.txt.text = NULL;
@@ -167,11 +175,14 @@ void window_addImage(const unsigned int wid, const int x, const int y,
   wgt->type = WIDGET_IMAGE;
   wgt->name = strdup(name);
 
-  // Set the propertied.
+  // Set the properties.
   wgt->dat.img.image = image;
-  if(x < 0) wgt->x = wdw->w - wgt->dat.img.image->sw + x;
+	wgt->w = (image == NULL) ? 0 : wgt->dat.img.image->sw;
+	wgt->h = (image == NULL) ? 0 : wgt->dat.img.image->sh;
+  if(x < 0) wgt->x = wdw->w - wgt->w + x;
   else wgt->x = (double)x;
-  if(y < 0) wgt->y = wdw->h + y;
+	if(y < 0) wgt->y = wdw->h + y;
+  //if(y < 0) wgt->y = wdw->h - wgt->h + y;
   else wgt->y = (double)y;
 }
 
@@ -202,6 +213,26 @@ void window_addList(const unsigned int wid, const int x, const int y,
 		toolkit_nextFocus();
 }
 
+void window_addRect(const unsigned int wid, const int x, const int y, const int w,
+			const int h, char* name, glColour* colour, int border) {
+	
+	Window* wdw = window_wget(wid);
+	Widget* wgt = window_newWidget(wdw);
+
+	wgt->type = WIDGET_RECT;
+	wgt->name = strdup(name);
+
+	wgt->dat.rct.colour = colour;
+	wgt->dat.rct.border	= border;
+
+	wgt->w = (double)w;
+	wgt->h = (double)h;
+	if(x < 0) wgt->x = wdw->w - wgt->w + x;
+	else wgt->x = (double)x;
+	if(y < 0) wgt->y = wdw->h - wgt->h + y;
+	else wgt->y = (double)y;
+}
+
 // Return pointer to newly allocated widget.
 static Widget* window_newWidget(Window* w) {
   Widget* wgt = NULL;
@@ -595,6 +626,9 @@ static void window_render(Window* w) {
       case WIDGET_LIST:
 				toolkit_renderList(&w->widgets[i], x, y);
         break;
+			case WIDGET_RECT:
+				toolkit_renderRect(&w->widgets[i], x, y);
+				break;
     }
   }
 	// Focus widget.
@@ -736,6 +770,30 @@ static void toolkit_renderList(Widget* lst, double bx, double by) {
 	}
 }
 
+// Render a rectangle.
+static void toolkit_renderRect(Widget* rct, double bx, double by) {
+	double x, y;
+	glColour* lc, *c, *oc;
+
+	x = bx + rct->x;
+	y = by + rct->y;
+
+	lc 	= &cGrey90;
+	c		= &cGrey70;
+	oc 	= &cGrey30;
+
+	if(rct->dat.rct.colour)
+		// Draw rect only if it exists.
+		toolkit_drawRect(x, y, rct->w, rct->h, rct->dat.rct.colour, NULL);
+
+	if(rct->dat.rct.border) {
+		// Inner outline.
+		toolkit_drawOutline(x, y, rct->w, rct->h, 0., lc, c);
+		// Outter outline.
+		toolkit_drawOutline(x, y, rct->w, rct->h, 1., oc, NULL);
+	}
+}
+
 // Render the window.
 void toolkit_render(void) {
   int i;
@@ -981,7 +1039,7 @@ static void toolkit_listFocus(Widget* lst, double bx, double by) {
 char* toolkit_getList(const unsigned int wid, char* name) {
 	Widget* wgt = window_getwgt(wid, name);
 
-	if((wgt->type != WIDGET_LIST) || (wgt->dat.lst.selected != -1))
+	if((wgt->type != WIDGET_LIST) || (wgt->dat.lst.selected == -1))
 		return NULL;
 	
 	return wgt->dat.lst.options[wgt->dat.lst.selected];
diff --git a/src/toolkit.h b/src/toolkit.h
index 3ff1df6..a9faddf 100644
--- a/src/toolkit.h
+++ b/src/toolkit.h
@@ -25,6 +25,11 @@ void window_addList(const unsigned int wid,
 			char* name, char** items, int nitems, int defitem,
 			void(*call)(char*));
 
+void window_addRect(const unsigned int wid,
+			const int x, const int y, // Position.
+			const int w, const int h, // size.
+			char* name, glColour* colour, int border); // Properties.
+
 // Modification
 void window_modifyText(const unsigned int wid, char* name, char* newstring);
 void window_modifyImage(const unsigned int wid, char* name, glTexture* image);