From c5280cb4b3b76757519d7bb4b660209eaefe9d61 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 6 Mar 2013 21:52:33 +0000
Subject: [PATCH] [Add] More list support. More shipyard features. Allow for
 NULL text in widget.

---
 src/land.c    | 31 +++++++++++++++++++++++++++++++
 src/ship.c    | 10 ++++++++++
 src/ship.h    |  1 +
 src/toolkit.c | 19 +++++++++++++++----
 4 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/src/land.c b/src/land.c
index e8dcca0..2ee60d2 100644
--- a/src/land.c
+++ b/src/land.c
@@ -1,3 +1,4 @@
+#include "log.h"
 #include "toolkit.h"
 #include "player.h"
 #include "rng.h"
@@ -41,14 +42,20 @@ static int land_wid = 0; // Primary land window.
 static int secondary_wid = 0; // For the second opened land window (We can only have 2 max).
 static Planet* planet = NULL;
 
+// Commodity excahnge.
 static void commodity_exchange(void);
 static void commodity_exchange_close(char* str);
+// Outfits.
 static void outfits(void);
 static void outfits_close(char* str);
+// Shipyard.
 static void shipyard(void);
 static void shipyard_close(char* str);
+static void shipyard_update(char* str);
+// Spaceport bar.
 static void spaceport_bar(void);
 static void spaceport_bar_close(char* str);
+// News.
 static void news(void);
 static void news_close(char* str);
 
@@ -93,6 +100,9 @@ static void outfits_close(char* str) {
 }
 
 static void shipyard(void) {
+	char** ships;
+	int nships;
+
 	secondary_wid = window_create("Shipyard",
 				SHIPYARD_XPOS, SHIPYARD_YPOS,
 				SHIPYARD_WIDTH, SHIPYARD_HEIGHT);
@@ -100,6 +110,20 @@ static void shipyard(void) {
 	window_addButton(secondary_wid, -20, 20,
 				BUTTON_WIDTH, BUTTON_HEIGHT, "btnCloseShipyard",
 				"Close", shipyard_close);
+	
+	window_addButton(secondary_wid, -40-BUTTON_WIDTH, 20,
+				BUTTON_WIDTH, BUTTON_HEIGHT, "btnBuyShip",
+				"Buy", NULL);
+
+	window_addText(secondary_wid, 20+200+20, -160,
+				SHIPYARD_WIDTH-260, 200, 0, "txtDescription",
+				&gl_defFont, &cConsole, NULL);
+
+	// Setup the ships to buy/sell.
+	ships = ship_getAll(&nships);
+	window_addList(secondary_wid, 20, 40,
+				200, SHIPYARD_HEIGHT-120, "lstShipyard",
+				ships, nships, 0, shipyard_update);
 }
 
 static void shipyard_close(char* str) {
@@ -107,6 +131,13 @@ static void shipyard_close(char* str) {
 		window_destroy(secondary_wid);
 }
 
+static void shipyard_update(char* str) {
+	(void)str;
+	char* tship;
+
+	tship = toolkit_getList(secondary_wid, "lstShipyard");
+}
+
 // Spaceport bar.
 static void spaceport_bar(void) {
   secondary_wid = window_create("SpacePort Bar", -1, -1, BAR_WIDTH, BAR_HEIGHT);
diff --git a/src/ship.c b/src/ship.c
index dfe5a5d..2987cfc 100644
--- a/src/ship.c
+++ b/src/ship.c
@@ -46,6 +46,16 @@ static char* ship_classes[] = {
 	"NULL",	"Civialian Light", "Civilian Medium", "Civilian Heavy"
 };
 
+// Return all the ships in text form.
+char** ship_getAll(int* n) {
+	char** shipnames = malloc(sizeof(Ship*) * ships);
+
+	for((*n)=0; (*n) < ships; (*n)++)
+		shipnames[*n] = strdup(ship_stack[*n].name);
+	
+	return shipnames;
+}
+
 static char* ship_class(Ship* s) {
 	return ship_classes[s->class];
 }
diff --git a/src/ship.h b/src/ship.h
index c6d1778..7920e91 100644
--- a/src/ship.h
+++ b/src/ship.h
@@ -59,6 +59,7 @@ typedef struct Ship_ {
 
 // Get.
 Ship* ship_get(const char* name);
+char** ship_getAll(int* n);
 
 // Load/quit.
 int ships_load(void);
diff --git a/src/toolkit.c b/src/toolkit.c
index bfeb4bd..deac402 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -153,7 +153,8 @@ void window_addText(const unsigned int wid, const int x, const int y,
   else wgt->y = (double) y;
   wgt->dat.txt.colour = colour;
   wgt->dat.txt.centered = centered;
-  wgt->dat.txt.text = strdup(string);
+  if(string) wgt->dat.txt.text = strdup(string);
+	else wgt->dat.txt.text = NULL;
 }
 
 // Add a graphic to the window.
@@ -315,15 +316,23 @@ unsigned int window_create(char* name, const int x, const int y, const int w, co
 
 // Destroy a widget.
 static void widget_cleanup(Widget* widget) {
+	int i;
   if(widget->name) free(widget->name);
   
   switch(widget->type) {
-    case WIDGET_BUTTON:
+    case WIDGET_BUTTON: // Must clear the button display text.
       if(widget->dat.btn.display) free(widget->dat.btn.display);
       break;
-    case WIDGET_TEXT:
+    case WIDGET_TEXT: // Must clear the text.
       if(widget->dat.txt.text) free(widget->dat.txt.text);
       break;
+		case WIDGET_LIST: // Must clear the list.
+			if(widget->dat.lst.options) {
+				for(i = 0; i < widget->dat.lst.noptions; i++)
+					free(widget->dat.lst.options[i]);
+				free(widget->dat.lst.options);
+			}
+			break;
     default:
       break;
   }
@@ -647,8 +656,10 @@ static void toolkit_renderButton(Widget* btn, double bx, double by) {
 }
 
 static void toolkit_renderText(Widget* txt, double bx, double by) {
+	if(txt->dat.txt.text == NULL) return;
+
   if(txt->dat.txt.centered)
-    gl_printMid(txt->dat.txt.font, txt->w,
+  gl_printMid(txt->dat.txt.font, txt->w,
           bx + (double)gl_screen.w/2. + txt->x,
           by + (double)gl_screen.h/2. + txt->y,
           txt->dat.txt.colour, txt->dat.txt.text);